Nothing Special   »   [go: up one dir, main page]

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?

Copy link
Member

Choose a reason for hiding this comment

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

Yes please 🙏

I'm a bit worried about the new gas values, and a comparison would help us make a fair assessment of the gas bumps

@@ -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
@Kouteki Kouteki added this to the 🚀 Mainnet launch milestone Nov 11, 2024
@Kouteki Kouteki mentioned this pull request Nov 19, 2024
Copy link
Member
@zivkovicmilos zivkovicmilos left a comment

Choose a reason for hiding this comment

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

I think the PR looks fine, apart from the comments left by @thehowl, which should be addressed about the global gas meter setting in the VM keeper 🙏

I'm temporarily just putting a pause on the PR, to allow for us to discuss whether these new gas values make sense, since they are a huge change. I also want to see some more reasoning and gas comparisons, apart from #2241

If we're fine with the bumps, I am also cool with the PR moving forward as is ✅

cc @moul @notJoon

stdout OK!

# send 10000ugnot to `proxywugnot` to wrap it
gnokey maketx call -pkgpath gno.land/r/demo/proxywugnot --send "10000ugnot" -func ProxyWrap -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
Copy link
Member

Choose a reason for hiding this comment

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

Is this real? This is a huge increase in gas usage.
Do we have some kind of comparison data?

@@ -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.

Yes please 🙏

I'm a bit worried about the new gas values, and a comparison would help us make a fair assessment of the gas bumps

@Kouteki Kouteki requested a review from moul November 20, 2024 11:37
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: In Progress
Status: In Review
Development

Successfully merging this pull request may close these issues.

4 participants