Skip to content

Commit

Permalink
Module cache followup (#1375)
Browse files Browse the repository at this point in the history
This is a set of followups to #1359 to cover missing testing and wiring
around the module cache. It includes calibration of the
`VmCachedInstantiation` cost type, a shift to using a single shared
linker when using a module cache (this turns out to be critical -- the
existing code deadlocks on cross-contract calls) and some fairly subtle
wiring to patch up costs in recording mode, along with some tests to
check that it's mostly working.

Several tests remain broken with --feature=next, so I wouldn't say it's
quite ready yet. I tried, but am out of time for tonight.
  • Loading branch information
graydon authored Apr 2, 2024
1 parent c03cc14 commit a1cb3fd
Show file tree
Hide file tree
Showing 29 changed files with 1,049 additions and 303 deletions.
6 changes: 2 additions & 4 deletions soroban-env-host/benches/common/cost_types/vm_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use soroban_env_host::{
vm::{ParsedModule, VersionedContractCodeCostInputs},
xdr, Host,
};
use std::rc::Rc;

// Protocol 20 coarse cost model.
pub(crate) struct VmInstantiationMeasure;
Expand Down Expand Up @@ -40,10 +39,9 @@ macro_rules! impl_measurement_for_instantiation_cost_type {
.unwrap(),
)
}
let module = Rc::new(
let module =
ParsedModule::new_with_isolated_engine(_host, &wasm, cost_inputs.clone())
.unwrap(),
);
.unwrap();
VmInstantiationSample {
id: Some(id),
wasm,
Expand Down
10 changes: 6 additions & 4 deletions soroban-env-host/benches/worst_case_linear_models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ fn write_budget_params_code(
ty,
);
}
#[cfg(not(feature = "next"))]
ContractCostType::VmCachedInstantiation => {
println!(
"
Expand All @@ -230,7 +231,7 @@ fn write_budget_params_code(
cpu.const_term, cpu.lin_term
),
None => println!(
"ContractCostType::VmCachedInstantiation => !todo()"
"ContractCostType::VmCachedInstantiation => todo!()"
),
}
}
Expand All @@ -239,7 +240,7 @@ fn write_budget_params_code(
"ContractCostType::{:?} => {{ cpu.const_term = {}; cpu.lin_term = {:?}; }}",
ty, cpu.const_term, cpu.lin_term
),
None => println!("ContractCostType::VmCachedInstantiation => !todo()"),
None => println!("ContractCostType::{:?} => todo!()", ty),
},
}
}
Expand Down Expand Up @@ -292,6 +293,7 @@ fn write_budget_params_code(
ty
)
}
#[cfg(not(feature = "next"))]
ContractCostType::VmCachedInstantiation => {
println!(
"
Expand All @@ -305,7 +307,7 @@ fn write_budget_params_code(
mem.const_term, mem.lin_term
),
None => println!(
"ContractCostType::VmCachedInstantiation => !todo()"
"ContractCostType::VmCachedInstantiation => todo!()"
),
}
}
Expand All @@ -314,7 +316,7 @@ fn write_budget_params_code(
"ContractCostType::{:?} => {{ mem.const_term = {}; mem.lin_term = {:?}; }}",
ty, mem.const_term, mem.lin_term
),
None => println!("ContractCostType::VmCachedInstantiation => !todo()"),
None => println!("ContractCostType::{:?} => todo!()", ty),
},
}
}
Expand Down
63 changes: 37 additions & 26 deletions soroban-env-host/src/budget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,16 @@ impl Default for BudgetImpl {
cpu.const_term = 451626;
cpu.lin_term = ScaledU64(45405);
}
#[cfg(not(feature = "next"))]
ContractCostType::VmCachedInstantiation => {
cpu.const_term = 451626;
cpu.lin_term = ScaledU64(45405);
}
#[cfg(feature = "next")]
ContractCostType::VmCachedInstantiation => {
cpu.const_term = 41142;
cpu.lin_term = ScaledU64(634);
}
ContractCostType::InvokeVmFunction => {
cpu.const_term = 1948;
cpu.lin_term = ScaledU64(0);
Expand Down Expand Up @@ -412,73 +418,73 @@ impl Default for BudgetImpl {

#[cfg(feature = "next")]
ContractCostType::ParseWasmInstructions => {
cpu.const_term = 72736;
cpu.lin_term = ScaledU64(25420);
cpu.const_term = 73077;
cpu.lin_term = ScaledU64(25410);
}
#[cfg(feature = "next")]
ContractCostType::ParseWasmFunctions => {
cpu.const_term = 0;
cpu.lin_term = ScaledU64(536688);
cpu.lin_term = ScaledU64(540752);
}
#[cfg(feature = "next")]
ContractCostType::ParseWasmGlobals => {
cpu.const_term = 0;
cpu.lin_term = ScaledU64(176902);
cpu.lin_term = ScaledU64(176363);
}
#[cfg(feature = "next")]
ContractCostType::ParseWasmTableEntries => {
cpu.const_term = 0;
cpu.lin_term = ScaledU64(29639);
cpu.lin_term = ScaledU64(29989);
}
#[cfg(feature = "next")]
ContractCostType::ParseWasmTypes => {
cpu.const_term = 0;
cpu.lin_term = ScaledU64(1048891);
cpu.lin_term = ScaledU64(1061449);
}
#[cfg(feature = "next")]
ContractCostType::ParseWasmDataSegments => {
cpu.const_term = 0;
cpu.lin_term = ScaledU64(236970);
cpu.lin_term = ScaledU64(237336);
}
#[cfg(feature = "next")]
ContractCostType::ParseWasmElemSegments => {
cpu.const_term = 0;
cpu.lin_term = ScaledU64(317249);
cpu.lin_term = ScaledU64(328476);
}
#[cfg(feature = "next")]
ContractCostType::ParseWasmImports => {
cpu.const_term = 0;
cpu.lin_term = ScaledU64(694667);
cpu.lin_term = ScaledU64(701845);
}
#[cfg(feature = "next")]
ContractCostType::ParseWasmExports => {
cpu.const_term = 0;
cpu.lin_term = ScaledU64(427037);
cpu.lin_term = ScaledU64(429383);
}
#[cfg(feature = "next")]
ContractCostType::ParseWasmDataSegmentBytes => {
cpu.const_term = 66075;
cpu.const_term = 0;
cpu.lin_term = ScaledU64(28);
}
#[cfg(feature = "next")]
ContractCostType::InstantiateWasmInstructions => {
cpu.const_term = 25059;
cpu.const_term = 43030;
cpu.lin_term = ScaledU64(0);
}
#[cfg(feature = "next")]
ContractCostType::InstantiateWasmFunctions => {
cpu.const_term = 0;
cpu.lin_term = ScaledU64(7503);
cpu.lin_term = ScaledU64(7556);
}
#[cfg(feature = "next")]
ContractCostType::InstantiateWasmGlobals => {
cpu.const_term = 0;
cpu.lin_term = ScaledU64(10761);
cpu.lin_term = ScaledU64(10711);
}
#[cfg(feature = "next")]
ContractCostType::InstantiateWasmTableEntries => {
cpu.const_term = 0;
cpu.lin_term = ScaledU64(3211);
cpu.lin_term = ScaledU64(3300);
}
#[cfg(feature = "next")]
ContractCostType::InstantiateWasmTypes => {
Expand All @@ -488,26 +494,26 @@ impl Default for BudgetImpl {
#[cfg(feature = "next")]
ContractCostType::InstantiateWasmDataSegments => {
cpu.const_term = 0;
cpu.lin_term = ScaledU64(16370);
cpu.lin_term = ScaledU64(23038);
}
#[cfg(feature = "next")]
ContractCostType::InstantiateWasmElemSegments => {
cpu.const_term = 0;
cpu.lin_term = ScaledU64(28309);
cpu.lin_term = ScaledU64(42488);
}
#[cfg(feature = "next")]
ContractCostType::InstantiateWasmImports => {
cpu.const_term = 0;
cpu.lin_term = ScaledU64(683461);
cpu.lin_term = ScaledU64(828974);
}
#[cfg(feature = "next")]
ContractCostType::InstantiateWasmExports => {
cpu.const_term = 0;
cpu.lin_term = ScaledU64(297065);
cpu.lin_term = ScaledU64(297100);
}
#[cfg(feature = "next")]
ContractCostType::InstantiateWasmDataSegmentBytes => {
cpu.const_term = 25191;
cpu.const_term = 0;
cpu.lin_term = ScaledU64(14);
}
}
Expand Down Expand Up @@ -569,10 +575,16 @@ impl Default for BudgetImpl {
mem.const_term = 130065;
mem.lin_term = ScaledU64(5064);
}
#[cfg(not(feature = "next"))]
ContractCostType::VmCachedInstantiation => {
mem.const_term = 130065;
mem.lin_term = ScaledU64(5064);
}
#[cfg(feature = "next")]
ContractCostType::VmCachedInstantiation => {
mem.const_term = 69472;
mem.lin_term = ScaledU64(1217);
}
ContractCostType::InvokeVmFunction => {
mem.const_term = 14;
mem.lin_term = ScaledU64(0);
Expand Down Expand Up @@ -652,7 +664,7 @@ impl Default for BudgetImpl {
#[cfg(feature = "next")]
ContractCostType::ParseWasmImports => {
mem.const_term = 0;
mem.lin_term = ScaledU64(102890);
mem.lin_term = ScaledU64(103229);
}
#[cfg(feature = "next")]
ContractCostType::ParseWasmExports => {
Expand All @@ -661,13 +673,12 @@ impl Default for BudgetImpl {
}
#[cfg(feature = "next")]
ContractCostType::ParseWasmDataSegmentBytes => {
mem.const_term = 17580;
mem.const_term = 0;
mem.lin_term = ScaledU64(257);
}

#[cfg(feature = "next")]
ContractCostType::InstantiateWasmInstructions => {
mem.const_term = 70192;
mem.const_term = 70704;
mem.lin_term = ScaledU64(0);
}
#[cfg(feature = "next")]
Expand Down Expand Up @@ -703,7 +714,7 @@ impl Default for BudgetImpl {
#[cfg(feature = "next")]
ContractCostType::InstantiateWasmImports => {
mem.const_term = 0;
mem.lin_term = ScaledU64(77273);
mem.lin_term = ScaledU64(97637);
}
#[cfg(feature = "next")]
ContractCostType::InstantiateWasmExports => {
Expand All @@ -712,7 +723,7 @@ impl Default for BudgetImpl {
}
#[cfg(feature = "next")]
ContractCostType::InstantiateWasmDataSegmentBytes => {
mem.const_term = 69256;
mem.const_term = 0;
mem.lin_term = ScaledU64(126);
}
}
Expand Down
4 changes: 2 additions & 2 deletions soroban-env-host/src/cost_runner/cost_types/vm_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ mod v21 {
_iter: u64,
sample: Self::SampleType,
) -> Self::RecycledType {
let module = black_box(Rc::new(
let module = black_box(
ParsedModule::new(
host,
sample.module.module.engine(),
&sample.wasm[..],
sample.module.cost_inputs.clone(),
)
.unwrap(),
));
);
(Some(module), sample.wasm)
}

Expand Down
34 changes: 33 additions & 1 deletion soroban-env-host/src/e2e_testutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ pub fn wasm_entry(wasm: &[u8]) -> LedgerEntry {
}))
}

#[cfg(feature = "next")]
pub fn wasm_entry_with_refined_contract_cost_inputs(wasm: &[u8]) -> LedgerEntry {
use crate::xdr::ContractCodeEntryV1;
let host = crate::Host::default();
ledger_entry(LedgerEntryData::ContractCode(ContractCodeEntry {
ext: ContractCodeEntryExt::V1(ContractCodeEntryV1 {
ext: ExtensionPoint::V0,
cost_inputs: crate::vm::ParsedModule::extract_refined_contract_cost_inputs(&host, wasm)
.unwrap(),
}),
hash: get_wasm_hash(wasm).try_into().unwrap(),
code: wasm.try_into().unwrap(),
}))
}

pub fn default_ledger_info() -> LedgerInfo {
LedgerInfo {
protocol_version: 20,
Expand All @@ -84,6 +99,13 @@ pub struct CreateContractData {

impl CreateContractData {
pub fn new(salt: [u8; 32], wasm: &[u8]) -> Self {
Self::new_with_refined_contract_cost_inputs(salt, wasm, false)
}
pub fn new_with_refined_contract_cost_inputs(
salt: [u8; 32],
wasm: &[u8],
_refined_cost_inputs: bool,
) -> Self {
let deployer = get_account_id([123; 32]);
let contract_id_preimage = get_contract_id_preimage(&deployer, &salt);

Expand Down Expand Up @@ -114,10 +136,20 @@ impl CreateContractData {
}),
}));

#[cfg(not(feature = "next"))]
let wasm_entry = wasm_entry(wasm);

#[cfg(feature = "next")]
let wasm_entry = if _refined_cost_inputs {
wasm_entry_with_refined_contract_cost_inputs(wasm)
} else {
wasm_entry(wasm)
};

Self {
deployer,
wasm_key: get_wasm_key(wasm),
wasm_entry: wasm_entry(wasm),
wasm_entry,
contract_key,
contract_entry,
contract_address,
Expand Down
Loading

0 comments on commit a1cb3fd

Please sign in to comment.