Skip to content

Commit

Permalink
Decompose VM instantiation costs and add module cache (#1359)
Browse files Browse the repository at this point in the history
This is a sketch of the first step in the plan for addressing #1292 and
eventually #827 and #1313, namely:

- add a bunch of cost types that decompose the current "worst case" VM
instantiation cost type
  - continue to charge the worst case on initial contract upload
- _store_ the decomposed cost-type inputs in the ledger, since we can
observe them after the initial upload parse
- _use_ those decomposed cost-type inputs when doing runtime
instantiation
  - add a module cache
  - populate the module cache with all modules on host startup
  - use cached modules during instantiation

This PR has accompanying changes in XDR and wasmi:

  - stellar/stellar-xdr#177
  - stellar/rs-stellar-xdr#346

Remaining to do:

  - [x] determine what the correct set of decomposed cost types even is
  - [ ] ~add more code to wasmi to enable observing more inputs~
- [x] add cost-type runners / calibrations for all the decomposed
cost-types
  - [x] protocol-gate this new behaviour
- [x] make the linker-loop do less work to match the tighter cost model
(i.e. complete #1292)
- [x] possibly _duplicate_ the set of cost types added here, so we have
a cached and uncached version of each, and implement a solution to #827
- [ ] possibly decompose the set sufficiently to model what will happen
when we take wasmi 0.32 and enable lazy translation (i.e. complete
#1313)
  • Loading branch information
graydon authored Mar 26, 2024
1 parent 93120b6 commit 41b4ee3
Show file tree
Hide file tree
Showing 40 changed files with 2,671 additions and 307 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
continue-on-error: ${{ matrix.checks == 'advisories' }}
steps:
- uses: actions/checkout@v3
- uses: EmbarkStudios/cargo-deny-action@e0a440755b184aa50374330fa75cca0f84fcb59a
- uses: EmbarkStudios/cargo-deny-action@b01e7a8cfb1f496c52d77361e84c1840d8246393
with:
command: check ${{ matrix.checks }}

Expand Down
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ soroban-env-guest = { version = "=20.3.0", path = "soroban-env-guest" }
soroban-env-host = { version = "=20.3.0", path = "soroban-env-host" }
soroban-env-macros = { version = "=20.3.0", path = "soroban-env-macros" }
soroban-builtin-sdk-macros = { version = "=20.3.0", path = "soroban-builtin-sdk-macros" }
# NB: this must match the wasmparser version wasmi is using
wasmparser = "=0.116.1"

# NB: When updating, also update the version in rs-soroban-env dev-dependencies
[workspace.dependencies.stellar-xdr]
version = "=20.1.0"
git = "https://github.com/stellar/rs-stellar-xdr"
rev = "2e0f7f7d42fcd6c3c42eb0d65570fba9f5193d7e"
rev = "44b7e2d4cdf27a3611663e82828de56c5274cba0"
default-features = false

[workspace.dependencies.wasmi]
Expand Down
4 changes: 2 additions & 2 deletions soroban-bench-utils/src/tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ mod cpu {

#[cfg(not(any(target_os = "linux", target_os = "macos")))]
mod cpu {
pub struct InstructionCounter(u64);
pub struct InstructionCounter;
impl InstructionCounter {
pub fn new() -> Self {
InstructionCounter(0)
InstructionCounter
}
pub fn begin(&mut self) {}
pub fn end_and_count(&mut self) -> u64 {
Expand Down
3 changes: 2 additions & 1 deletion soroban-env-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ crate-git-revision = "=0.0.6"
soroban-env-macros = { workspace = true }
stellar-xdr = { workspace = true, default-features = false, features = [ "curr" ] }
wasmi = { workspace = true, optional = true }
wasmparser = { workspace = true, optional = true}
serde = { version = "=1.0.192", features = ["derive"], optional = true }
static_assertions = "=1.1.0"
ethnum = "=1.5.0"
Expand All @@ -34,7 +35,7 @@ num-traits = "=0.2.17"
[features]
std = ["stellar-xdr/std", "stellar-xdr/base64"]
serde = ["dep:serde", "stellar-xdr/serde"]
wasmi = ["dep:wasmi"]
wasmi = ["dep:wasmi", "dep:wasmparser"]
testutils = ["dep:arbitrary", "stellar-xdr/arbitrary"]
next = ["stellar-xdr/next", "soroban-env-macros/next"]
tracy = ["dep:tracy-client"]
Expand Down
7 changes: 7 additions & 0 deletions soroban-env-common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,13 @@ impl From<wasmi::Error> for Error {
}
}

#[cfg(feature = "wasmi")]
impl From<wasmparser::BinaryReaderError> for Error {
fn from(_: wasmparser::BinaryReaderError) -> Self {
Error::from_type_and_code(ScErrorType::WasmVm, ScErrorCode::InvalidInput)
}
}

impl Error {
// NB: we don't provide a "get_type" to avoid casting a bad bit-pattern into
// an ScErrorType. Instead we provide an "is_type" to check any specific
Expand Down
5 changes: 3 additions & 2 deletions soroban-env-host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "Apache-2.0"
version.workspace = true
readme = "../README.md"
edition = "2021"
rust-version.workspace = true
rust-version = "1.74"
build = "build.rs"

exclude = ["observations/"]
Expand All @@ -17,6 +17,7 @@ exclude = ["observations/"]
soroban-builtin-sdk-macros = { workspace = true }
soroban-env-common = { workspace = true, features = ["std", "wasmi", "shallow-val-hash"] }
wasmi = { workspace = true }
wasmparser = { workspace = true }
stellar-strkey = "=0.0.8"
static_assertions = "=1.1.0"
sha2 = "=0.10.8"
Expand Down Expand Up @@ -86,7 +87,7 @@ rustversion = "1.0"
[dev-dependencies.stellar-xdr]
version = "=20.1.0"
git = "https://github.com/stellar/rs-stellar-xdr"
rev = "2e0f7f7d42fcd6c3c42eb0d65570fba9f5193d7e"
rev = "44b7e2d4cdf27a3611663e82828de56c5274cba0"
default-features = false
features = ["arbitrary"]

Expand Down
Loading

0 comments on commit 41b4ee3

Please sign in to comment.