Skip to content

Commit

Permalink
optimize EVM memory extend
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed May 30, 2023
1 parent cd78458 commit 82dc904
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
10 changes: 3 additions & 7 deletions nimbus/evm/memory.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ proc len*(memory: Memory): int =
proc extend*(memory: var Memory; startPos: Natural; size: Natural) =
if size == 0:
return
var newSize = ceil32(startPos + size)
let newSize = ceil32(startPos + size)
if newSize <= len(memory):
return
var sizeToExtend = newSize - len(memory)
memory.bytes = memory.bytes.concat(repeat(0.byte, sizeToExtend))
memory.bytes.setLen(newSize)

proc newMemory*(size: Natural): Memory =
result = newMemory()
Expand All @@ -51,9 +50,6 @@ proc write*(memory: var Memory, startPos: Natural, value: openArray[byte]) =
let size = value.len
if size == 0:
return
validateLte(startPos + size, memory.len)
if memory.len < startPos + size:
memory.bytes = memory.bytes.concat(repeat(0.byte, memory.len - (startPos + size))) # TODO: better logarithmic scaling?

validateLte(startPos + size, memory.len)
for z, b in value:
memory.bytes[z + startPos] = b
5 changes: 5 additions & 0 deletions nimbus/evm/validation.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ proc validateLt*(value: UInt256 | int, maximum: int, title: string = "Value") =
raise newException(ValidationError,
&"{title} {value} is not less than {maximum}")

proc validateLte*(value: int, maximum: int, title: string = "Value") =
if value > maximum:
raise newException(ValidationError,
&"{title} {value} is not less or equal to {maximum}")

proc validateStackItem*(value: string) =
if value.len > 32:
raise newException(ValidationError,
Expand Down

0 comments on commit 82dc904

Please sign in to comment.