lambdaclass / cairo-vm

cairo-vm is a Rust implementation of the Cairo VM. Cairo (CPU Algebraic Intermediate Representation) is a programming language for writing provable programs, where one party can prove to another that a certain computation was executed correctly without the need for this party to re-execute the same program.
https://lambdaclass.github.io/cairo-vm
Apache License 2.0
488 stars 133 forks source link

perf: cache-friendly `MemoryCell` #1666

Closed Oppen closed 1 month ago

Oppen commented 4 months ago

Refactors MemoryCell to store a simple [u64; 4] to use the cache more appropriately. This structure is now a fixed 32 bytes with 32 bytes alignment, meaning any given cell in the VM memory will never be split across two different cache lines, and eviction will be minimized in virtue of being the optimal size for a Felt of 252 bits.

The cost of this change is no longer being able to extract the MemoryCell value as a simple reference, but requiring a conversion for the "normal" MaybeRelocatable, that is, it implies extra computation and extra copying to extract values. This behaves well for Lambdaworks where the data is inline in the array, but quite a hit for the BigUint implementation, which now allocates and frees memory much more often. Upgrading is recommended only when using Lambdaworks. The tradeoff is deemed acceptable since only the Lambdaworks (actually types-rs) implementation is preserved for the 1.x branch.

Checklist

github-actions[bot] commented 4 months ago

Benchmark Results for unmodified programs :rocket:

Command Mean [s] Min [s] Max [s] Relative
base big_factorial 2.555 ± 0.093 2.443 2.794 1.00
head big_factorial 3.900 ± 0.069 3.843 4.037 1.53 ± 0.06
Command Mean [s] Min [s] Max [s] Relative
base big_fibonacci 2.164 ± 0.030 2.136 2.244 1.00
head big_fibonacci 3.643 ± 0.037 3.613 3.714 1.68 ± 0.03
Command Mean [s] Min [s] Max [s] Relative
base blake2s_integration_benchmark 6.190 ± 0.040 6.106 6.234 1.00
head blake2s_integration_benchmark 14.169 ± 0.034 14.126 14.243 2.29 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base compare_arrays_200000 2.254 ± 0.009 2.235 2.268 1.00
head compare_arrays_200000 3.679 ± 0.011 3.663 3.695 1.63 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base dict_integration_benchmark 1.580 ± 0.005 1.571 1.586 1.00
head dict_integration_benchmark 2.303 ± 0.020 2.290 2.359 1.46 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base field_arithmetic_get_square_benchmark 1.077 ± 0.005 1.070 1.083 1.00
head field_arithmetic_get_square_benchmark 1.972 ± 0.037 1.956 2.076 1.83 ± 0.04
Command Mean [s] Min [s] Max [s] Relative
base integration_builtins 6.156 ± 0.044 6.092 6.222 1.00
head integration_builtins 13.942 ± 0.040 13.872 14.014 2.26 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base keccak_integration_benchmark 6.307 ± 0.050 6.230 6.405 1.00
head keccak_integration_benchmark 14.435 ± 0.077 14.359 14.607 2.29 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base linear_search 2.279 ± 0.046 2.242 2.400 1.00
head linear_search 3.631 ± 0.020 3.615 3.670 1.59 ± 0.03
Command Mean [s] Min [s] Max [s] Relative
base math_cmp_and_pow_integration_benchmark 1.540 ± 0.008 1.531 1.557 1.00
head math_cmp_and_pow_integration_benchmark 2.115 ± 0.010 2.097 2.132 1.37 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base math_integration_benchmark 1.472 ± 0.006 1.459 1.481 1.00
head math_integration_benchmark 2.056 ± 0.006 2.045 2.066 1.40 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base memory_integration_benchmark 1.292 ± 0.008 1.277 1.307 1.00
head memory_integration_benchmark 1.882 ± 0.005 1.876 1.888 1.46 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base operations_with_data_structures_benchmarks 1.431 ± 0.008 1.418 1.444 1.00
head operations_with_data_structures_benchmarks 2.197 ± 0.022 2.183 2.256 1.54 ± 0.02
Command Mean [ms] Min [ms] Max [ms] Relative
base pedersen 581.0 ± 1.2 578.0 582.3 1.00
head pedersen 744.7 ± 4.1 739.2 753.9 1.28 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base poseidon_integration_benchmark 1.158 ± 0.009 1.150 1.183 1.00
head poseidon_integration_benchmark 1.282 ± 0.004 1.276 1.288 1.11 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base secp_integration_benchmark 2.007 ± 0.006 2.000 2.014 1.00
head secp_integration_benchmark 2.904 ± 0.009 2.894 2.923 1.45 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base set_integration_benchmark 1.151 ± 0.005 1.143 1.161 1.30 ± 0.01
head set_integration_benchmark 0.883 ± 0.006 0.877 0.896 1.00
Command Mean [s] Min [s] Max [s] Relative
base uint256_integration_benchmark 3.916 ± 0.015 3.886 3.933 1.00
head uint256_integration_benchmark 7.449 ± 0.054 7.402 7.576 1.90 ± 0.02
fmoletta commented 1 month ago

Already merged #1672