-
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 mem reading in Memory #2667
Draft
Vovchyk
wants to merge
5
commits into
master
Choose a base branch
from
vovchyk/mem-impv
base: master
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.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e5cd007
perf: improve mem reading in Memory
Vovchyk bfbd22a
perf: fix sonar complaints
Vovchyk 71f6ea7
perf: add jmh test; add java doc
Vovchyk e1d6fd4
perf: add overflow check
Vovchyk ff944f2
perf: add write versioning check; add tests
Vovchyk 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 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
rskj-core/src/jmh/java/co/rsk/jmh/utils/BenchmarkByteUtil.java
This file contains 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 @@ | ||
/* | ||
* This file is part of RskJ | ||
* Copyright (C) 2024 RSK Labs Ltd. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package co.rsk.jmh.utils; | ||
|
||
import co.rsk.core.types.bytes.Bytes; | ||
import co.rsk.jmh.utils.plan.DataPlan; | ||
import org.openjdk.jmh.annotations.*; | ||
import org.openjdk.jmh.runner.Runner; | ||
import org.openjdk.jmh.runner.RunnerException; | ||
import org.openjdk.jmh.runner.options.Options; | ||
import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@BenchmarkMode({Mode.Throughput}) | ||
@Warmup(iterations = 1, time = 5 /* secs */) | ||
@Measurement(iterations = 3, time = 5 /* secs */) | ||
@OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
public class BenchmarkByteUtil { | ||
|
||
@Benchmark | ||
public void toHexString_Bouncycastle(DataPlan plan) { | ||
byte[] data = plan.getData(); | ||
int off = plan.getNextRand(data.length); | ||
int len = plan.getNextRand(data.length - off); | ||
org.bouncycastle.util.encoders.Hex.toHexString(data, off, len); | ||
} | ||
|
||
@Benchmark | ||
public void toHexString_V2(DataPlan plan) { | ||
Bytes bytes = plan.getBytes(); | ||
int bytesLen = bytes.length(); | ||
int off = plan.getNextRand(bytesLen); | ||
int len = plan.getNextRand(bytesLen - off); | ||
bytes.toHexString(off, len); | ||
} | ||
|
||
public static void main(String[] args) throws RunnerException { | ||
Options opt = new OptionsBuilder() | ||
.include(BenchmarkByteUtil.class.getName()) | ||
.forks(2) | ||
.build(); | ||
|
||
new Runner(opt).run(); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
rskj-core/src/jmh/java/co/rsk/jmh/utils/plan/DataPlan.java
This file contains 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,56 @@ | ||
/* | ||
* This file is part of RskJ | ||
* Copyright (C) 2024 RSK Labs Ltd. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package co.rsk.jmh.utils.plan; | ||
|
||
import co.rsk.core.types.bytes.Bytes; | ||
import org.openjdk.jmh.annotations.Level; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
|
||
import java.util.Random; | ||
|
||
@State(Scope.Benchmark) | ||
public class DataPlan { | ||
|
||
private final byte[] data = new byte[1024]; | ||
|
||
private Bytes bytes; | ||
|
||
private Random random; | ||
|
||
@Setup(Level.Trial) | ||
public void doSetup() { | ||
random = new Random(111); | ||
random.nextBytes(data); | ||
bytes = Bytes.of(data); | ||
} | ||
|
||
public byte[] getData() { | ||
return data; | ||
} | ||
|
||
public Bytes getBytes() { | ||
return bytes; | ||
} | ||
|
||
public int getNextRand(int bound) { | ||
return random.nextInt(bound); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
rskj-core/src/main/java/co/rsk/core/types/bytes/BoundaryUtils.java
This file contains 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,42 @@ | ||
/* | ||
* This file is part of RskJ | ||
* Copyright (C) 2024 RSK Labs Ltd. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package co.rsk.core.types.bytes; | ||
|
||
public class BoundaryUtils { | ||
|
||
private BoundaryUtils() { /* hidden */ } | ||
|
||
public static void checkArraycopyParams(int srcLength, int srcPos, byte[] dest, int destPos, int length) { | ||
if (length < 0) { | ||
throw new IndexOutOfBoundsException("invalid 'length': " + length); | ||
} | ||
if (srcPos < 0 || Long.sum(srcPos, length) > srcLength) { | ||
throw new IndexOutOfBoundsException("invalid 'srcPos' and/or 'length': [" + srcPos + ";" + length + ")"); | ||
} | ||
if (destPos < 0 || Long.sum(destPos, length) > dest.length) { | ||
throw new IndexOutOfBoundsException("invalid 'destPos' and/or 'length': [" + destPos + ";" + length + ")"); | ||
} | ||
} | ||
|
||
public static void checkArrayIndexParam(int srcLength, int index) { | ||
if (index < 0 || index >= srcLength) { | ||
throw new IndexOutOfBoundsException("invalid index: " + index); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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
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.
What do you think about convert this class to an abstract class? I think it could give us various advantages:
BoundaryUtils.checkArrayIndexParam(length(), index)
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.
I'd rather use
interface
here. I guess it's quite similar toCharSequence
interface that's being used for chars without concrete implementation ofequals
andhashCode