Skip to content

Commit cb1f0e0

Browse files
committed
Add cost function docs
1 parent d27b25c commit cb1f0e0

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

packages/vm/src/wasm_backend/engine.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use super::limiting_tunables::LimitingTunables;
1717
/// https://github.com/WebAssembly/memory64/blob/master/proposals/memory64/Overview.md
1818
const MAX_WASM_PAGES: u32 = 65536;
1919

20+
// This function is hashed and put into the `module_version_discriminator` because it is used as
21+
// part of the compilation process. If it changes, modules need to be recompiled.
2022
#[hash_function(const_name = "COST_FUNCTION_HASH")]
2123
fn cost(operator: &Operator) -> u64 {
2224
// A flat fee for each operation
@@ -25,13 +27,18 @@ fn cost(operator: &Operator) -> u64 {
2527
// In https://github.com/CosmWasm/cosmwasm/pull/1042 a profiler is developed to
2628
// identify runtime differences between different Wasm operation, but this is not yet
2729
// precise enough to derive insights from it.
28-
//
29-
// Please note that any changes to this function need to be accompanied by a bump of
30-
// `MODULE_SERIALIZATION_VERSION` to avoid cached modules from using different amounts of gas
31-
// compared to newly compiled ones.
3230
const GAS_PER_OPERATION: u64 = 115;
3331

3432
if is_accounting(operator) {
33+
// Accounting operators are operators where the `Metering` middleware injects instructions
34+
// to count the gas usage and check for gas exhaustion. Therefore they are more expensive.
35+
//
36+
// Benchmarks show that the overhead is about 14 times the cost of a normal operation.
37+
// To benchmark this, set `GAS_PER_OPERATION = 100` and run the "infinite loop" and
38+
// "argon2" benchmarks. From the "Gas used" output, you can calculate the number of
39+
// operations and from that together with the run time the expected gas value per operation:
40+
// GAS_PER_OP = GAS_TARGET_PER_SEC / (NUM_OPS / RUNTIME_IN_SECS)
41+
// This is repeated with different multipliers to bring the two benchmarks closer together.
3542
GAS_PER_OPERATION * 14
3643
} else {
3744
GAS_PER_OPERATION

0 commit comments

Comments
 (0)