forked from tmio/tuweni
-
Notifications
You must be signed in to change notification settings - Fork 13
Flatten bytes class hierarchy #40
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
Open
lu-pinto
wants to merge
3
commits into
Consensys:main
Choose a base branch
from
lu-pinto:flatten-Bytes-class-hierarchy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
bytes/src/jmh/java/org/benchmark/BytesMegamorphicBenchmarkV1.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright The Tuweni Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package org.benchmark; | ||
|
|
||
| import org.apache.tuweni.bytes.Bytes; | ||
|
|
||
| import java.util.Random; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| import org.openjdk.jmh.annotations.Mode; | ||
| import org.openjdk.jmh.annotations.OperationsPerInvocation; | ||
| import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
| import org.openjdk.jmh.annotations.Param; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
|
|
||
| @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) | ||
| @Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS) | ||
| @BenchmarkMode(value = Mode.AverageTime) | ||
| @State(Scope.Thread) | ||
| @OutputTimeUnit(value = TimeUnit.NANOSECONDS) | ||
| public class BytesMegamorphicBenchmarkV1 { | ||
| private static final int N = 4; | ||
| private static final int FACTOR = 1_000; | ||
| private static final Random RANDOM = new Random(23L); | ||
| Bytes[] bytesV1; | ||
|
|
||
| @Param({"mono", "mega"}) | ||
| private String mode; | ||
|
|
||
| @Setup | ||
| public void setup() { | ||
| bytesV1 = new Bytes[N * FACTOR]; | ||
| for (int i = 0; i < N * FACTOR; i += N) { | ||
| bytesV1[i] = Bytes.wrap(getBytes(32)); | ||
| bytesV1[i + 1] = "mega".equals(mode) ? Bytes.wrap(getBytes(48)) : Bytes.wrap(getBytes(32)); | ||
| bytesV1[i + 2] = | ||
| "mega".equals(mode) ? Bytes.repeat((byte) 0x09, 16) : Bytes.wrap(getBytes(32)); | ||
| bytesV1[i + 3] = | ||
| "mega".equals(mode) ? Bytes.wrap(bytesV1[i], bytesV1[i + 1]) : Bytes.wrap(getBytes(32)); | ||
| } | ||
| } | ||
|
|
||
| private static byte[] getBytes(final int size) { | ||
| byte[] b = new byte[size]; | ||
| RANDOM.nextBytes(b); | ||
| return b; | ||
| } | ||
|
|
||
| @Benchmark | ||
| @OperationsPerInvocation(N * FACTOR) | ||
| public void test() { | ||
| for (Bytes b : bytesV1) { | ||
| b.slice(1); | ||
| } | ||
| } | ||
| } |
62 changes: 62 additions & 0 deletions
62
bytes/src/jmh/java/org/benchmark/BytesMegamorphicBenchmarkV2.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright The Tuweni Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package org.benchmark; | ||
|
|
||
| import org.apache.tuweni.v2.bytes.Bytes; | ||
|
|
||
| import java.util.Random; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| import org.openjdk.jmh.annotations.Mode; | ||
| import org.openjdk.jmh.annotations.OperationsPerInvocation; | ||
| import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
| import org.openjdk.jmh.annotations.Param; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
|
|
||
| @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) | ||
| @Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS) | ||
| @BenchmarkMode(value = Mode.AverageTime) | ||
| @State(Scope.Thread) | ||
| @OutputTimeUnit(value = TimeUnit.NANOSECONDS) | ||
| public class BytesMegamorphicBenchmarkV2 { | ||
| private static final int N = 4; | ||
| private static final int FACTOR = 1_000; | ||
| private static final Random RANDOM = new Random(23L); | ||
| Bytes[] bytesV2; | ||
|
|
||
| @Param({"mono", "mega"}) | ||
| private String mode; | ||
|
|
||
| @Setup | ||
| public void setup() { | ||
| bytesV2 = new Bytes[N * FACTOR]; | ||
| for (int i = 0; i < N * FACTOR; i += N) { | ||
| bytesV2[i] = Bytes.wrap(getBytes(32)); | ||
| bytesV2[i + 1] = "mega".equals(mode) ? Bytes.wrap(getBytes(48)) : Bytes.wrap(getBytes(32)); | ||
| bytesV2[i + 2] = | ||
| "mega".equals(mode) ? Bytes.repeat((byte) 0x09, 16) : Bytes.wrap(getBytes(32)); | ||
| bytesV2[i + 3] = | ||
| "mega".equals(mode) ? Bytes.wrap(bytesV2[i], bytesV2[i + 1]) : Bytes.wrap(getBytes(32)); | ||
| } | ||
| } | ||
|
|
||
| private static byte[] getBytes(final int size) { | ||
| byte[] b = new byte[size]; | ||
| RANDOM.nextBytes(b); | ||
| return b; | ||
| } | ||
|
|
||
| @Benchmark | ||
| @OperationsPerInvocation(N * FACTOR) | ||
| public void test() { | ||
| for (Bytes b : bytesV2) { | ||
| b.slice(1); | ||
| } | ||
| } | ||
| } |
143 changes: 143 additions & 0 deletions
143
bytes/src/main/java/org/apache/tuweni/v2/bytes/ArrayWrappingBytes.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| // Copyright The Tuweni Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package org.apache.tuweni.v2.bytes; | ||
|
|
||
| import static org.apache.tuweni.v2.bytes.Utils.checkArgument; | ||
| import static org.apache.tuweni.v2.bytes.Utils.checkElementIndex; | ||
| import static org.apache.tuweni.v2.bytes.Utils.checkLength; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.security.MessageDigest; | ||
| import java.util.Arrays; | ||
|
|
||
| import io.vertx.core.buffer.Buffer; | ||
|
|
||
| class ArrayWrappingBytes extends Bytes { | ||
|
|
||
| protected byte[] bytes; | ||
| protected final int offset; | ||
|
|
||
| ArrayWrappingBytes(byte[] bytes) { | ||
| this(bytes, 0, bytes.length); | ||
| } | ||
|
|
||
| ArrayWrappingBytes(byte[] bytes, int offset, int length) { | ||
| super(length); | ||
| this.bytes = bytes; | ||
| this.offset = offset; | ||
| } | ||
|
|
||
| @Override | ||
| public byte get(int i) { | ||
| // Check bounds because while the array access would throw, the error message would be confusing | ||
| // for the caller. | ||
| checkElementIndex(offset + i, bytes.length); | ||
| checkElementIndex(i, size()); | ||
| return bytes[offset + i]; | ||
| } | ||
|
|
||
| @Override | ||
| public Bytes slice(int i, int length) { | ||
| if (i == 0 && length == size()) { | ||
| return this; | ||
| } | ||
|
|
||
| if (length == 0) { | ||
| return EMPTY; | ||
| } | ||
|
|
||
| checkArgument(length > 0, "Invalid negative length"); | ||
| if (bytes.length > 0) { | ||
| checkElementIndex(offset + i, bytes.length); | ||
| } | ||
| checkLength(bytes.length, offset + i, length); | ||
|
|
||
| return new ArrayWrappingBytes(bytes, offset + i, length); | ||
| } | ||
|
|
||
| @Override | ||
| public int commonPrefixLength(Bytes other) { | ||
| if (!(other instanceof ArrayWrappingBytes o)) { | ||
| return super.commonPrefixLength(other); | ||
| } | ||
| int i = 0; | ||
| while (i < size() && i < o.size() && bytes[offset + i] == o.bytes[o.offset + i]) { | ||
| i++; | ||
| } | ||
| return i; | ||
| } | ||
|
|
||
| @Override | ||
| public void update(MessageDigest digest) { | ||
| digest.update(bytes, offset, size()); | ||
| } | ||
|
|
||
| @Override | ||
| public void appendTo(ByteBuffer byteBuffer) { | ||
| byteBuffer.put(bytes, offset, size()); | ||
| } | ||
|
|
||
| @Override | ||
| public MutableBytes mutableCopy() { | ||
| return MutableBytes.fromArray(bytes, offset, size()); | ||
| } | ||
|
|
||
| @Override | ||
| public void appendTo(Buffer buffer) { | ||
| buffer.appendBytes(bytes, offset, size()); | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] toArrayUnsafe() { | ||
| if (offset == 0 && size() == bytes.length) { | ||
| return bytes; | ||
| } | ||
| return Arrays.copyOfRange(bytes, offset, offset + size()); | ||
| } | ||
|
|
||
| @Override | ||
| protected void and(byte[] bytesArray, int offset, int length) { | ||
| Utils.and(this.bytes, this.offset, bytesArray, offset, length); | ||
| } | ||
|
|
||
| @Override | ||
| protected void or(byte[] bytesArray, int offset, int length) { | ||
| Utils.or(this.bytes, this.offset, bytesArray, offset, length); | ||
| } | ||
|
|
||
| @Override | ||
| protected void xor(byte[] bytesArray, int offset, int length) { | ||
| Utils.xor(this.bytes, this.offset, bytesArray, offset, length); | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sens to add shiftLeft and shiftRight here ?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as #40 (comment) |
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (obj == this) { | ||
| return true; | ||
| } | ||
| if (!(obj instanceof Bytes other)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (this.size() != other.size()) { | ||
| return false; | ||
| } | ||
|
|
||
| for (int i = 0; i < size(); i++) { | ||
| if (bytes[i + offset] != other.get(i)) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| protected int computeHashcode() { | ||
| int result = 1; | ||
| for (int i = 0; i < size(); i++) { | ||
| result = 31 * result + bytes[i + offset]; | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
lu-pinto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
👍
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.
It would be nice to have jmh benchmarks on and other binary operations below. I think it will show a pretty good improvement. This is not a blocking comment, but a nice to have.
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.
let's do that in another PR