Skip to content
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

Merged
merged 5 commits into from
Oct 4, 2024
Merged

Conversation

Vovchyk
Copy link
Contributor

@Vovchyk Vovchyk commented Jul 26, 2024

Description

Motivation and Context

How Has This Been Tested?

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • Tests for the changes have been added (for bug fixes / features)
  • Requires Activation Code (Hard Fork)
  • Other information:

@Vovchyk Vovchyk marked this pull request as ready for review August 5, 2024 13:45
asoto-iov
asoto-iov previously approved these changes Aug 7, 2024
fmacleal
fmacleal previously approved these changes Aug 7, 2024
Copy link
Contributor

@fmacleal fmacleal left a 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.

Comment on lines +31 to +40
@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());
}
Copy link
Contributor

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.

Suggested change
@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)
);
}

Copy link
Contributor Author

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

Comment on lines 44 to 47
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));
Copy link
Contributor

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());
Copy link
Contributor

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? 🤔

Copy link
Contributor Author

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) {
Copy link
Contributor

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);

@Vovchyk Vovchyk dismissed stale reviews from fmacleal and asoto-iov via 2cb3dc6 August 9, 2024 12:33
fmacleal
fmacleal previously approved these changes Aug 14, 2024
Copy link
Contributor

@fmacleal fmacleal left a comment

Choose a reason for hiding this comment

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

LGTM

asoto-iov
asoto-iov previously approved these changes Aug 15, 2024
@fmacleal fmacleal dismissed stale reviews from asoto-iov and themself via b35867c August 20, 2024 16:02
@fmacleal
Copy link
Contributor

pipeline:run

Copy link
Contributor

@nagarev nagarev left a comment

Choose a reason for hiding this comment

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

LGTM :shipit:

Copy link

sonarcloud bot commented Aug 21, 2024

Copy link
Contributor

@fmacleal fmacleal left a comment

Choose a reason for hiding this comment

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

LGTM

@fmacleal
Copy link
Contributor

pipeline:run

if (newLength < 0) {
throw new IllegalArgumentException(from + " > " + to);
}
byte[] copy = new byte[newLength];
Copy link

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@VaWheel , the idea here was to mimic the behaviour of Arrays#copyOfRange(Object[], int, int) (pls see this and this javadoc's)

Copy link

sonarcloud bot commented Oct 4, 2024

@Vovchyk Vovchyk merged commit 4084daf into master Oct 4, 2024
11 checks passed
@Vovchyk Vovchyk deleted the vovchyk/native-cont-impv branch October 4, 2024 11:46
@aeidelman aeidelman added this to the Arrowhead 6.4.0 milestone Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants