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

Optimization: Small changes on many functions V3 (wip) #217

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

JulianVentura
Copy link
Contributor

@JulianVentura JulianVentura commented Aug 28, 2024

Description

This PR improves vm performance by making small changes to many functions.

Results

The results vary across different benchmark suites, showing changes from no difference to a 12% enhancement. When we aggregate the average execution times of all benchmarks, we observe an overall improvement of approximately 3%.

Dependencies

Changes were made to default_aa_code_hash and evm_interpreter_code_hash variable types under execution.rs which require a bump on underlying zksync-era repository dependency. Because of that, this PR should only be merged together with era_vm_small_changes_v2

Copy link

github-actions bot commented Aug 28, 2024

Ergs comparison results:

╔═╡ Size (-%) ╞════════════════╡ All M3B3 ╞═╗
║ Mean                                0.000 ║
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                                 NaN ║
╠═╡ Cycles (-%) ╞══════════════╡ All M3B3 ╞═╣
║ Mean                              100.000 ║
║ Best                              100.000 ║
║ Worst                               0.000 ║
║ Total                             100.000 ║
╠═╡ Ergs (-%) ╞════════════════╡ All M3B3 ╞═╣
║ Mean                              100.000 ║
║ Best                              100.000 ║
║ Worst                               0.000 ║
║ Total                             100.000 ║
╚═══════════════════════════════════════════╝

╔═╡ Size (-%) ╞════════════════╡ All MzB3 ╞═╗
║ Mean                                0.000 ║
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                                 NaN ║
╠═╡ Cycles (-%) ╞══════════════╡ All MzB3 ╞═╣
║ Mean                                0.000 ║
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                                 NaN ║
╠═╡ Ergs (-%) ╞════════════════╡ All MzB3 ╞═╣
║ Mean                                0.000 ║
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                                 NaN ║
╚═══════════════════════════════════════════╝

╔═╡ Size (-%) ╞═════╡ EVMInterpreter M3B3 ╞═╗
║ Mean                                0.000 ║
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                                 NaN ║
╠═╡ Cycles (-%) ╞═══╡ EVMInterpreter M3B3 ╞═╣
║ Mean                              100.000 ║
║ Best                              100.000 ║
║ Worst                               0.000 ║
║ Total                             100.000 ║
╠═╡ Ergs (-%) ╞═════╡ EVMInterpreter M3B3 ╞═╣
║ Mean                              100.000 ║
║ Best                              100.000 ║
║ Worst                               0.000 ║
║ Total                             100.000 ║
╠═╡ Ergs/gas ╞══════╡ EVMInterpreter M3B3 ╞═╣
║ ADD                                 0.000 ║
║ MUL                                 0.000 ║
║ SUB                                 0.000 ║
║ DIV                                 0.000 ║
║ SDIV                                0.000 ║
║ MOD                                 0.000 ║
║ SMOD                                0.000 ║
║ ADDMOD                              0.000 ║
║ MULMOD                              0.000 ║
║ EXP                                 0.000 ║
║ SIGNEXTEND                          0.000 ║
║ LT                                  0.000 ║
║ GT                                  0.000 ║
║ SLT                                 0.000 ║
║ SGT                                 0.000 ║
║ EQ                                  0.000 ║
║ ISZERO                              0.000 ║
║ AND                                 0.000 ║
║ OR                                  0.000 ║
║ XOR                                 0.000 ║
║ NOT                                 0.000 ║
║ BYTE                                0.000 ║
║ SHL                                 0.000 ║
║ SHR                                 0.000 ║
║ SAR                                 0.000 ║
║ SGT                                 0.000 ║
║ SHA3                                0.000 ║
║ ADDRESS                             0.000 ║
║ BALANCE                             0.000 ║
║ ORIGIN                              0.000 ║
║ CALLER                              0.000 ║
║ CALLVALUE                           0.000 ║
║ CALLDATALOAD                        0.000 ║
║ CALLDATASIZE                        0.000 ║
║ CALLDATACOPY                        0.000 ║
║ CODESIZE                            0.000 ║
║ CODECOPY                            0.000 ║
║ GASPRICE                            0.000 ║
║ EXTCODESIZE                         0.000 ║
║ EXTCODECOPY                         0.000 ║
║ RETURNDATASIZE                      0.000 ║
║ RETURNDATACOPY                      0.000 ║
║ EXTCODEHASH                         0.000 ║
║ BLOCKHASH                           0.000 ║
║ COINBASE                            0.000 ║
║ TIMESTAMP                           0.000 ║
║ NUMBER                              0.000 ║
║ PREVRANDAO                          0.000 ║
║ GASLIMIT                            0.000 ║
║ CHAINID                             0.000 ║
║ SELFBALANCE                         0.000 ║
║ BASEFEE                             0.000 ║
║ POP                                 0.000 ║
║ MLOAD                               0.000 ║
║ MSTORE                              0.000 ║
║ MSTORE8                             0.000 ║
║ SLOAD                               0.000 ║
║ SSTORE                              0.000 ║
║ JUMP                                0.000 ║
║ JUMPI                               0.000 ║
║ PC                                  0.000 ║
║ MSIZE                               0.000 ║
║ GAS                                 0.000 ║
║ JUMPDEST                            0.000 ║
║ PUSH0                               0.000 ║
║ PUSH1                               0.000 ║
║ PUSH2                               0.000 ║
║ PUSH4                               0.000 ║
║ PUSH5                               0.000 ║
║ PUSH6                               0.000 ║
║ PUSH7                               0.000 ║
║ PUSH8                               0.000 ║
║ PUSH9                               0.000 ║
║ PUSH10                              0.000 ║
║ PUSH11                              0.000 ║
║ PUSH12                              0.000 ║
║ PUSH13                              0.000 ║
║ PUSH14                              0.000 ║
║ PUSH15                              0.000 ║
║ PUSH16                              0.000 ║
║ PUSH17                              0.000 ║
║ PUSH18                              0.000 ║
║ PUSH19                              0.000 ║
║ PUSH20                              0.000 ║
║ PUSH21                              0.000 ║
║ PUSH22                              0.000 ║
║ PUSH23                              0.000 ║
║ PUSH24                              0.000 ║
║ PUSH25                              0.000 ║
║ PUSH26                              0.000 ║
║ PUSH27                              0.000 ║
║ PUSH28                              0.000 ║
║ PUSH29                              0.000 ║
║ PUSH30                              0.000 ║
║ PUSH31                              0.000 ║
║ PUSH32                              0.000 ║
║ DUP1                                0.000 ║
║ DUP2                                0.000 ║
║ DUP3                                0.000 ║
║ DUP4                                0.000 ║
║ DUP5                                0.000 ║
║ DUP6                                0.000 ║
║ DUP7                                0.000 ║
║ DUP8                                0.000 ║
║ DUP9                                0.000 ║
║ DUP10                               0.000 ║
║ DUP11                               0.000 ║
║ DUP12                               0.000 ║
║ DUP13                               0.000 ║
║ DUP14                               0.000 ║
║ DUP15                               0.000 ║
║ DUP16                               0.000 ║
║ SWAP1                               0.000 ║
║ SWAP2                               0.000 ║
║ SWAP3                               0.000 ║
║ SWAP4                               0.000 ║
║ SWAP5                               0.000 ║
║ SWAP6                               0.000 ║
║ SWAP7                               0.000 ║
║ SWAP8                               0.000 ║
║ SWAP9                               0.000 ║
║ SWAP10                              0.000 ║
║ SWAP11                              0.000 ║
║ SWAP12                              0.000 ║
║ SWAP13                              0.000 ║
║ SWAP14                              0.000 ║
║ SWAP15                              0.000 ║
║ SWAP16                              0.000 ║
║ CALL                                0.000 ║
║ STATICCALL                          0.000 ║
║ DELEGATECALL                        0.000 ║
║ CREATE                              0.000 ║
║ CREATE2                             0.000 ║
║ RETURN                              0.000 ║
║ REVERT                              0.000 ║
╠═╡ Ergs/gas (-%) ╞═╡ EVMInterpreter M3B3 ╞═╣
║ ADD                               100.000 ║
║ MUL                               100.000 ║
║ SUB                               100.000 ║
║ DIV                               100.000 ║
║ SDIV                              100.000 ║
║ MOD                               100.000 ║
║ SMOD                              100.000 ║
║ ADDMOD                            100.000 ║
║ MULMOD                            100.000 ║
║ EXP                               100.000 ║
║ SIGNEXTEND                        100.000 ║
║ LT                                100.000 ║
║ GT                                100.000 ║
║ SLT                               100.000 ║
║ SGT                               100.000 ║
║ EQ                                100.000 ║
║ ISZERO                            100.000 ║
║ AND                               100.000 ║
║ OR                                100.000 ║
║ XOR                               100.000 ║
║ NOT                               100.000 ║
║ BYTE                              100.000 ║
║ SHL                               100.000 ║
║ SHR                               100.000 ║
║ SAR                               100.000 ║
║ SGT                               100.000 ║
║ SHA3                              100.000 ║
║ ADDRESS                           100.000 ║
║ BALANCE                           100.000 ║
║ ORIGIN                            100.000 ║
║ CALLER                            100.000 ║
║ CALLVALUE                         100.000 ║
║ CALLDATALOAD                      100.000 ║
║ CALLDATASIZE                      100.000 ║
║ CALLDATACOPY                      100.000 ║
║ CODESIZE                          100.000 ║
║ CODECOPY                          100.000 ║
║ GASPRICE                          100.000 ║
║ EXTCODESIZE                       100.000 ║
║ EXTCODECOPY                       100.000 ║
║ RETURNDATASIZE                    100.000 ║
║ RETURNDATACOPY                    100.000 ║
║ EXTCODEHASH                       100.000 ║
║ BLOCKHASH                         100.000 ║
║ COINBASE                          100.000 ║
║ TIMESTAMP                         100.000 ║
║ NUMBER                            100.000 ║
║ PREVRANDAO                        100.000 ║
║ GASLIMIT                          100.000 ║
║ CHAINID                           100.000 ║
║ SELFBALANCE                       100.000 ║
║ BASEFEE                           100.000 ║
║ POP                               100.000 ║
║ MLOAD                             100.000 ║
║ MSTORE                            100.000 ║
║ MSTORE8                           100.000 ║
║ SLOAD                             100.000 ║
║ SSTORE                            100.000 ║
║ JUMP                              100.000 ║
║ JUMPI                             100.000 ║
║ PC                                100.000 ║
║ MSIZE                             100.000 ║
║ GAS                               100.000 ║
║ JUMPDEST                          100.000 ║
║ PUSH0                             100.000 ║
║ PUSH1                             100.000 ║
║ PUSH2                             100.000 ║
║ PUSH4                             100.000 ║
║ PUSH5                             100.000 ║
║ PUSH6                             100.000 ║
║ PUSH7                             100.000 ║
║ PUSH8                             100.000 ║
║ PUSH9                             100.000 ║
║ PUSH10                            100.000 ║
║ PUSH11                            100.000 ║
║ PUSH12                            100.000 ║
║ PUSH13                            100.000 ║
║ PUSH14                            100.000 ║
║ PUSH15                            100.000 ║
║ PUSH16                            100.000 ║
║ PUSH17                            100.000 ║
║ PUSH18                            100.000 ║
║ PUSH19                            100.000 ║
║ PUSH20                            100.000 ║
║ PUSH21                            100.000 ║
║ PUSH22                            100.000 ║
║ PUSH23                            100.000 ║
║ PUSH24                            100.000 ║
║ PUSH25                            100.000 ║
║ PUSH26                            100.000 ║
║ PUSH27                            100.000 ║
║ PUSH28                            100.000 ║
║ PUSH29                            100.000 ║
║ PUSH30                            100.000 ║
║ PUSH31                            100.000 ║
║ PUSH32                            100.000 ║
║ DUP1                              100.000 ║
║ DUP2                              100.000 ║
║ DUP3                              100.000 ║
║ DUP4                              100.000 ║
║ DUP5                              100.000 ║
║ DUP6                              100.000 ║
║ DUP7                              100.000 ║
║ DUP8                              100.000 ║
║ DUP9                              100.000 ║
║ DUP10                             100.000 ║
║ DUP11                             100.000 ║
║ DUP12                             100.000 ║
║ DUP13                             100.000 ║
║ DUP14                             100.000 ║
║ DUP15                             100.000 ║
║ DUP16                             100.000 ║
║ SWAP1                             100.000 ║
║ SWAP2                             100.000 ║
║ SWAP3                             100.000 ║
║ SWAP4                             100.000 ║
║ SWAP5                             100.000 ║
║ SWAP6                             100.000 ║
║ SWAP7                             100.000 ║
║ SWAP8                             100.000 ║
║ SWAP9                             100.000 ║
║ SWAP10                            100.000 ║
║ SWAP11                            100.000 ║
║ SWAP12                            100.000 ║
║ SWAP13                            100.000 ║
║ SWAP14                            100.000 ║
║ SWAP15                            100.000 ║
║ SWAP16                            100.000 ║
║ CALL                              100.000 ║
║ STATICCALL                        100.000 ║
║ DELEGATECALL                      100.000 ║
║ CREATE                            100.000 ║
║ CREATE2                           100.000 ║
║ RETURN                            100.000 ║
║ REVERT                            100.000 ║
╚═══════════════════════════════════════════╝

@JulianVentura JulianVentura marked this pull request as ready for review August 29, 2024 14:14
// Invalid
_ => return Err(EraVmError::IncorrectBytecodeFormat),
};

code_info_bytes[1] = 0;
// let code_key = U256::from_big_endian(&code_info_bytes);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Delete this comment

Comment on lines -55 to -59
let mut address = 0;
for value in code.iter() {
heap.store(address, *value);
address += 32;
}
Copy link
Member

Choose a reason for hiding this comment

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

Seeing it starts with address 0, maybe we can just assign code to the new heap instead. This way we avoid all the copying and just move the whole vector.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually don't know if there is an easy way of doing that, since code is of type Vec<U256> but heap is of type Vec<u8>.
I'll keep the code as is, but if you happen to know how to do that transformation it could be a good optimization.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am not saying to make this change now, but something to consider for a future pr: we could easily retrieve the code as a Vec<u8> by adding a new function to the Storage trait that decommits and returns it in that format. The reason it's easy is that in the current zksync-era storage, bytecodes are originally stored as Vec<u8>, and we are actually converting them to Vec (see here).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, if we are able to modify that trait (we are not following any API), then thats totally an option.

src/execution.rs Outdated Show resolved Hide resolved
Comment on lines -55 to -59
let mut address = 0;
for value in code.iter() {
heap.store(address, *value);
address += 32;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not saying to make this change now, but something to consider for a future pr: we could easily retrieve the code as a Vec<u8> by adding a new function to the Storage trait that decommits and returns it in that format. The reason it's easy is that in the current zksync-era storage, bytecodes are originally stored as Vec<u8>, and we are actually converting them to Vec (see here).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants