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

optimize EVM memory extend #1592

Merged
merged 1 commit into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading