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

feat(sandbox): Implement LRU cache for Wasmer modules #3801

Merged
merged 9 commits into from
Mar 20, 2024

Conversation

ukint-vs
Copy link
Member

@ukint-vs ukint-vs commented Mar 8, 2024

type CachedModule = (Vec<u8>, Arc<Module>);

static CACHED_MODULES: OnceLock<Mutex<LRUCache<CachedModule, 1024>>> =
    OnceLock::new();

In-memory cache reduces wasmer module access latency by up to 50%.
It also has a small memory footprint (I will do more testing on the testnet) that can be moved to the swap if something goes wrong.
We don't need to rely on the filesystem, now a new temporary directory is created every time, so there is no point in serializing modules to the filesystem, temp сache lifetime is the same as that of the node.
I would remove file caching if in-memory approach doesn't cause any problems.

  • LRU cache with 1024 cap.
  • BLAKE3 Hash only on cache miss (to store to temp file cache).

module <- LRU <- file cache <- compile

benches:

# instantiate_module_per_kb
./gear-master benchmark pallet --chain=vara-dev --steps=20 --repeat=10 --heap-pages=4096 --pallet=pallet_gear --extrinsic="instantiate_module_per_kb"
Model:
Time ~=    55.35
    + c    1.297
              µs

./gear-master-lru-hash benchmark pallet --chain=vara-dev --steps=20 --repeat=10 --heap-pages=4096 --pallet=pallet_gear --extrinsic="instantiate_module_per_kb"
Model:
Time ~=    33.42
    + c     0.76
              µs

# avoid BLAKE3 hash on cache hit
./gear-master-lru-wasm benchmark pallet --chain=vara-dev --steps=20 --repeat=10 --heap-pages=4096 --pallet=pallet_gear --extrinsic="instantiate_module_per_kb"
Model:
Time ~=    29.95
    + c    0.111
              µs

@gear-tech/dev

@ukint-vs ukint-vs added A1-inprogress Issue is in progress or PR draft is not ready to be reviewed C1-feature Feature request labels Mar 8, 2024
@ukint-vs ukint-vs self-assigned this Mar 8, 2024
@NikVolf
Copy link
Member

NikVolf commented Mar 9, 2024

Consider bump host functions versions that use this, just in case? @grishasobol ?

@ukint-vs
Copy link
Member Author

ukint-vs commented Mar 9, 2024

Consider bump host functions versions that use this, just in case? @grishasobol ?

This actually acts as a drop-in host replacement. Additionally, the current implementation does not affect the file cache in any way, since the file cache is empty every time the node is started. but I left the interaction if during further tests I have to clear the cache in memory

@grishasobol
Copy link
Member

grishasobol commented Mar 11, 2024

Consider bump host functions versions that use this, just in case? @grishasobol ?

Ok, we can consider, but actually I cannot see reasons to do this. Everything must work fine even for old blocks

@NikVolf
Copy link
Member

NikVolf commented Mar 11, 2024

Consider bump host functions versions that use this, just in case? @grishasobol ?

Ok, we can consider, but actually I cannot see reasons to do this. Everything must work fine even for old blocks

Okk, maybe test resyncing from scratch then @ukint-vs

sandbox/host/src/lib.rs Outdated Show resolved Hide resolved
sandbox/host/src/sandbox/wasmer_backend.rs Outdated Show resolved Hide resolved
@ukint-vs ukint-vs changed the title feat(sandbox): Implement in-memory caching for Wasmer modules feat(sandbox): Implement LRU cache for Wasmer modules Mar 14, 2024
@ukint-vs ukint-vs added A0-pleasereview PR is ready to be reviewed by the team and removed A1-inprogress Issue is in progress or PR draft is not ready to be reviewed labels Mar 14, 2024
@ukint-vs ukint-vs marked this pull request as ready for review March 14, 2024 19:22
@ukint-vs ukint-vs added E2-forcemacos Force CI to run macOS build in PR D7-performance Increase our node/runtime/programs execution work performance labels Mar 14, 2024
@NikVolf NikVolf added check-validator Build binary and validate on 8 vara validator and removed C1-feature Feature request D7-performance Increase our node/runtime/programs execution work performance labels Mar 15, 2024
@ukint-vs
Copy link
Member Author

Consider bump host functions versions that use this, just in case? @grishasobol ?

Ok, we can consider, but actually I cannot see reasons to do this. Everything must work fine even for old blocks

Okk, maybe test resyncing from scratch then @ukint-vs

Testnet from start is ok. Without any problems

sandbox/host/src/sandbox/wasmer_backend.rs Outdated Show resolved Hide resolved
sandbox/host/src/sandbox/wasmer_backend.rs Outdated Show resolved Hide resolved
sandbox/host/src/lib.rs Outdated Show resolved Hide resolved
sandbox/host/Cargo.toml Outdated Show resolved Hide resolved
@breathx
Copy link
Member

breathx commented Mar 18, 2024

I'd consider using mapping within LRU caching to avoid 1024 slices comparison, e.g. use 64 bytes from the middle as keys.

Mostly, not a big deal cause wasms are differ in size and comparison goes super fast

@ukint-vs
Copy link
Member Author

I'd consider using mapping within LRU caching to avoid 1024 slices comparison, e.g. use 64 bytes from the middle as keys.

Mostly, not a big deal cause wasms are differ in size and comparison goes super fast

We can't guarantee that the bytes will be unique. Yeah it's not a big deal and it's better to avoid vec manipulation which can lead to unnecessary logic. I think we can pass code_id in the future.

Copy link
Member

@grishasobol grishasobol 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, just some comments to consider

sandbox/host/src/sandbox/wasmer_backend.rs Outdated Show resolved Hide resolved
sandbox/host/src/sandbox/wasmer_backend.rs Outdated Show resolved Hide resolved
@ukint-vs ukint-vs merged commit 53071e1 into master Mar 20, 2024
12 checks passed
@ukint-vs ukint-vs deleted the vs/in-memory-cache branch March 20, 2024 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A0-pleasereview PR is ready to be reviewed by the team check-validator Build binary and validate on 8 vara validator E2-forcemacos Force CI to run macOS build in PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants