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

EVM optimizations #2405

Merged
merged 3 commits into from
Dec 2, 2022
Merged
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
11 changes: 7 additions & 4 deletions packages/evm/src/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const ceil = (value: number, ceiling: number): number => {
}
}

const CONTAINER_SIZE = 8192

/**
* Memory implements a simple memory model
* for the ethereum virtual machine.
Expand All @@ -30,7 +32,10 @@ export class Memory {
const newSize = ceil(offset + size, 32)
const sizeDiff = newSize - this._store.length
if (sizeDiff > 0) {
this._store = Buffer.concat([this._store, Buffer.alloc(sizeDiff)])
this._store = Buffer.concat([
this._store,
Buffer.alloc(Math.ceil(sizeDiff / CONTAINER_SIZE) * CONTAINER_SIZE),
])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, just did a quick search ._store.length for the EVM to see if there are any external accesses to memory length where we would have side effects, but this seems not to be the case (apart from some memory-internal tests which can eventually be adopted if needed). 🙂

So this seems like a really clever solution to reduce the buffer allocations drastically along memory extensions! 👍

(still am a bit cautious and wonder though if this might have some side effect(s) somewhere? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that we should very carefully examine this. (I also did the same search as you did since I was afraid that there indeed might be external reads to it)

}
}

Expand All @@ -50,9 +55,7 @@ export class Memory {
if (value.length !== size) throw new Error('Invalid value size')
if (offset + size > this._store.length) throw new Error('Value exceeds memory capacity')

for (let i = 0; i < size; i++) {
this._store[offset + i] = value[i]
}
value.copy(this._store, offset)
}

/**
Expand Down