Skip to content

core/state: reduce allocation in updateStateObject #31861

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

Merged
merged 1 commit into from
May 20, 2025

Conversation

MariusVanDerWijden
Copy link
Member

I couldn't really believe it myself, but turns out this saves us one heap allocation

goos: linux
goarch: amd64
pkg: github.com/ethereum/go-ethereum/core/state
cpu: Intel(R) Core(TM) Ultra 7 155U
                     │ /tmp/old.txt │            /tmp/new.txt             │
                     │    sec/op    │   sec/op     vs base                │
UpdateStateObject-14   782.6n ± 19%   674.1n ± 6%  -13.86% (p=0.002 n=10)

                     │ /tmp/old.txt │           /tmp/new.txt            │
                     │     B/op     │    B/op     vs base               │
UpdateStateObject-14     368.0 ± 0%   344.0 ± 0%  -6.52% (p=0.000 n=10)

                     │ /tmp/old.txt │            /tmp/new.txt            │
                     │  allocs/op   │ allocs/op   vs base                │
UpdateStateObject-14     8.000 ± 0%   7.000 ± 0%  -12.50% (p=0.000 n=10)

Benchmark:

func BenchmarkUpdateStateObject(b *testing.B) {
	db := NewDatabaseForTesting()
	orig, _ := New(types.EmptyRootHash, db)

	// Fill up the initial states
	obj := orig.getOrNewStateObject(common.BytesToAddress([]byte{}))
	obj.AddBalance(uint256.NewInt(uint64(1)))
	obj.data.Root = common.HexToHash("0xdeadbeef")
	for range b.N {
		orig.updateStateObject(obj)
	}
}

@fjl
Copy link
Contributor

fjl commented May 19, 2025

Ahh, right, so it saves an allocation because the variable escapes to heap when passed as any to fmt.Errorf.

@fjl fjl changed the title core/state: reduce allocations in updateStateObject core/state: reduce allocation in updateStateObject May 20, 2025
Copy link
Contributor

@s1na s1na left a comment

Choose a reason for hiding this comment

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

LGTM

@s1na s1na merged commit 63740b7 into ethereum:master May 20, 2025
3 of 4 checks passed
@s1na s1na added this to the 1.15.12 milestone May 20, 2025
Dargon789 pushed a commit to Dargon789/go-ethereum that referenced this pull request May 27, 2025
Optimize updateStateObject to reduce an allocation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants