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

perf: cache-friendly MemoryCell #1669

Closed
wants to merge 5 commits into from
Closed

Conversation

Oppen
Copy link
Member

@Oppen Oppen commented Mar 15, 2024

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

  • Linked to Github Issue
  • Unit tests added
  • Integration tests added.
  • This change requires new documentation.
    • Documentation has been added/updated.
    • CHANGELOG has been updated.

@Oppen Oppen changed the base branch from main to releases/0.9.X March 15, 2024 19:15
@Oppen Oppen changed the title perf/compact memory cell perf: cache-friendly MemoryCell Mar 15, 2024
@Oppen Oppen marked this pull request as ready for review March 15, 2024 19:16
Store data and metadata inline as a single `[u64; 4]` with `32` bytes
alignment, fitting a whole number of cells per cache line to reduce
evictions and double sharing and to avoid split line access.

Besides performance, an observable change is that now
`Memory::get_value` always returns a `Cow::Owned` variant because the
decoding process always creates a new value.
Copy link

github-actions bot commented Mar 15, 2024

Benchmark Results for unmodified programs 🚀

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
Copy link
Member

Already merged #1672

@fmoletta fmoletta closed this May 29, 2024
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.

None yet

2 participants