Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cpu and store gas updates #3054

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

piux2
Copy link
Contributor

@piux2 piux2 commented Oct 31, 2024

Summary:

This PR updates the CPU and Store gas based on results from the benchmarking tool:#2241

For CPU gas, the measurement is in nanoseconds per opcode execution.
For storage gas, the measurement is in nanoseconds per byte for each type of Gno store access.

Changes:

We moved the gas meter from the underlying store to the upper Gno store to capture accurate resource consumption for VM transactions. At the same time, we retain the original gas store and gas meter for the Auth Keeper to handle regular blockchain transactions that do not necessarily involve the VM.

We also updated the gas-wanted in the integration test to reflect actual gas usage. This can serve as a flag to alert us to future changes that might increase gas assumptions.

Additional reasons for these changes include:

  • The Gno VM store processes additional complex encoding and decoding of data structures, rather than simply reading and writing bytes to disk.
  • For the above reason, we benchmarked gas for store access at the Gno store level.
  • We want to avoid consuming gas at two points for a single store access during a VM transaction.

Here are the diagrams to explain the store access gas before and after changes

Before:

image

After:

image

Contributors' checklist...
  • Added new tests
  • Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory
  • Updated the official documentation or not needed
  • No breaking changes were made, or a BREAKING CHANGE: xxx message was included in the description
  • Added references to related issues and PRs
  • Provided any useful hints for running manual tests

@github-actions github-actions bot added 📦 🤖 gnovm Issues or PRs gnovm related 📦 🌐 tendermint v2 Issues or PRs tm2 related 📦 ⛰️ gno.land Issues or PRs gno.land package related labels Oct 31, 2024
gno.land/pkg/sdk/vm/gas_test.go Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we just make the txtars all just use a -gas-wanted of 10M?

just because they'll have to be updated again. I'd want for us to keep track of "golden" gas values just in one place.

@@ -1154,127 +1154,130 @@ func (m *Machine) incrCPU(cycles int64) {
}

const (
// CPU cycles
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a reference to the benchmarks and methodology?

@@ -205,6 +205,7 @@ var gnoStoreContextKey gnoStoreContextKeyType
func (vm *VMKeeper) newGnoTransactionStore(ctx sdk.Context) gno.TransactionStore {
base := ctx.Store(vm.baseKey)
iavl := ctx.Store(vm.iavlKey)
vm.gnoStore.SetGasMeter(ctx.GasMeter())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unsafe, because vm.gnoStore is shared, and this would set the gas meter globally; ie. not scoped to the transaction.

Comment on lines +762 to +764
func (ds *defaultStore) SetGasMeter(gm store.GasMeter) {
ds.gasMeter = gm
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this ought to go in BeginTransaction, and the store shouldn't use a gas store otherwise.

Comment on lines +115 to +127
func DefaultGasConfig() GasConfig {
return GasConfig{
GasGetObject: 16, // per byte cost
GasSetObject: 16, // per byte cost
GasGetType: 52, // per byte cost
GasSetType: 52, // per byte cost
GasGetPackageRealm: 524, // per byte cost
GasSetPackageRealm: 524, // per byte cost
GasAddMemPackage: 8, // per byte cost
GasGetMemPackage: 8, // per byte cost
GasDeleteObject: 3715, // flat cost
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are constants for the VM. Can these also not be constants?

Comment on lines +178 to +181
func (c Context) Store(key store.StoreKey) store.Store {
return c.MultiStore().GetStore(key)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can understand the rename to GasStore, but I don't think we should expose a gas-less Store method, especially when getting the underlying store is still trivial and exported.

@Kouteki Kouteki added the in focus Core team is prioritizing this work label Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in focus Core team is prioritizing this work 📦 🌐 tendermint v2 Issues or PRs tm2 related 📦 ⛰️ gno.land Issues or PRs gno.land package related 📦 🤖 gnovm Issues or PRs gnovm related
Projects
Status: No status
Status: In Review
Development

Successfully merging this pull request may close these issues.

3 participants