Skip to content

Optimize solidity mapping perf#1221

Merged
otherview merged 7 commits into
release/hayabusafrom
pedro/hayabusa/improve_solidity_mapping_perf
Aug 4, 2025
Merged

Optimize solidity mapping perf#1221
otherview merged 7 commits into
release/hayabusafrom
pedro/hayabusa/improve_solidity_mapping_perf

Conversation

@otherview

@otherview otherview commented Aug 1, 2025

Copy link
Copy Markdown
Member

Description

This PR optimizes the generic Mapping[K, V] implementation by cutting down on allocations and CPU overhead in both Get and Set:

  • Pooled RLP buffers/readers via sync.Pool to avoid per-call allocations of bytes.Buffer and bytes.Reader.
  • Fixed double-encode bug in Set (now returns the pooled encoding instead of calling rlp.EncodeToBytes twice).
  • Nil-handling in Get now only does a single reflection check on missing keys.

Benchmark Comparison

Case Old New Δ (ns/op) Δ (allocs) Δ (bytes)
Set 677 ns/op · 16 allocs · 475 B/op 550 ns/op · 11 allocs · 344 B/op –127 ns (~19%) –5 –131 B
GetEmpty 322 ns/op · 5 allocs · 168 B/op 313 ns/op · 5 allocs · 168 B/op –9 ns 0 0
GetNonEmpty 540 ns/op · 11 allocs · 416 B/op 535 ns/op · 10 allocs · 368 B/op –5 ns –1 –48 B

Benchmark test

// mapping_bench_test.go
package solidity

import (
	"github.com/vechain/thor/v2/muxdb"
	"github.com/vechain/thor/v2/state"
	"github.com/vechain/thor/v2/trie"
	"math/big"
	"testing"

	"github.com/vechain/thor/v2/thor"
)

// stringKey implements the Key interface
type stringKey string

func (k stringKey) Bytes() []byte { return []byte(k) }

// ComplexVal is a sample value type containing *big.Int, uint32, and thor.Address
type ComplexVal struct {
	N *big.Int
	U uint32
	A thor.Address
}

func BenchmarkMapping(b *testing.B) {
	// 1) spin up an in-memory state.State (so you don’t hit disk or a real trie DB)
	//    adjust these calls to whatever the real constructor is
	st := state.New(muxdb.NewMem(), trie.Root{})

	// 2) you can pass `nil` for the charger if you don’t care about gas accounting
	ctx := NewContext(thor.Address{}, st, nil)

	m := NewMapping[stringKey, ComplexVal](ctx, thor.Bytes32{})

	key := stringKey("bench")
	missingKey := stringKey("missing-key")
	val := ComplexVal{}

	b.Run("Set", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			if err := m.Set(key, val, true); err != nil {
				b.Fatal(err)
			}
		}
	})

	b.Run("GetEmpty", func(b *testing.B) {
		b.ResetTimer()
		for i := 0; i < b.N; i++ {
			if _, err := m.Get(missingKey); err != nil {
				b.Fatal(err)
			}
		}
	})

	// preload
	if err := m.Set(key, val, true); err != nil {
		b.Fatal(err)
	}

	b.Run("Get", func(b *testing.B) {
		b.ResetTimer()
		for i := 0; i < b.N; i++ {
			if _, err := m.Get(key); err != nil {
				b.Fatal(err)
			}
		}
	})
}

Fixes # (issue)

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • New and existing E2E tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • I have not added any vulnerable dependencies to my code

@otherview
otherview requested a review from a team as a code owner August 1, 2025 13:20
@codecov-commenter

codecov-commenter commented Aug 1, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 52.38095% with 30 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (release/hayabusa@e9bed70). Learn more about missing BASE report.

Files with missing lines Patch % Lines
builtin/staker/aggregation/service.go 0.00% 12 Missing ⚠️
builtin/staker/validation/repository.go 18.18% 8 Missing and 1 partial ⚠️
builtin/solidity/mapping.go 86.11% 4 Missing and 1 partial ⚠️
builtin/staker/delegation/service.go 0.00% 4 Missing ⚠️
Additional details and impacted files
@@                 Coverage Diff                 @@
##             release/hayabusa    #1221   +/-   ##
===================================================
  Coverage                    ?   58.34%           
===================================================
  Files                       ?      273           
  Lines                       ?    28978           
  Branches                    ?        0           
===================================================
  Hits                        ?    16907           
  Misses                      ?    10627           
  Partials                    ?     1444           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@otherview
otherview force-pushed the pedro/hayabusa/improve_solidity_mapping_perf branch from a4da1bf to 1ae4bdc Compare August 1, 2025 13:26
@otherview
otherview force-pushed the pedro/hayabusa/improve_solidity_mapping_perf branch from 4e49a56 to d06e95f Compare August 1, 2025 14:19
Comment thread builtin/solidity/mapping.go Outdated
Comment thread builtin/solidity/mapping.go Outdated

@paologalligit paologalligit left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good, left just a minor comment you can ignore

Comment thread builtin/solidity/mapping.go Outdated
@otherview
otherview merged commit a76daa6 into release/hayabusa Aug 4, 2025
25 checks passed
@otherview
otherview deleted the pedro/hayabusa/improve_solidity_mapping_perf branch August 4, 2025 14:30
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.

5 participants