Skip to content

Commit

Permalink
Lower storage compute costs (#12044)
Browse files Browse the repository at this point in the history
See #11925 for all the gory
details.

The major points are:
- We currently overestimate `read_base` and `has_key_base` by 150G. This
is to handle the fact that we don't actually have ReadTTN yet. Once we
have ReadTTN, we can reduce these numbers to match estimator outputs
again. But this will have to wait for a solution for RPC and archival
nodes, that currently live on flat storage. The solution might be to
enforce memtrie for them too, but it'd increase operational expenses.
- Reduction of (Write)TTN is made possible by the fact that the critical
path only has lots of (memtrie) reads, and a single write at the end of
the block. The latency cost is accounted for in `write_base`, and
RocksDB batches writes, so WriteTTN can be reasonably close to estimator
results nowadays, even though we still have to take some variance in
disk write throughput into account.
- Once this lands, we should check whether blocks become limited by
chunk witness size, or whether gas is still the limiting factor in most
cases.
- `ReadCachedTrieNode` was not reduced due to concerns about how it is
actually implemented with memtrie.
- We had to set some gas costs to `1`. This is because our current way
of accounting for compute costs is to take the gas costs, divide by the
gas cost, and multiply by the compute cost. So our current code does not
support a gas cost of 0 with a non-zero compute costs, and changing that
would require refactoring.
- All raw estimator results and graphs from which the numbers were
derived are available on #11925.

cc @Ekleog-NEAR
  • Loading branch information
Ekleog authored Sep 12, 2024
1 parent ce8ca01 commit 52b4684
Show file tree
Hide file tree
Showing 67 changed files with 266 additions and 89 deletions.
15 changes: 14 additions & 1 deletion core/parameters/res/runtime_configs/72.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
main_storage_proof_size_soft_limit: {old: 3_000_000, new: 4_000_000}
main_storage_proof_size_soft_limit: {old: 3_000_000, new: 4_000_000}
# See https://github.com/near/nearcore/pull/12044 for why the values are set to these values.
# In addition, `gas` is set to 1 for the large read variants, because we need that in actual code.
# For this to be transparent for smart contracts, the `read_base` and `read_value_byte` values were
# also reduced by 1. This means that at best contracts now pay 1 fewer gas, and at worst the same
# value.
wasm_storage_has_key_base: { old: { gas: 54_039_896_625, compute: 200_000_000_000 }, new: { gas: 54_039_896_625, compute: 158_000_000_000 } }
wasm_storage_has_key_byte: { old: 30_790_845, new: { gas: 30_790_845, compute: 10_000_000 } }
wasm_storage_read_base: { old: { gas: 56_356_845_750, compute: 200_000_000_000 }, new: { gas: 56_356_845_749, compute: 159_000_000_000 } }
wasm_storage_read_key_byte: { old: 30_952_533, new: { gas: 30_952_533, compute: 10_000_000 } }
wasm_storage_read_value_byte: { old: 5_611_005, new: { gas: 5_611_004, compute: 2_500_000 } }
wasm_storage_large_read_overhead_base: { old: 0, new: { gas: 1, compute: 41_000_000_000 } }
wasm_storage_large_read_overhead_byte: { old: 0, new: { gas: 1, compute: 3_111_005 } }
wasm_touching_trie_node: { old: { gas: 16_101_955_926, compute: 110_000_000_000 }, new: { gas: 16_101_955_926, compute: 20_000_000_000 } }
14 changes: 8 additions & 6 deletions core/parameters/res/runtime_configs/parameters.snap
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,16 @@ wasm_storage_write_base 64_196_736_000, compute: 200_
wasm_storage_write_key_byte 70_482_867
wasm_storage_write_value_byte 31_018_539
wasm_storage_write_evicted_byte 32_117_307
wasm_storage_read_base 56_356_845_750, compute: 200_000_000_000
wasm_storage_read_key_byte 30_952_533
wasm_storage_read_value_byte 5_611_005
wasm_storage_read_base 56_356_845_749, compute: 159_000_000_000
wasm_storage_read_key_byte 30_952_533, compute: 10_000_000
wasm_storage_read_value_byte 5_611_004, compute: 2_500_000
wasm_storage_large_read_overhead_base 1, compute: 41_000_000_000
wasm_storage_large_read_overhead_byte 1, compute: 3_111_005
wasm_storage_remove_base 53_473_030_500, compute: 200_000_000_000
wasm_storage_remove_key_byte 38_220_384
wasm_storage_remove_ret_value_byte 11_531_556
wasm_storage_has_key_base 54_039_896_625, compute: 200_000_000_000
wasm_storage_has_key_byte 30_790_845
wasm_storage_has_key_base 54_039_896_625, compute: 158_000_000_000
wasm_storage_has_key_byte 30_790_845, compute: 10_000_000
wasm_storage_iter_create_prefix_base 0
wasm_storage_iter_create_prefix_byte 0
wasm_storage_iter_create_range_base 0
Expand All @@ -129,7 +131,7 @@ wasm_storage_iter_create_to_byte 0
wasm_storage_iter_next_base 0
wasm_storage_iter_next_key_byte 0
wasm_storage_iter_next_value_byte 0
wasm_touching_trie_node 16_101_955_926, compute: 110_000_000_000
wasm_touching_trie_node 16_101_955_926, compute: 20_000_000_000
wasm_read_cached_trie_node 2_280_000_000
wasm_promise_and_base 1_465_013_400
wasm_promise_and_per_promise 5_452_176
Expand Down
2 changes: 2 additions & 0 deletions core/parameters/res/runtime_configs/parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ wasm_storage_write_evicted_byte: 32_117_307
wasm_storage_read_base: 56_356_845_750
wasm_storage_read_key_byte: 30_952_533
wasm_storage_read_value_byte: 5_611_005
wasm_storage_large_read_overhead_base: 0
wasm_storage_large_read_overhead_byte: 0
wasm_storage_remove_base: 53_473_030_500
wasm_storage_remove_key_byte: 38_220_384
wasm_storage_remove_ret_value_byte: 11_531_556
Expand Down
2 changes: 2 additions & 0 deletions core/parameters/res/runtime_configs/parameters_testnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ wasm_storage_write_evicted_byte: 32_117_307
wasm_storage_read_base: 56_356_845_750
wasm_storage_read_key_byte: 30_952_533
wasm_storage_read_value_byte: 5_611_005
wasm_storage_large_read_overhead_base: 0
wasm_storage_large_read_overhead_byte: 0
wasm_storage_remove_base: 53_473_030_500
wasm_storage_remove_key_byte: 38_220_384
wasm_storage_remove_ret_value_byte: 11_531_556
Expand Down
10 changes: 10 additions & 0 deletions core/parameters/src/cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ impl ExtCostsConfig {
ExtCosts::storage_read_base => SAFETY_MULTIPLIER * 18785615250,
ExtCosts::storage_read_key_byte => SAFETY_MULTIPLIER * 10317511,
ExtCosts::storage_read_value_byte => SAFETY_MULTIPLIER * 1870335,
ExtCosts::storage_large_read_overhead_base => 0,
ExtCosts::storage_large_read_overhead_byte => 0,
ExtCosts::storage_remove_base => SAFETY_MULTIPLIER * 17824343500,
ExtCosts::storage_remove_key_byte => SAFETY_MULTIPLIER * 12740128,
ExtCosts::storage_remove_ret_value_byte => SAFETY_MULTIPLIER * 3843852,
Expand Down Expand Up @@ -268,6 +270,8 @@ pub enum ExtCosts {
bls12381_p1_decompress_element = 80,
bls12381_p2_decompress_base = 81,
bls12381_p2_decompress_element = 82,
storage_large_read_overhead_base = 83,
storage_large_read_overhead_byte = 84,
}

// Type of an action, used in fees logic.
Expand Down Expand Up @@ -351,6 +355,12 @@ impl ExtCosts {
ExtCosts::storage_read_base => Parameter::WasmStorageReadBase,
ExtCosts::storage_read_key_byte => Parameter::WasmStorageReadKeyByte,
ExtCosts::storage_read_value_byte => Parameter::WasmStorageReadValueByte,
ExtCosts::storage_large_read_overhead_base => {
Parameter::WasmStorageLargeReadOverheadBase
}
ExtCosts::storage_large_read_overhead_byte => {
Parameter::WasmStorageLargeReadOverheadByte
}
ExtCosts::storage_remove_base => Parameter::WasmStorageRemoveBase,
ExtCosts::storage_remove_key_byte => Parameter::WasmStorageRemoveKeyByte,
ExtCosts::storage_remove_ret_value_byte => Parameter::WasmStorageRemoveRetValueByte,
Expand Down
2 changes: 2 additions & 0 deletions core/parameters/src/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ pub enum Parameter {
WasmStorageReadBase,
WasmStorageReadKeyByte,
WasmStorageReadValueByte,
WasmStorageLargeReadOverheadBase,
WasmStorageLargeReadOverheadByte,
WasmStorageRemoveBase,
WasmStorageRemoveKeyByte,
WasmStorageRemoveRetValueByte,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,11 @@ expression: config_view
"storage_write_key_byte": 70482867,
"storage_write_value_byte": 31018539,
"storage_write_evicted_byte": 32117307,
"storage_read_base": 56356845750,
"storage_read_base": 56356845749,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_read_value_byte": 5611004,
"storage_large_read_overhead_base": 1,
"storage_large_read_overhead_byte": 1,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,11 @@ expression: config_view
"storage_write_key_byte": 70482867,
"storage_write_value_byte": 31018539,
"storage_write_evicted_byte": 32117307,
"storage_read_base": 56356845750,
"storage_read_base": 56356845749,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_read_value_byte": 5611004,
"storage_large_read_overhead_base": 1,
"storage_large_read_overhead_byte": 1,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,11 @@ expression: config_view
"storage_write_key_byte": 70482867,
"storage_write_value_byte": 31018539,
"storage_write_evicted_byte": 32117307,
"storage_read_base": 56356845750,
"storage_read_base": 56356845749,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_read_value_byte": 5611004,
"storage_large_read_overhead_base": 1,
"storage_large_read_overhead_byte": 1,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ expression: config_view
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_large_read_overhead_base": 0,
"storage_large_read_overhead_byte": 0,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
Expand Down
Loading

0 comments on commit 52b4684

Please sign in to comment.