optimize EVM memory extend

This commit is contained in:
jangko 2023-05-30 23:15:04 +07:00
parent 2fc349feb9
commit 742759e607
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
2 changed files with 8 additions and 7 deletions

View File

@ -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()
@ -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

View File

@ -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,