Skip to content

Add precompile result caching / memoization - #4366

Draft
bhartnett wants to merge 17 commits into
masterfrom
precompile-cache2
Draft

Add precompile result caching / memoization#4366
bhartnett wants to merge 17 commits into
masterfrom
precompile-cache2

Conversation

@bhartnett

@bhartnett bhartnett commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Performance testing results

Running bench_precompiles.nim:

precompile cache speedup vs disabled, by hit rate (fork=FkOsaka, threadSafe=false)
speedup = compute / enabled;  >1.0 cache faster, <1.0 net loss;  miss forced by eviction
        precompile   compute ns         5%      25%      50%      75%      95%     100%  input B
       paEcRecover      25952.3      1.05x    1.34x    2.01x    4.01x   18.87x  303.11x      128
          paSha256        519.7      0.82x    0.99x    1.39x    2.27x    4.68x    6.17x      128
          paModExp      20069.0      1.10x    1.39x    2.05x    4.00x   18.36x  196.70x      160
           paEcAdd        275.8      0.78x    0.91x    1.24x    1.91x    3.51x    4.18x       64
           paEcMul      51537.7      1.29x    1.59x    2.41x    4.82x   23.11x  649.92x       96
         paPairing    1625728.8      1.05x    1.32x    1.98x    3.97x   19.56x 9781.49x      384
        paBlake2bf        156.9      0.47x    0.53x    0.66x    0.86x    1.21x    1.33x      213
        paBlsG1Add       3421.3      1.00x    1.25x    1.80x    3.32x   10.73x   24.62x      256
   paBlsG1MultiExp     119986.8      1.05x    1.34x    2.00x    3.94x   19.19x  567.86x      480
        paBlsG2Add       5258.1      0.98x    1.23x    1.80x    3.31x   10.67x   25.00x      512
   paBlsG2MultiExp      51902.4      1.05x    1.33x    1.99x    3.64x   18.87x  361.64x      288
      paBlsPairing     759850.5      1.05x    1.33x    2.01x    4.05x   19.93x 2703.88x      768
        paBlsMapG1      35100.3      1.04x    1.31x    1.96x    3.93x   18.97x  517.99x       64
        paBlsMapG2     114290.0      1.04x    1.32x    1.97x    3.98x   19.71x 1275.61x      128
      paP256Verify     234862.7      1.05x    1.32x    1.98x    3.98x   19.68x 2361.41x      160
       OVERALL sum    3048912.1      1.05x    1.33x    1.99x    3.99x   19.59x 1569.19x  each x1
   OVERALL geomean            -      0.97x    1.20x    1.75x    3.24x   12.17x  180.36x equal wt

Imported 10K blocks starting at block 24 million with optimistic state prefetching disabled:
master - elapsed=17m3s84ms
precompiles - elapsed=16m59s570ms

Imported 10K blocks starting at block 24 million with optimistic state prefetching enabled:
master - elapsed=16m9s598ms
precompiles - elapsed=14m9s902ms

@bhartnett
bhartnett marked this pull request as ready for review June 30, 2026 17:20
@bhartnett
bhartnett marked this pull request as draft July 1, 2026 01:36
@bhartnett

Copy link
Copy Markdown
Contributor Author

Leaving this in draft for now, since it requires more time to collect the realistic hit rates of each of the precompiles over larger block ranges. I've removed sha256, ecAdd and blake2bf from the caching since these are not slow enough to bother caching.

For the precompiles that can have a variable length input, it might be worth pre-hashing these inputs and only storing a Hash32 as the cache key but it remains to be seen if this is worth the computational overhead in order to save space in the caches.

It might turn out that the precompile caching is only beneficial when re-executing blocks/transaction a second time, such as with optimistic state prefetching or when running eth_call via RPC.

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.

1 participant