Skip to content

Commit

Permalink
Sketch of cheaper instantiation change
Browse files Browse the repository at this point in the history
  • Loading branch information
graydon committed Mar 1, 2024
1 parent abd977d commit 21f4af5
Show file tree
Hide file tree
Showing 21 changed files with 640 additions and 121 deletions.
5 changes: 0 additions & 5 deletions Cargo.lock

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

10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,20 @@ soroban-builtin-sdk-macros = { version = "=20.2.2", path = "soroban-builtin-sdk-
# NB: When updating, also update the version in rs-soroban-env dev-dependencies
[workspace.dependencies.stellar-xdr]
version = "=20.1.0"
path = "/src/rs-stellar-xdr/"
# git = "https://github.com/stellar/rs-stellar-xdr"
# rev = "8b9d623ef40423a8462442b86997155f2c04d3a1"
default-features = false

[workspace.dependencies.wasmi]
package = "soroban-wasmi"
version = "=0.31.1-soroban.20.0.1"
git = "https://github.com/stellar/wasmi"
rev = "0ed3f3dee30dc41ebe21972399e0a73a41944aa0"
path = "/src/wasmi/crates/wasmi/"
#git = "https://github.com/stellar/wasmi"
#rev = "0ed3f3dee30dc41ebe21972399e0a73a41944aa0"

# [patch."https://github.com/stellar/rs-stellar-xdr"]
# stellar-xdr = { path = "../rs-stellar-xdr/" }
#[patch."https://github.com/stellar/rs-stellar-xdr"]
# stellar-xdr = { path = "/src/rs-stellar-xdr/" }
# [patch."https://github.com/stellar/wasmi"]
# soroban-wasmi = { path = "../wasmi/crates/wasmi/" }
# soroban-wasmi_core = { path = "../wasmi/crates/core/" }
Expand Down
1 change: 1 addition & 0 deletions soroban-env-host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ rustversion = "1.0"

[dev-dependencies.stellar-xdr]
version = "=20.1.0"
path = "/src/rs-stellar-xdr"
# git = "https://github.com/stellar/rs-stellar-xdr"
# rev = "8b9d623ef40423a8462442b86997155f2c04d3a1"
default-features = false
Expand Down
2 changes: 1 addition & 1 deletion soroban-env-host/benches/common/cost_types/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl HostCostMeasurement for InvokeVmFunctionMeasure {
fn new_random_case(host: &Host, _rng: &mut StdRng, _input: u64) -> (Rc<Vm>, Vec<Value>) {
let id: Hash = [0; 32].into();
let code = wasm_module_with_empty_invoke();
let vm = Vm::new(&host, id, &code).unwrap();
let vm = Vm::new(&host, id, &code, None).unwrap();
let args = vec![Value::I64(0); MAX_VM_ARGS];
(vm, args)
}
Expand Down
163 changes: 134 additions & 29 deletions soroban-env-host/benches/common/cost_types/vm_ops.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,150 @@
#[allow(unused)]
use super::wasm_insn_exec::{wasm_module_with_4n_insns, wasm_module_with_n_internal_funcs};
use super::wasm_insn_exec::{
wasm_module_with_n_data_segments, wasm_module_with_n_elem_segments, wasm_module_with_n_exports,
wasm_module_with_n_globals, wasm_module_with_n_imports, wasm_module_with_n_insns,
wasm_module_with_n_internal_funcs, wasm_module_with_n_table_entries, wasm_module_with_n_types,
};
use crate::common::{util, HostCostMeasurement};
use rand::{rngs::StdRng, Rng};
use soroban_env_host::{
cost_runner::{VmInstantiationRun, VmInstantiationSample},
xdr, Host,
cost_runner::{
VmInstantiationDataSegmentsRun, VmInstantiationElemSegmentsRun, VmInstantiationExportsRun,
VmInstantiationFunctionsRun, VmInstantiationGlobalsRun, VmInstantiationImportsRun,
VmInstantiationInstructionsRun, VmInstantiationRun, VmInstantiationSample,
VmInstantiationTableEntriesRun, VmInstantiationTypesRun,
},
xdr, Host, Vm,
};

pub(crate) struct VmInstantiationMeasure;
pub(crate) struct VmInstantiationInstructionsMeasure;
pub(crate) struct VmInstantiationFunctionsMeasure;
pub(crate) struct VmInstantiationGlobalsMeasure;
pub(crate) struct VmInstantiationTableEntriesMeasure;
pub(crate) struct VmInstantiationTypesMeasure;
pub(crate) struct VmInstantiationDataSegmentsMeasure;
pub(crate) struct VmInstantiationElemSegmentsMeasure;
pub(crate) struct VmInstantiationImportsMeasure;
pub(crate) struct VmInstantiationExportsMeasure;

// This measures the cost of instantiating a host::Vm on a variety of possible
// wasm modules, of different sizes. The input value should be the size of the
// module, though for now we're just selecting modules from the fixed example
// repertoire. Costs should be linear.
impl HostCostMeasurement for VmInstantiationMeasure {
type Runner = VmInstantiationRun;
macro_rules! impl_measurement_for_instantiation_cost_type {
($RUNNER:ty, $MEASURE:ty, $BUILD:ident, $HAS_INPUTS:expr) => {
impl HostCostMeasurement for $MEASURE {
type Runner = $RUNNER;

fn new_best_case(_host: &Host, _rng: &mut StdRng) -> VmInstantiationSample {
let id: xdr::Hash = [0; 32].into();
let wasm: Vec<u8> = soroban_test_wasms::ADD_I32.into();
VmInstantiationSample { id: Some(id), wasm }
}
fn new_best_case(_host: &Host, _rng: &mut StdRng) -> VmInstantiationSample {
let id: xdr::Hash = [0; 32].into();
let wasm: Vec<u8> = soroban_test_wasms::ADD_I32.into();
VmInstantiationSample {
id: Some(id),
wasm,
cost_inputs: None,
}
}

fn new_worst_case(_host: &Host, _rng: &mut StdRng, input: u64) -> VmInstantiationSample {
let id: xdr::Hash = [0; 32].into();
// generate a test wasm contract with many trivial internal functions,
// which represents the worst case in terms of work needed for WASM parsing.
let n = (Self::INPUT_BASE_SIZE + input * 30) as usize;
let wasm = wasm_module_with_n_internal_funcs(n);
// replace the above two lines with these below to test with wasm contracts
// with a single function of many instructions. In both tests the cpu grows
// linearly with the contract size however the slopes are very different.
// let n = (input * 50) as usize;
// let wasm = wasm_module_with_4n_insns(n);
VmInstantiationSample { id: Some(id), wasm }
}
fn new_worst_case(host: &Host, _rng: &mut StdRng, input: u64) -> VmInstantiationSample {
let id: xdr::Hash = [0; 32].into();
let n = (Self::INPUT_BASE_SIZE + input) as usize;
let wasm = $BUILD(n);
let cost_inputs = if $HAS_INPUTS {
let vm = Vm::new(host, id.clone(), &wasm[..], None).unwrap();
Some(vm.get_contract_code_cost_inputs().unwrap())
} else {
None
};
VmInstantiationSample {
id: Some(id),
wasm,
cost_inputs,
}
}

fn new_random_case(_host: &Host, rng: &mut StdRng, _input: u64) -> VmInstantiationSample {
let id: xdr::Hash = [0; 32].into();
let idx = rng.gen_range(0..=10) % util::TEST_WASMS.len();
let wasm = util::TEST_WASMS[idx].into();
VmInstantiationSample { id: Some(id), wasm }
}
fn new_random_case(
host: &Host,
rng: &mut StdRng,
_input: u64,
) -> VmInstantiationSample {
let id: xdr::Hash = [0; 32].into();
let idx = rng.gen_range(0..=10) % util::TEST_WASMS.len();
let wasm: Vec<u8> = util::TEST_WASMS[idx].into();
let cost_inputs = if $HAS_INPUTS {
let vm = Vm::new(host, id.clone(), &wasm[..], None).unwrap();
Some(vm.get_contract_code_cost_inputs().unwrap())
} else {
None
};
VmInstantiationSample {
id: Some(id),
wasm,
cost_inputs,
}
}
}
};
}

impl_measurement_for_instantiation_cost_type!(
VmInstantiationRun,
VmInstantiationMeasure,
wasm_module_with_n_internal_funcs,
false
);

impl_measurement_for_instantiation_cost_type!(
VmInstantiationInstructionsRun,
VmInstantiationInstructionsMeasure,
wasm_module_with_n_insns,
true
);
impl_measurement_for_instantiation_cost_type!(
VmInstantiationFunctionsRun,
VmInstantiationFunctionsMeasure,
wasm_module_with_n_internal_funcs,
true
);
impl_measurement_for_instantiation_cost_type!(
VmInstantiationGlobalsRun,
VmInstantiationGlobalsMeasure,
wasm_module_with_n_globals,
true
);
impl_measurement_for_instantiation_cost_type!(
VmInstantiationTableEntriesRun,
VmInstantiationTableEntriesMeasure,
wasm_module_with_n_table_entries,
true
);
impl_measurement_for_instantiation_cost_type!(
VmInstantiationTypesRun,
VmInstantiationTypesMeasure,
wasm_module_with_n_types,
true
);
impl_measurement_for_instantiation_cost_type!(
VmInstantiationDataSegmentsRun,
VmInstantiationDataSegmentsMeasure,
wasm_module_with_n_data_segments,
true
);
impl_measurement_for_instantiation_cost_type!(
VmInstantiationElemSegmentsRun,
VmInstantiationElemSegmentsMeasure,
wasm_module_with_n_elem_segments,
true
);
impl_measurement_for_instantiation_cost_type!(
VmInstantiationImportsRun,
VmInstantiationImportsMeasure,
wasm_module_with_n_imports,
true
);
impl_measurement_for_instantiation_cost_type!(
VmInstantiationExportsRun,
VmInstantiationExportsMeasure,
wasm_module_with_n_exports,
true
);
Loading

0 comments on commit 21f4af5

Please sign in to comment.