Skip to content

Optimize memory representation and operations #707

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

Merged
merged 8 commits into from
Apr 29, 2025
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
shrinking it.
- Buffers are now handled more lazily when inspecting a model, which avoids some
unnecesary internal errors.
- EVM memory is now grown on demand using a 2x factor, to avoid repeated smaller
increases which hurt concrete execution performance due to their linear cost.
- The concrete MCOPY implementation has been optimized to avoid freezing the whole
EVM memory.

## [0.54.2] - 2024-12-12

Expand Down
20 changes: 14 additions & 6 deletions src/EVM.hs
Original file line number Diff line number Diff line change
Expand Up @@ -674,9 +674,9 @@ exec1 conf = do
mcopy sz srcOff dstOff = do
m <- gets (.state.memory)
case m of
ConcreteMemory mem -> do
buf <- freezeMemory mem
copyBytesToMemory buf sz srcOff dstOff
ConcreteMemory _ -> do
buf <- readMemory srcOff sz
copyBytesToMemory buf sz (Lit 0) dstOff
SymbolicMemory mem -> do
assign (#state % #memory) (SymbolicMemory $ copySlice srcOff dstOff sz mem mem)

Expand Down Expand Up @@ -2958,10 +2958,18 @@ writeMemory memory offset buf = do
mapM_ (uncurry (VUnboxed.Mutable.write memory'))
(zip [offset..] (BS.unpack buf))
where
expandMemory targetSize = do
let toAlloc = targetSize - VUnboxed.Mutable.length memory
expandMemory requiredSize = do
let currentSize = VUnboxed.Mutable.length memory
let toAlloc = requiredSize - currentSize
if toAlloc > 0 then do
memory' <- VUnboxed.Mutable.grow memory toAlloc
-- As grow does a larger *copy* of the vector on a new place,
-- we double the vector size to avoid the performance impact
-- that would happen with repeated small expansion operations.
let growthFactor = 2
let targetSize = requiredSize * growthFactor
-- Always grow at least 8k
let toGrow = max 8192 $ targetSize - currentSize
memory' <- VUnboxed.Mutable.grow memory toGrow
assign (#state % #memory) (ConcreteMemory memory')
pure memory'
else
Expand Down
Loading