Skip to content

Commit

Permalink
Merge pull request #2365 from rsksmart/improve_performance_codesize_c…
Browse files Browse the repository at this point in the history
…odecopy

Update getCode so it does not make a copy of the array
  • Loading branch information
Vovchyk authored May 29, 2024
2 parents 8959ab2 + 64d1f63 commit 07e9c13
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
11 changes: 6 additions & 5 deletions rskj-core/src/main/java/org/ethereum/vm/VM.java
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ protected void doCODESIZE() {
// EXECUTION PHASE
DataWord codeLength;
if (op == OpCode.CODESIZE) {
codeLength = DataWord.valueOf(program.getCode().length); // during initialization it will return the initialization code size
codeLength = DataWord.valueOf(program.getCodeLength()); // during initialization it will return the initialization code size
} else {
DataWord address = program.stackPop();
codeLength = DataWord.valueOf(program.getCodeLengthAt(address));
Expand Down Expand Up @@ -879,13 +879,14 @@ protected void doCODECOPY() {
int lengthData = lengthDataDW.intValueSafe(); // amount of bytes to copy

int sizeToBeCopied;
if ((long) codeOffset + lengthData > fullCode.length) {
int fullCodeLength = fullCode.length;
if ((long) codeOffset + lengthData > fullCodeLength) {
// if user wants to read more info from code what actual code has then..
// if all code that users wants lies after code has ended..
if (codeOffset >=fullCode.length) {
if (codeOffset >= fullCodeLength) {
sizeToBeCopied=0; // do not copy anything
} else {
sizeToBeCopied = fullCode.length - codeOffset; // copy only the remaining
sizeToBeCopied = fullCodeLength - codeOffset; // copy only the remaining
}

} else
Expand All @@ -898,7 +899,7 @@ protected void doCODECOPY() {
// enough space to contain filling also.
byte[] codeCopy = new byte[lengthData];

if (codeOffset < fullCode.length) {
if (codeOffset < fullCodeLength) {
System.arraycopy(fullCode, codeOffset, codeCopy, 0, sizeToBeCopied);
}

Expand Down
6 changes: 5 additions & 1 deletion rskj-core/src/main/java/org/ethereum/vm/program/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,11 @@ private RskAddress getOwnerRskAddress() {
}

public byte[] getCode() {
return Arrays.copyOf(ops, ops.length);
return ops;
}

public int getCodeLength() {
return ops.length;
}

public Keccak256 getCodeHashAt(RskAddress addr, boolean standard) {
Expand Down

0 comments on commit 07e9c13

Please sign in to comment.