Skip to content
This repository was archived by the owner on Aug 23, 2020. It is now read-only.

replace all trit representations from int[] to byte[]#879

Merged
GalRogozinski merged 3 commits intoiotaledger-archive:devfrom
alon-e:feat/tritsAsBytes
Jul 22, 2018
Merged

replace all trit representations from int[] to byte[]#879
GalRogozinski merged 3 commits intoiotaledger-archive:devfrom
alon-e:feat/tritsAsBytes

Conversation

@alon-e
Copy link
Copy Markdown
Contributor

@alon-e alon-e commented Jul 21, 2018

Description

Replace all trit representations from int[] to byte[]
Currently, Trits (-1,0,1) are stored in integer arrays, which waste memory (and compute to GC).
This PR replaces all usage trit representations from int[] to byte[].

Fixes #799

Type of change

  • Enhancement (a non-breaking change which adds functionality)

How Has This Been Tested?

  • Fixed unit tests
  • Syncing a node with brach - improved memory and CPU footprint be > 50%

Checklist:

  • My code follows the style guidelines for this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@alon-e alon-e requested review from GalRogozinski and th0br0 July 21, 2018 19:54
@alon-e alon-e force-pushed the feat/tritsAsBytes branch from b0612d1 to aa06b2e Compare July 21, 2018 20:01
Hash hash = getRandomTransactionHash();
TransactionViewModel transactionViewModel = new TransactionViewModel(trits, hash);
transactionViewModel.store(tangle);
assertArrayEquals(transactionViewModel.trits(), TransactionViewModel.fromHash(tangle, transactionViewModel.getHash()).trits());
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-enabled test and fixed, could move to another PR if you want.

Hash hash = getRandomTransactionHash();
TransactionViewModel transactionViewModel = new TransactionViewModel(trits, hash);
transactionViewModel.store(tangle);
assertArrayEquals(transactionViewModel.getBytes(), TransactionViewModel.fromHash(tangle, transactionViewModel.getHash()).getBytes());
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-enabled test and fixed, could move to another PR if you want.

Transaction transaction = new Transaction();

int[] trits = Arrays.stream(new int[TransactionViewModel.SIGNATURE_MESSAGE_FRAGMENT_TRINARY_SIZE]).map(i -> seed.nextInt(3)-1).toArray();
byte[] trits = new byte[TransactionViewModel.SIGNATURE_MESSAGE_FRAGMENT_TRINARY_SIZE];
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getRandomTransaction impl. appears a few times, better to move to a Utils class.
but not part of this scope.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

TransactionViewModel.BRANCH_TRANSACTION_TRINARY_SIZE);
return trits;
}
public static int[] getRandomTransactionWithTrunkAndBranchValidBundle(Hash trunk, Hash branch) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed b/c not used.

@iotaledger-archive iotaledger-archive deleted a comment Jul 22, 2018
@iotaledger-archive iotaledger-archive deleted a comment Jul 22, 2018
@iotaledger-archive iotaledger-archive deleted a comment Jul 22, 2018
Copy link
Copy Markdown
Contributor

@GalRogozinski GalRogozinski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disliked the if hack to differentiate between the construction of bytes and trits.

If you want to save time I can agree on merging this as long as there is a github issue opened on this matter.

} else {
return validate(trits, minWeightMagnitude, SpongeFactory.create(SpongeFactory.Mode.CURLP81));
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this hack, perhaphs create two methods:
validateTrits and validateBytes.

The above change is just confusing (even though it works)...

this.hash = hash;
weightMagnitude = this.hash.trailingZeros();
transaction.type = FILLED_SLOT;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would create two static factory methods:
createFromBytes and createFromTrits and make the constructor private.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll open an issue on this. but it shouldn't block the goodness from this PR imo.

@@ -16,7 +16,7 @@ public class ISSInPlace {
private static final int MIN_TRIT_VALUE = -1, MAX_TRIT_VALUE = 1;
private static final int MIN_TRYTE_VALUE = -13, MAX_TRYTE_VALUE = 13;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change from int to byte.
Your code works either way, but still...

System.arraycopy(source, sourceOffset, dest, 0, dest.length);
this.tritSafe = new TritSafe(dest);
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again I would encourage the use of factory methods...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll open an issue on this. but it shouldn't block the goodness from this PR imo.

Transaction transaction = new Transaction();

int[] trits = Arrays.stream(new int[TransactionViewModel.SIGNATURE_MESSAGE_FRAGMENT_TRINARY_SIZE]).map(i -> seed.nextInt(3)-1).toArray();
byte[] trits = new byte[TransactionViewModel.SIGNATURE_MESSAGE_FRAGMENT_TRINARY_SIZE];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

return out;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this is more reasdable than the one liner.
Another option was to not change the code and just add new lines before each aggregate call.

Anyhows, both variations are fine with me.

Copy link
Copy Markdown
Contributor

@GalRogozinski GalRogozinski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes will be handled in a different issue

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make the hash class take up less memory

2 participants