Skip to content

Commit

Permalink
Io Gas adjustments
Browse files Browse the repository at this point in the history
1. read bytes charged in 4k steps
2. remove per write op free quota, while make per byte write cheaper

tune write gas numbers
  • Loading branch information
msmouse committed Dec 13, 2023
1 parent ce30868 commit 3c0ca5c
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
"state_change_hash": "",
"event_root_hash": "",
"state_checkpoint_hash": null,
"gas_used": "6",
"gas_used": "7",
"success": true,
"vm_status": "Executed successfully",
"accumulator_root_hash": "",
Expand Down Expand Up @@ -280,7 +280,7 @@
"io_gas_units": "2",
"storage_fee_octas": "0",
"storage_fee_refund_octas": "0",
"total_charge_gas_units": "6"
"total_charge_gas_units": "7"
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
"state_change_hash": "",
"event_root_hash": "",
"state_checkpoint_hash": null,
"gas_used": "6",
"gas_used": "7",
"success": true,
"vm_status": "Executed successfully",
"accumulator_root_hash": "",
Expand Down Expand Up @@ -285,7 +285,7 @@
"io_gas_units": "2",
"storage_fee_octas": "0",
"storage_fee_refund_octas": "0",
"total_charge_gas_units": "6"
"total_charge_gas_units": "7"
}
}
],
Expand Down Expand Up @@ -412,7 +412,7 @@
"state_change_hash": "",
"event_root_hash": "",
"state_checkpoint_hash": null,
"gas_used": "6",
"gas_used": "7",
"success": true,
"vm_status": "Executed successfully",
"accumulator_root_hash": "",
Expand Down Expand Up @@ -578,7 +578,7 @@
"io_gas_units": "2",
"storage_fee_octas": "0",
"storage_fee_refund_octas": "0",
"total_charge_gas_units": "6"
"total_charge_gas_units": "7"
}
}
],
Expand Down Expand Up @@ -705,7 +705,7 @@
"state_change_hash": "",
"event_root_hash": "",
"state_checkpoint_hash": null,
"gas_used": "6",
"gas_used": "7",
"success": true,
"vm_status": "Executed successfully",
"accumulator_root_hash": "",
Expand Down Expand Up @@ -871,7 +871,7 @@
"io_gas_units": "2",
"storage_fee_octas": "0",
"storage_fee_refund_octas": "0",
"total_charge_gas_units": "6"
"total_charge_gas_units": "7"
}
}
],
Expand Down Expand Up @@ -998,7 +998,7 @@
"state_change_hash": "",
"event_root_hash": "",
"state_checkpoint_hash": null,
"gas_used": "6",
"gas_used": "7",
"success": true,
"vm_status": "Executed successfully",
"accumulator_root_hash": "",
Expand Down Expand Up @@ -1164,7 +1164,7 @@
"io_gas_units": "2",
"storage_fee_octas": "0",
"storage_fee_refund_octas": "0",
"total_charge_gas_units": "6"
"total_charge_gas_units": "7"
}
}
],
Expand Down Expand Up @@ -1291,7 +1291,7 @@
"state_change_hash": "",
"event_root_hash": "",
"state_checkpoint_hash": null,
"gas_used": "6",
"gas_used": "7",
"success": true,
"vm_status": "Executed successfully",
"accumulator_root_hash": "",
Expand Down Expand Up @@ -1457,7 +1457,7 @@
"io_gas_units": "2",
"storage_fee_octas": "0",
"storage_fee_refund_octas": "0",
"total_charge_gas_units": "6"
"total_charge_gas_units": "7"
}
}
],
Expand Down Expand Up @@ -1584,7 +1584,7 @@
"state_change_hash": "",
"event_root_hash": "",
"state_checkpoint_hash": null,
"gas_used": "6",
"gas_used": "7",
"success": true,
"vm_status": "Executed successfully",
"accumulator_root_hash": "",
Expand Down Expand Up @@ -1750,7 +1750,7 @@
"io_gas_units": "2",
"storage_fee_octas": "0",
"storage_fee_refund_octas": "0",
"total_charge_gas_units": "6"
"total_charge_gas_units": "7"
}
}
],
Expand Down Expand Up @@ -1877,7 +1877,7 @@
"state_change_hash": "",
"event_root_hash": "",
"state_checkpoint_hash": null,
"gas_used": "6",
"gas_used": "7",
"success": true,
"vm_status": "Executed successfully",
"accumulator_root_hash": "",
Expand Down Expand Up @@ -2043,7 +2043,7 @@
"io_gas_units": "2",
"storage_fee_octas": "0",
"storage_fee_refund_octas": "0",
"total_charge_gas_units": "6"
"total_charge_gas_units": "7"
}
}
],
Expand Down Expand Up @@ -2170,7 +2170,7 @@
"state_change_hash": "",
"event_root_hash": "",
"state_checkpoint_hash": null,
"gas_used": "6",
"gas_used": "7",
"success": true,
"vm_status": "Executed successfully",
"accumulator_root_hash": "",
Expand Down Expand Up @@ -2336,7 +2336,7 @@
"io_gas_units": "2",
"storage_fee_octas": "0",
"storage_fee_refund_octas": "0",
"total_charge_gas_units": "6"
"total_charge_gas_units": "7"
}
}
],
Expand Down
32 changes: 22 additions & 10 deletions aptos-move/aptos-gas-schedule/src/gas_schedule/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,38 +76,50 @@ crate::gas_schedule::macros::define_gas_parameters!(
[
storage_io_per_state_slot_read: InternalGasPerArg,
{ 0..=9 => "load_data.base", 10.. => "storage_io_per_state_slot_read"},
300_000,
// At the current mainnet scale, we should assume most levels of the (hexary) JMT nodes
// in cache, hence target charging 1-2 4k-sized pages for each read. Notice the cost
// of seeking for the leaf node is covered by the first page of the "value size fee"
// (storage_io_per_state_byte_read) defined below.
800_000,

Check warning on line 83 in aptos-move/aptos-gas-schedule/src/gas_schedule/transaction.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-gas-schedule/src/gas_schedule/transaction.rs#L79-L83

Added lines #L79 - L83 were not covered by tests
],
[
storage_io_per_state_byte_read: InternalGasPerByte,
{ 0..=9 => "load_data.per_byte", 10.. => "storage_io_per_state_byte_read"},
300,
// Notice in the latest IoPricing, bytes are charged at 4k intervals (even the smallest
// read will be charged for 4KB) to reflect the assumption that every roughly 4k bytes
// might require a separate random IO upon the FS.
100,

Check warning on line 91 in aptos-move/aptos-gas-schedule/src/gas_schedule/transaction.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-gas-schedule/src/gas_schedule/transaction.rs#L88-L91

Added lines #L88 - L91 were not covered by tests
],
[load_data_failure: InternalGas, "load_data.failure", 0],
// Gas parameters for writing data to storage.
[
storage_io_per_state_slot_write: InternalGasPerArg,
{ 0..=9 => "write_data.per_op", 10.. => "storage_io_per_state_slot_write"},
300_000,
// The cost of writing down the upper level new JMT nodes are shared between transactions
// because we write down the JMT in batches, however the bottom levels will be specific
// to each transactions assuming they don't touch exactly the same leaves. It's fair to
// target roughly 1-2 full internal JMT nodes (about 0.5-1KB in total) worth of writes
// for each write op.
100_000,

Check warning on line 103 in aptos-move/aptos-gas-schedule/src/gas_schedule/transaction.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-gas-schedule/src/gas_schedule/transaction.rs#L98-L103

Added lines #L98 - L103 were not covered by tests
],
[
write_data_per_new_item: InternalGasPerArg,
"write_data.new_item",
1_280_000
legacy_write_data_per_new_item: InternalGasPerArg,
{0..=9 => "write_data.new_item"},
1_280_000,

Check warning on line 108 in aptos-move/aptos-gas-schedule/src/gas_schedule/transaction.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-gas-schedule/src/gas_schedule/transaction.rs#L106-L108

Added lines #L106 - L108 were not covered by tests
],
[
storage_io_per_state_byte_write: InternalGasPerByte,
{ 0..=9 => "write_data.per_byte_in_key", 10.. => "storage_io_per_state_byte_write"},
5_000
100,

Check warning on line 113 in aptos-move/aptos-gas-schedule/src/gas_schedule/transaction.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-gas-schedule/src/gas_schedule/transaction.rs#L113

Added line #L113 was not covered by tests
],
[
write_data_per_byte_in_val: InternalGasPerByte,
"write_data.per_byte_in_val",
legacy_write_data_per_byte_in_val: InternalGasPerByte,
{ 0..=9 => "write_data.per_byte_in_val" },

Check warning on line 117 in aptos-move/aptos-gas-schedule/src/gas_schedule/transaction.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-gas-schedule/src/gas_schedule/transaction.rs#L116-L117

Added lines #L116 - L117 were not covered by tests
10_000
],
[memory_quota: AbstractValueSize, { 1.. => "memory_quota" }, 10_000_000],
[
free_write_bytes_quota: NumBytes,
legacy_free_write_bytes_quota: NumBytes,

Check warning on line 122 in aptos-move/aptos-gas-schedule/src/gas_schedule/transaction.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-gas-schedule/src/gas_schedule/transaction.rs#L122

Added line #L122 was not covered by tests
{ 5.. => "free_write_bytes_quota" },
1024, // 1KB free per state write
],
Expand Down
55 changes: 46 additions & 9 deletions aptos-move/aptos-vm-types/src/storage/io_pricing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ impl IoPricingV1 {
fn new(gas_params: &AptosGasParameters) -> Self {
Self {
write_data_per_op: gas_params.vm.txn.storage_io_per_state_slot_write,
write_data_per_new_item: gas_params.vm.txn.write_data_per_new_item,
write_data_per_new_item: gas_params.vm.txn.legacy_write_data_per_new_item,

Check warning on line 37 in aptos-move/aptos-vm-types/src/storage/io_pricing.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-vm-types/src/storage/io_pricing.rs#L37

Added line #L37 was not covered by tests
write_data_per_byte_in_key: gas_params.vm.txn.storage_io_per_state_byte_write,
write_data_per_byte_in_val: gas_params.vm.txn.write_data_per_byte_in_val,
write_data_per_byte_in_val: gas_params.vm.txn.legacy_write_data_per_byte_in_val,

Check warning on line 39 in aptos-move/aptos-vm-types/src/storage/io_pricing.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-vm-types/src/storage/io_pricing.rs#L39

Added line #L39 was not covered by tests
load_data_base: gas_params.vm.txn.storage_io_per_state_slot_read * NumArgs::new(1),
load_data_per_byte: gas_params.vm.txn.storage_io_per_state_byte_read,
load_data_failure: gas_params.vm.txn.load_data_failure,
Expand Down Expand Up @@ -120,7 +120,7 @@ impl IoPricingV2 {
0 => unreachable!("PricingV2 not applicable for feature version 0"),
1..=2 => 0.into(),
3..=4 => 1024.into(),
5.. => gas_params.vm.txn.free_write_bytes_quota,
5.. => gas_params.vm.txn.legacy_free_write_bytes_quota,

Check warning on line 123 in aptos-move/aptos-vm-types/src/storage/io_pricing.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-vm-types/src/storage/io_pricing.rs#L123

Added line #L123 was not covered by tests
}
}

Expand Down Expand Up @@ -167,7 +167,7 @@ impl IoPricingV2 {
#[derive(Debug, Clone)]
pub struct IoPricingV3 {
pub feature_version: u64,
pub free_write_bytes_quota: NumBytes,
pub legacy_free_write_bytes_quota: NumBytes,
}

impl IoPricingV3 {
Expand All @@ -183,7 +183,7 @@ impl IoPricingV3 {
let value_size = NumBytes::new(value_size);

(key_size + value_size)
.checked_sub(self.free_write_bytes_quota)
.checked_sub(self.legacy_free_write_bytes_quota)

Check warning on line 186 in aptos-move/aptos-vm-types/src/storage/io_pricing.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-vm-types/src/storage/io_pricing.rs#L186

Added line #L186 was not covered by tests
.unwrap_or(NumBytes::zero())
}

Expand All @@ -204,11 +204,45 @@ impl IoPricingV3 {
}
}

#[derive(Debug, Clone)]
pub struct IoPricingV4;

impl IoPricingV4 {
fn calculate_read_gas(
&self,
loaded: NumBytes,
) -> impl GasExpression<VMGasParameters, Unit = InternalGasUnit> {
// Round up bytes to whole pages
// TODO(gas): make PAGE_SIZE configurable
const PAGE_SIZE: u64 = 4096;

let loaded_u64: u64 = loaded.into();
let r = loaded_u64 % PAGE_SIZE;
let rounded_up = loaded_u64 + if r == 0 { 0 } else { PAGE_SIZE - r };

STORAGE_IO_PER_STATE_SLOT_READ * NumArgs::from(1)
+ STORAGE_IO_PER_STATE_BYTE_READ * NumBytes::new(rounded_up)
}

fn io_gas_per_write(
&self,
key: &StateKey,
op_size: &WriteOpSize,
) -> impl GasExpression<VMGasParameters, Unit = InternalGasUnit> {
let key_size = NumBytes::new(key.size() as u64);
let value_size = NumBytes::new(op_size.write_len().unwrap_or(0));
let size = key_size + value_size;

STORAGE_IO_PER_STATE_SLOT_WRITE * NumArgs::new(1) + STORAGE_IO_PER_STATE_BYTE_WRITE * size
}
}

#[derive(Clone, Debug)]
pub enum IoPricing {
V1(IoPricingV1),
V2(IoPricingV2),
V3(IoPricingV3),
V4(IoPricingV4),
}

impl IoPricing {
Expand All @@ -230,10 +264,11 @@ impl IoPricing {
gas_params,
)),
},
10.. => V3(IoPricingV3 {
10..=11 => V3(IoPricingV3 {
feature_version,
free_write_bytes_quota: gas_params.vm.txn.free_write_bytes_quota,
legacy_free_write_bytes_quota: gas_params.vm.txn.legacy_free_write_bytes_quota,

Check warning on line 269 in aptos-move/aptos-vm-types/src/storage/io_pricing.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-vm-types/src/storage/io_pricing.rs#L269

Added line #L269 was not covered by tests
}),
12.. => V4(IoPricingV4),
}
}

Expand All @@ -253,7 +288,8 @@ impl IoPricing {
},
)),
V2(v2) => Either::Left(v2.calculate_read_gas(bytes_loaded)),
V3(v3) => Either::Right(v3.calculate_read_gas(bytes_loaded)),
V3(v3) => Either::Right(Either::Left(v3.calculate_read_gas(bytes_loaded))),

Check warning on line 291 in aptos-move/aptos-vm-types/src/storage/io_pricing.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-vm-types/src/storage/io_pricing.rs#L291

Added line #L291 was not covered by tests
V4(v4) => Either::Right(Either::Right(v4.calculate_read_gas(bytes_loaded))),
}
}

Expand All @@ -269,7 +305,8 @@ impl IoPricing {
match self {
V1(v1) => Either::Left(v1.io_gas_per_write(key, op_size)),
V2(v2) => Either::Left(v2.io_gas_per_write(key, op_size)),
V3(v3) => Either::Right(v3.io_gas_per_write(key, op_size)),
V3(v3) => Either::Right(Either::Left(v3.io_gas_per_write(key, op_size))),

Check warning on line 308 in aptos-move/aptos-vm-types/src/storage/io_pricing.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-vm-types/src/storage/io_pricing.rs#L308

Added line #L308 was not covered by tests
V4(v4) => Either::Right(Either::Right(v4.io_gas_per_write(key, op_size))),
}
}
}
2 changes: 1 addition & 1 deletion aptos-move/aptos-vm-types/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl StorageGasParameters {
Self {
io_pricing: IoPricing::V3(IoPricingV3 {
feature_version: LATEST_GAS_FEATURE_VERSION,
free_write_bytes_quota,
legacy_free_write_bytes_quota: free_write_bytes_quota,

Check warning on line 46 in aptos-move/aptos-vm-types/src/storage/mod.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-vm-types/src/storage/mod.rs#L46

Added line #L46 was not covered by tests
}),
space_pricing: DiskSpacePricing::v1(),
change_set_configs: ChangeSetConfigs::unlimited_at_gas_feature_version(
Expand Down
2 changes: 1 addition & 1 deletion aptos-move/aptos-vm-types/src/storage/space_pricing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl DiskSpacePricing {
value_size: u64,
) -> NumBytes {
let size = NumBytes::new(key.size() as u64) + NumBytes::new(value_size);
size.checked_sub(params.free_write_bytes_quota)
size.checked_sub(params.legacy_free_write_bytes_quota)
.unwrap_or(NumBytes::zero())
}

Expand Down
1 change: 1 addition & 0 deletions aptos-move/aptos-vm/src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub(crate) fn get_gas_parameters(
let storage_gas_params =
StorageGasParameters::new(gas_feature_version, gas_params, config_storage);

// TODO(gas): Table extension utilizes IoPricing directly.
// Overwrite table io gas parameters with global io pricing.
let g = &mut gas_params.natives.table;
match gas_feature_version {
Expand Down
Loading

0 comments on commit 3c0ca5c

Please sign in to comment.