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

Surface module cache for reuse. #1506

Merged
merged 3 commits into from
Jan 22, 2025
Merged

Conversation

graydon
Copy link
Contributor

@graydon graydon commented Jan 9, 2025

The goals of this PR are:

  1. Surface the ModuleCache from inside soroban to its embedding environment.
  2. Allow a module cache to outlive the soroban Host, and be reused from one Host to the next.
  3. Allow the ModuleCache to be pre-populated from before the lifetime of a single Host, (eg. by stellar-core, with all the non-archived contracts currently live in the ledger, at startup or such.)

These goals are accomplished by the following structural changes:

  1. Factoring out error-handling / error-mapping functionality of Host into a separate trait ErrorHandler that Host implements, and adding a new trait CompilationContext that extends both ErrorHandler + AsBudget. This abstracts the services the ModuleCache needs from the Host, such that the embedding environment can provide its own CompilationContext in the times pre- or post-Host lifetime.
  2. Modifying the internal map that stores Modules inside the ModuleCache to use a refcounted-shared-mutable std::map::BtreeMap that can be incrementally and efficiently added-to or removed-from, and is not metered.
  3. Module storage and arguments that were Rc<> are made Arc<> and the whole ModuleCache is made Send+Sync. This is mainly because we want to work with the reusable ModuleCache across C++ threads, and while Rust has no idea about C++ threads it at least helps us shoot ourselves in the foot less if all the Rust code that works with it preserves the sense of "this would be threadsafe in Rust if Rust was making the threads".
  4. There is also a bit of gratuitous churn where things get renamed from foo => wasmi_foo and Foo => wasmi::Foo. This is because this change was done as part of (and there'll be a followup PR including) work equipping the Vm and ModuleCache with an experimental wasmtime/winch backend. It turns out this PR's work (on just the ModuleCache reuse part) is high enough value, and agnostic enough to which backend it's using, that we want to split this work out, move it up in the schedule and explore getting it into as early a release as possible. But I didn't bother reverting-out the wasmi_-qualification since it's intertwined with other changes here and harmless to keep.

This PR is a continuation of #1511 and should be merged after it.

This PR implements CAP-0065

@graydon graydon requested review from leighmcculloch, dmkozh and sisuresh and removed request for sisuresh, dmkozh and leighmcculloch January 9, 2025 08:00
@graydon graydon mentioned this pull request Jan 9, 2025
@graydon graydon requested a review from jayz22 January 9, 2025 08:17
@graydon graydon marked this pull request as draft January 9, 2025 08:17
@graydon graydon force-pushed the reusable-module-cache branch 3 times, most recently from 86ea7ea to ad0d6d2 Compare January 10, 2025 19:34
Copy link
Contributor

@dmkozh dmkozh left a comment

Choose a reason for hiding this comment

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

Looks good to me overall, only not sure about the gating of the old behavior (left a comment about that).

soroban-env-host/src/vm/module_cache.rs Outdated Show resolved Hide resolved
@graydon graydon marked this pull request as ready for review January 11, 2025 01:14
soroban-env-host/src/vm.rs Outdated Show resolved Hide resolved
soroban-env-host/src/vm/module_cache.rs Outdated Show resolved Hide resolved
soroban-env-host/src/e2e_invoke.rs Show resolved Hide resolved
soroban-env-host/src/vm/module_cache.rs Show resolved Hide resolved
@graydon graydon requested a review from a team as a code owner January 16, 2025 07:30
@graydon graydon force-pushed the reusable-module-cache branch 2 times, most recently from 692d056 to 9515643 Compare January 18, 2025 07:45
@graydon graydon force-pushed the reusable-module-cache branch from 468aa94 to a3882e9 Compare January 22, 2025 02:38
@graydon graydon force-pushed the reusable-module-cache branch from a3882e9 to ecea5b9 Compare January 22, 2025 19:48
@graydon graydon added this pull request to the merge queue Jan 22, 2025
Merged via the queue into stellar:main with commit 0b7883d Jan 22, 2025
12 checks passed
@graydon graydon deleted the reusable-module-cache branch January 22, 2025 22:14
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.

3 participants