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` #1669

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.699 ± 0.049 2.616 2.762 1.00
head big_factorial 4.050 ± 0.030 4.004 4.094 1.50 ± 0.03
Command Mean [s] Min [s] Max [s] Relative
base big_fibonacci 2.246 ± 0.039 2.211 2.320 1.00
head big_fibonacci 3.637 ± 0.041 3.579 3.707 1.62 ± 0.03
Command Mean [s] Min [s] Max [s] Relative
base blake2s_integration_benchmark 6.478 ± 0.080 6.365 6.613 1.00
head blake2s_integration_benchmark 14.317 ± 0.062 14.201 14.402 2.21 ± 0.03
Command Mean [s] Min [s] Max [s] Relative
base compare_arrays_200000 2.376 ± 0.045 2.322 2.451 1.00
head compare_arrays_200000 3.765 ± 0.049 3.695 3.855 1.58 ± 0.04
Command Mean [s] Min [s] Max [s] Relative
base dict_integration_benchmark 1.651 ± 0.030 1.604 1.695 1.00
head dict_integration_benchmark 2.343 ± 0.035 2.295 2.417 1.42 ± 0.03
Command Mean [s] Min [s] Max [s] Relative
base field_arithmetic_get_square_benchmark 1.116 ± 0.020 1.087 1.140 1.00
head field_arithmetic_get_square_benchmark 2.014 ± 0.011 1.999 2.030 1.81 ± 0.03
Command Mean [s] Min [s] Max [s] Relative
base integration_builtins 6.412 ± 0.056 6.319 6.496 1.00
head integration_builtins 14.256 ± 0.147 14.120 14.634 2.22 ± 0.03
Command Mean [s] Min [s] Max [s] Relative
base keccak_integration_benchmark 6.587 ± 0.081 6.494 6.730 1.00
head keccak_integration_benchmark 14.761 ± 0.279 14.550 15.523 2.24 ± 0.05
Command Mean [s] Min [s] Max [s] Relative
base linear_search 2.425 ± 0.046 2.314 2.476 1.00
head linear_search 3.756 ± 0.037 3.688 3.803 1.55 ± 0.03
Command Mean [s] Min [s] Max [s] Relative
base math_cmp_and_pow_integration_benchmark 1.684 ± 0.057 1.602 1.797 1.00
head math_cmp_and_pow_integration_benchmark 2.227 ± 0.034 2.165 2.276 1.32 ± 0.05
Command Mean [s] Min [s] Max [s] Relative
base math_integration_benchmark 1.554 ± 0.045 1.478 1.642 1.00
head math_integration_benchmark 2.118 ± 0.020 2.083 2.150 1.36 ± 0.04
Command Mean [s] Min [s] Max [s] Relative
base memory_integration_benchmark 1.392 ± 0.018 1.362 1.425 1.00
head memory_integration_benchmark 1.946 ± 0.072 1.900 2.142 1.40 ± 0.05
Command Mean [s] Min [s] Max [s] Relative
base operations_with_data_structures_benchmarks 1.534 ± 0.028 1.487 1.571 1.00
head operations_with_data_structures_benchmarks 2.245 ± 0.020 2.213 2.270 1.46 ± 0.03
Command Mean [ms] Min [ms] Max [ms] Relative
base pedersen 601.4 ± 9.0 589.7 621.4 1.00
head pedersen 753.6 ± 9.2 743.8 770.0 1.25 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base poseidon_integration_benchmark 1.190 ± 0.010 1.179 1.212 1.00
head poseidon_integration_benchmark 1.304 ± 0.017 1.282 1.327 1.10 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base secp_integration_benchmark 2.112 ± 0.033 2.076 2.182 1.00
head secp_integration_benchmark 2.935 ± 0.014 2.913 2.958 1.39 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base set_integration_benchmark 1.179 ± 0.012 1.155 1.194 1.27 ± 0.02
head set_integration_benchmark 0.928 ± 0.008 0.916 0.944 1.00
Command Mean [s] Min [s] Max [s] Relative
base uint256_integration_benchmark 4.101 ± 0.039 4.042 4.150 1.00
head uint256_integration_benchmark 7.627 ± 0.066 7.542 7.759 1.86 ± 0.02
fmoletta commented 1 month ago

Already merged #1672