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

WASM Memory boosting mode with lazy cache #3079

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open

Conversation

querolita
Copy link
Member

@querolita querolita commented Mar 10, 2025

Closes o1-labs/o1js#2006 (kimchi-side o1-labs/o1js#2074)

This PR introduces a new enum called LazyCache which is meant to provide support for expensive evaluations that can be computed on-demand on the prover, instead of precomputed on the setup to save memory space for WASM.

LazyCache contains two variants: Cached and Lazy. By default, these evaluations (selector polynomials, precomputed constants, and lookups) will be cached in memory. But when the flag lazy_cache = true, then their computation is deferred from constraint system creation until the prover. This is done by passing an Arc closure that will obtain the evaluations when prompted. The API is quite simple, as it just requires to use .get() in these structures. This method will simply access the cached values in the Cached variant, and if it is a Lazy variant it will invoke the closure the first time, and then the result will be available in the computed field which is a OnceCell. When this is done, the closure is taken from memory to decrease the counter of the Arc and free the Mutex.

The main change is in kimchi/src/circuits/lazy_cache.rs. The rest of the diff is about adapting the constraint system, prover index, and prover code to the new type.

@querolita querolita marked this pull request as ready for review March 13, 2025 14:41
Copy link

@richardpringle richardpringle left a comment

Choose a reason for hiding this comment

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

Not quite sure how valuable this is, but that LazyCache is definitely doing some strange things.

I suggested you use LazyLock but realized that it only recently landed in stable and we're a little further behind.

It might be worth looking into how it's implemented though.

Using Box<dyn FnOnce() -> T> instead of Mutex<Option<Arc<Fn() -> T>>> would be much better if there's a way to do that. I guess the question of if that's possible is kind of dependent on where/when you expect cloning.

I also think that Default implementation is super dangerous and I'm curious why you needed that.

@dannywillems
Copy link
Member

dannywillems commented Mar 27, 2025

I suggested you use LazyLock but realized that it only recently landed in stable and we're a little further behind.

It seems LazyLock is available from 1.80.
I'm currently updating to 1.82 in Mina.

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.

Memory boosting mode
3 participants