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

vm: AnyCache #10825

Merged
merged 9 commits into from
Mar 20, 2024
Merged

vm: AnyCache #10825

merged 9 commits into from
Mar 20, 2024

Conversation

nagisa
Copy link
Collaborator

@nagisa nagisa commented Mar 18, 2024

AnyCache is a part of the now-expanded concept of the ContractRuntimeCache. It allows runtimes to put anything they would like into it, so long as this cache is keyed by CryptoKey.

Initially I was in a lot of pain as intermediate drafts have required me to add a unsafe impl Sync for UniversalArtifact which I'm not sure I could possibly prove. However in the end I ended up being able to avoid it entirely by giving out AnyCache by reference only. Lovely! -- EDIT: crappa, looks like I did something wrong testing this locally T_T Sync appears to remain a requirement.

Note that only NearVM uses this cache currently. I don't see a good reason to port this code back to the old runtimes.

Supersedes #10787
Co-authored-by: Longarithm the.aleksandr.logunov@gmail.com

This cache is more general than the previous cache and also better
abstracted in that it allows storing anything into it, and permits
configuration of it from outside the contract runtime.

Also no global statics (other than for the empty cache sentinel.)
Simplifies the code quite a bit.
The blast radius... Thank dog I decided against upgrading lru for the
entire workspace or I'd be sitting doing just that till deep winter.
@nagisa nagisa requested a review from Ekleog-NEAR March 18, 2024 19:18
@nagisa
Copy link
Collaborator Author

nagisa commented Mar 18, 2024

Pending: unit tests, benchmark results, configuration of the cache size.
Future PR: replace lru cache with moka (funny story – neighbours golden retriever is also Moka).

Alas this didn't pan out quite yet -- instantiation still requires an
`Arc<Artifact>` and removing that requirement is not the right time
right now
Copy link

codecov bot commented Mar 19, 2024

Codecov Report

Attention: Patch coverage is 90.66148% with 24 lines in your changes are missing coverage. Please review.

Project coverage is 71.60%. Comparing base (7cb9087) to head (adb7000).

Files Patch % Lines
runtime/near-vm-runner/src/cache.rs 91.42% 7 Missing and 5 partials ⚠️
runtime/near-vm-runner/src/near_vm_runner.rs 90.27% 1 Missing and 6 partials ⚠️
core/store/src/lib.rs 66.66% 1 Missing ⚠️
nearcore/src/config.rs 85.71% 0 Missing and 1 partial ⚠️
runtime/near-vm-runner/src/wasmer_runner.rs 75.00% 1 Missing ⚠️
runtime/near-vm-runner/src/wasmtime_runner.rs 50.00% 1 Missing ⚠️
...ntime/runtime-params-estimator/src/vm_estimator.rs 80.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #10825      +/-   ##
==========================================
+ Coverage   71.57%   71.60%   +0.02%     
==========================================
  Files         760      760              
  Lines      153133   153263     +130     
  Branches   153133   153263     +130     
==========================================
+ Hits       109607   109740     +133     
+ Misses      38539    38535       -4     
- Partials     4987     4988       +1     
Flag Coverage Δ
backward-compatibility 0.24% <0.00%> (-0.01%) ⬇️
db-migration 0.24% <0.00%> (-0.01%) ⬇️
genesis-check 1.42% <0.62%> (+<0.01%) ⬆️
integration-tests 36.89% <49.02%> (-0.02%) ⬇️
linux 70.24% <90.66%> (+0.02%) ⬆️
linux-nightly 71.11% <89.88%> (+0.03%) ⬆️
macos 54.78% <76.82%> (+1.66%) ⬆️
pytests 1.64% <0.62%> (-0.01%) ⬇️
sanity-checks 1.43% <0.62%> (+<0.01%) ⬆️
unittests 67.34% <88.32%> (+0.05%) ⬆️
upgradability 0.29% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@nagisa nagisa marked this pull request as ready for review March 19, 2024 13:59
@nagisa nagisa requested a review from a team as a code owner March 19, 2024 13:59
Copy link
Contributor

@mooori mooori left a comment

Choose a reason for hiding this comment

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

I'm leaving some initial questions and comments. Since it is the first time I'm looking at the compiled contract cache, I'll need to do another review.

Should cache eviction be tested or can this functionality be considered handled by lru and hence not tested here?

runtime/near-vm-runner/src/cache.rs Outdated Show resolved Hide resolved
runtime/near-vm-runner/src/cache.rs Outdated Show resolved Hide resolved
runtime/near-vm-runner/src/cache.rs Show resolved Hide resolved
runtime/near-vm-runner/src/near_vm_runner.rs Outdated Show resolved Hide resolved
runtime/near-vm-runner/src/cache.rs Show resolved Hide resolved
@nagisa
Copy link
Collaborator Author

nagisa commented Mar 19, 2024

Should cache eviction be tested or can this functionality be considered handled by lru and hence not tested here?

Yeah I think this is entirely a concern for the underlying crate. We might also just use a different algorithm soon, so eviction algorithm might change too...

@nagisa nagisa added this pull request to the merge queue Mar 20, 2024
Merged via the queue into near:master with commit 60ce4fb Mar 20, 2024
38 of 52 checks passed
@nagisa nagisa deleted the anycache branch March 20, 2024 09:16
Copy link
Contributor

@mooori mooori left a comment

Choose a reason for hiding this comment

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

LGTM.

nearcore/src/config.rs Show resolved Hide resolved
nearcore/src/config.rs Show resolved Hide resolved
runtime/near-vm-runner/src/cache.rs Show resolved Hide resolved
runtime/near-vm-runner/src/near_vm_runner.rs Show resolved Hide resolved
nagisa added a commit to nagisa/nearcore that referenced this pull request Mar 20, 2024
`AnyCache` is a part of the now-expanded concept of the
`ContractRuntimeCache`. It allows runtimes to put anything they would
like into it, so long as this cache is keyed by `CryptoKey`.

Initially I was in a lot of pain as intermediate drafts have required me
to add a `unsafe impl Sync for UniversalArtifact` which I'm not sure I
could possibly prove. However in the end I ended up being able to avoid
it entirely by giving out `AnyCache` by reference only. Lovely! -- EDIT:
crappa, looks like I did something wrong testing this locally T_T Sync
appears to remain a requirement.

Note that only NearVM uses this cache currently. I don't see a good
reason to port this code back to the old runtimes.

Supersedes near#10787
Co-authored-by: Longarithm <the.aleksandr.logunov@gmail.com>
VanBarbascu pushed a commit that referenced this pull request Mar 26, 2024
`AnyCache` is a part of the now-expanded concept of the
`ContractRuntimeCache`. It allows runtimes to put anything they would
like into it, so long as this cache is keyed by `CryptoKey`.

Initially I was in a lot of pain as intermediate drafts have required me
to add a `unsafe impl Sync for UniversalArtifact` which I'm not sure I
could possibly prove. However in the end I ended up being able to avoid
it entirely by giving out `AnyCache` by reference only. Lovely! -- EDIT:
crappa, looks like I did something wrong testing this locally T_T Sync
appears to remain a requirement.

Note that only NearVM uses this cache currently. I don't see a good
reason to port this code back to the old runtimes.

Supersedes #10787
Co-authored-by: Longarithm <the.aleksandr.logunov@gmail.com>
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