-
Notifications
You must be signed in to change notification settings - Fork 266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf: improve decoding in CallTransaction #2647
Conversation
1267e76
to
7d53a8e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job. I have added some comments, mostly with improvements suggestions to improve the readability of some tests. They doesn't prevent the approval, since it wouldn't change the current logic.
@Test | ||
void testBytesLength() { | ||
assertEquals(0, Bytes.of(new byte[]{}).slice(0, 0).length()); | ||
assertEquals(0, Bytes.of(new byte[]{1}).slice(0, 0).length()); | ||
assertEquals(1, Bytes.of(new byte[]{1}).slice(0, 1).length()); | ||
assertEquals(0, Bytes.of(new byte[]{1,2,3}).slice(1, 1).length()); | ||
assertEquals(1, Bytes.of(new byte[]{1,2,3}).slice(1, 2).length()); | ||
assertEquals(2, Bytes.of(new byte[]{1,2,3}).slice(0, 2).length()); | ||
assertEquals(3, Bytes.of(new byte[]{1,2,3}).slice(0, 3).length()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion:
Identify the scenarios you are testing it's a bit tricky, because we don't have clues with variable names and so on. I think we could get advantage of @ParameterizedTest
and a String to tell us the scenario you are testing. Here is a snippet code in how you could change this test. I think that we could get benefit of it or use similar ideas for the other tests below.
@Test | |
void testBytesLength() { | |
assertEquals(0, Bytes.of(new byte[]{}).slice(0, 0).length()); | |
assertEquals(0, Bytes.of(new byte[]{1}).slice(0, 0).length()); | |
assertEquals(1, Bytes.of(new byte[]{1}).slice(0, 1).length()); | |
assertEquals(0, Bytes.of(new byte[]{1,2,3}).slice(1, 1).length()); | |
assertEquals(1, Bytes.of(new byte[]{1,2,3}).slice(1, 2).length()); | |
assertEquals(2, Bytes.of(new byte[]{1,2,3}).slice(0, 2).length()); | |
assertEquals(3, Bytes.of(new byte[]{1,2,3}).slice(0, 3).length()); | |
} | |
@ParameterizedTest | |
@MethodSource("provideInputForBytesLengthTest") | |
void testBytesLength(String testDescription, int expectedLength, byte[] inputArray, int from, int to) { | |
assertEquals(expectedLength, Bytes.of(inputArray).slice(from, to).length()); | |
} | |
static Stream<Arguments> provideInputForBytesLengthTest(){ | |
return Stream.of( | |
Arguments.of("Length 0 for empty array", 0, new byte[]{}, 0,0), | |
Arguments.of("Length 0 for slice with same from and to", 0, new byte[]{1}, 0,0), | |
Arguments.of("Length 1 for slice with distance between from and to", 1, new byte[]{1,2,3}, 0,1), | |
Arguments.of("Length 0 for empty array", 0, new byte[]{1,2,3}, 1,1), | |
Arguments.of("Length 1 for slice with distance between from and to", 1, new byte[]{1,2,3}, 1, 2), | |
Arguments.of("Length 2 for slice with distance between from and to", 2, new byte[]{1,2,3}, 0, 2), | |
Arguments.of("Length 3 for slice with distance between from and to", 3, new byte[]{1,2,3}, 0, 3) | |
); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the suggestion. I prefer the original version more. It seems to be kinda minimalistic and more readable
assertThrows(IndexOutOfBoundsException.class, () -> Bytes.of(new byte[]{}).slice(0, 0).byteAt(0)); | ||
assertThrows(IndexOutOfBoundsException.class, () -> Bytes.of(new byte[]{1}).slice(0, 1).byteAt(1)); | ||
assertThrows(IndexOutOfBoundsException.class, () -> Bytes.of(new byte[]{1}).slice(0, 1).byteAt(-1)); | ||
assertThrows(IndexOutOfBoundsException.class, () -> Bytes.of(new byte[]{1,2,3}).slice(1, 2).byteAt(1)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion:
Break this test in two, one to test the Exceptions and other to validate the proper functionality of the method. You could also get use of the parameterized test here. HAving a single assertion with different inputs.
|
||
Bytes bytes = Bytes.of(finalArray); | ||
|
||
assertEquals(64, String.format("%s", bytes).length()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question:
Why the size is doubled? 😅
Because it become an object and it occupies more space? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here we're limiting printing of byte sequences to 32 bytes. In stringular / hex form it'd be 64 chars
@@ -74,4 +113,44 @@ void testEmptySlice() { | |||
assertEquals(0, actualResult.length()); | |||
assertArrayEquals(expectedResult, actualResult.copyArray()); | |||
} | |||
|
|||
private static void checkArraycopy(Functions.Action5<Object, Integer, Object, Integer, Integer> fun) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion:
Although all variables are used here, I would suggest create local variables to hold the value of the arrays. So we can have a better understanding just by reading the code withoug have to interpret it. For example:
...
byte[] anotherDestArray= new byte[5];
assertArrayEquals(anotherDestArray, dest); // yet unmodified
fun.apply(origin, 0, dest, 0, 5);
byte[] expectedDestAfterCopy = new byte[]{1,2,3,4,5};
assertArrayEquals(expectedDestAfterCopy, dest);
anotherDestArray = new byte[5];
fun.apply(origin, 1, anotherDestArray, 1, 3);
byte[] newExpectedArray = new byte[]{0,2,3,4,0};
assertArrayEquals(newExpectedArray, anotherDestArray);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
b35867c
to
b0bae39
Compare
pipeline:run |
rskj-core/src/test/java/co/rsk/core/types/bytes/BytesSliceTest.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Quality Gate passedIssues Measures |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
pipeline:run |
7ae40e4
to
f243260
Compare
if (newLength < 0) { | ||
throw new IllegalArgumentException(from + " > " + to); | ||
} | ||
byte[] copy = new byte[newLength]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should copy
array length also be Math.min(length() - from, newLength)
? in other case it may contain zero bytes at the end which are not present in original array
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
… Arrays.copyOfRange; add more tests
….java Co-authored-by: Nazaret García Revetria <nazaret@iovlabs.org>
f243260
to
39f9c56
Compare
Quality Gate passedIssues Measures |
Description
Motivation and Context
How Has This Been Tested?
Types of changes
Checklist: