-
Notifications
You must be signed in to change notification settings - Fork 773
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: Avoid memory.read() Memory Copy #2573
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ea144a2
EVM: added avoidCopy parameter to memory.read() function, first test …
holgerd77 c5e98f4
EVM: Add direct memory read to all calling opcodes
holgerd77 6f5e367
EVM: Copy over memory on IDENTITY precompile
holgerd77 055298d
EVM: remove length checks and return buffer 0-filling in Memory.read(…
holgerd77 e0dce0c
Some optimizations
holgerd77 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think what we can do here is:
slice
of the_store
loaded.length
does not equal the size, expand the current internal_store
with extra zeros. (Use theextend
method to allocate in 8kb chunks)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A that's a good idea, I will try that (have got 3 work hours from now on). I would nevertheless want to keep this in this limited scope (so "free" the memory one after one or - rather - over time eventually), otherwise this will get too hairy and uncontrollable.
In the current scope this feels already pretty good though, will also test on some couple of hundred tx loaded mainnet blocks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Direct update: ok, tested with
npm run client:start -- --discDns=false --discV4=false --maxPeers=0 --executeBlocks=1400000-1401000
on a couple of thousand txs, that all went well, only one fork (homestead) but already a good sign. 🙂There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. There is actually already a
this.extend(offset, size)
call at the beginning of the method.I don't think it would have any effect if I just delete the zero filling conditional (not seeing how this can ever evaluate to true in the current setup of the method).
Will do that.
Or maybe, from a procedural point of view: will first look into the current test failures, and then do it. 😋
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah right, sorry, there is indeed the
extend
method above.Somewhat unrelated, but each time we
extend
and it creates a 8kb memory, weconcat
the Buffer which, I think (https://nodejs.org/api/buffer.html#static-method-bufferconcatlist-totallength) copies the previous Buffer as well, which we also might not want. I am not sure how other clients allocate these Buffers, you could split them up in chunks of whatever size without copying them, but then if you want to read over 2+ containers the read-logic gets a big complex (and you have to copy them again). (Think of memory in this "optimized" (?) case as a linked list?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, that's interesting and might be also something worth addressing. Is there any spec or so reason why we start memory at 0? Is this given by the protocol? Otherwise we could at least safe this first memory copy over step by directly initializing with 96 bytes?
Just ran some mainnet blocks with
console.log(
Memory extended by ${sizeDiff} to ${newSize})
injected in theextend()
method (only when extension is taking place), and going once to 96 is the most common extension operation. 🤔(I have the impression I am in the range of some attack blocks, I guess therefore the occasional repeated 8768 extensions, no confirmation though on this and at the end a side question)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This 96 is due to solidity, it initializes the "free memory pointer" at slot 0x40 (64) which will thus write 32 bytes at offset 64 and thus extend to 96 bytes. I am not sure if I am ok with directly initializing it to 96 bytes, since it is super likely that more memory writes are happening, and if solidity changes their initial memory structure we have this artifact laying around.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will experiment with it a bit.
I tested 1024, that seemed like a reasonable default after some tests and looking into some numbers. The performance tests do not gain by that though, so doesn't seem like such an imminent topic to solve. Would like to do some mainnet block tests as well though.