Skip to content

rlp: improve SplitListValues allocation efficiency#33554

Merged
rjl493456442 merged 2 commits into
ethereum:masterfrom
mask-pp:improve_SplitListValues_method
Jan 18, 2026
Merged

rlp: improve SplitListValues allocation efficiency#33554
rjl493456442 merged 2 commits into
ethereum:masterfrom
mask-pp:improve_SplitListValues_method

Conversation

@mask-pp

@mask-pp mask-pp commented Jan 8, 2026

Copy link
Copy Markdown
Contributor

Improve the SplitListValues method by setting the capacity.

Benchmark code:

// splitListValues_origin extracts the raw elements from the list RLP-encoding blob.
func splitListValues_origin(b []byte) ([][]byte, error) {
	b, _, err := SplitList(b)
	if err != nil {
		return nil, err
	}
	var elements [][]byte
	for len(b) > 0 {
		_, tagsize, size, err := readKind(b)
		if err != nil {
			return nil, err
		}
		elements = append(elements, b[:tagsize+size])
		b = b[tagsize+size:]
	}
	return elements, nil
}

func BenchmarkSplitListValues(b *testing.B) {
	input := "F877A12000BF49F440A1CD0527E4D06E2765654C0F56452257516D793A9B8D604DCFDF2AB853F851808D10000000000000000000000000A056E81F171BCC55A6FF8345E692C0F86E5B48E01B996CADC001622FB5E363B421A0C5D2460186F7233C927E7DB2DCC703C0E500B653CA82273B7BFAD8045D85A470"
	data := unhex(input)

	b.Run("SplitListValues(origin)", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			splitListValues_origin(data) // warm up
		}
	})

	b.Run("SplitListValues(improved)", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			SplitListValues(data)
		}
	})
}

Result:

goos: darwin
goarch: arm64
pkg: github.com/ethereum/go-ethereum/rlp
cpu: Apple M3 Pro
BenchmarkSplitListValues
BenchmarkSplitListValues/SplitListValues(origin)
BenchmarkSplitListValues/SplitListValues(origin)-12         	12957044	        82.61 ns/op
BenchmarkSplitListValues/SplitListValues(improved)
BenchmarkSplitListValues/SplitListValues(improved)-12       	22989550	        51.51 ns/op

@mask-pp mask-pp requested a review from fjl as a code owner January 8, 2026 15:26
Copilot AI review requested due to automatic review settings January 8, 2026 15:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes the SplitListValues function by pre-allocating the result slice with the correct capacity. The optimization uses CountValues to determine the number of elements before allocation, avoiding dynamic slice growth and reallocations during append operations.

Key changes:

  • Pre-calculate element count using CountValues before allocating the slice
  • Initialize slice with capacity n to avoid reallocations
  • Remove error checking in the parsing loop (relying on prior validation)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread rlp/raw.go Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

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

Thank you.

@rjl493456442 rjl493456442 added this to the 1.17.0 milestone Jan 18, 2026
@rjl493456442 rjl493456442 changed the title rlp: improve SplitListValues method by setting capacity rlp: improve SplitListValues allocation efficiency Jan 18, 2026
@rjl493456442 rjl493456442 merged commit ef815c5 into ethereum:master Jan 18, 2026
8 of 9 checks passed
gballet pushed a commit to BZO95/go-ethereum that referenced this pull request May 21, 2026
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