Skip to content

profile: optimize Parse allocs #951

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion profile/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ package profile
import (
"errors"
"fmt"
"slices"
)

type buffer struct {
Expand Down Expand Up @@ -187,6 +188,19 @@ func le32(p []byte) uint32 {
return uint32(p[0]) | uint32(p[1])<<8 | uint32(p[2])<<16 | uint32(p[3])<<24
}

func decodeVarintsCount(data []byte) (n int) {
Copy link
Collaborator

@aalexand aalexand Aug 17, 2025

Choose a reason for hiding this comment

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

nit: Can this be written a bit shorter as

func peekNumVarints(data []byte) int {
	for numVarints := 0; len(data) > 0; numVarints++ {
		var err error
		if _, data, err = decodeVarint(data); err != nil {
			break
		}
	}
	return numVarints
}

valuesCount := 0
for len(data) > 0 {
var err error
if _, data, err = decodeVarint(data); err != nil {
return valuesCount
}
valuesCount++
}

return valuesCount
}

func decodeVarint(data []byte) (uint64, []byte, error) {
var u uint64
for i := 0; ; i++ {
Expand Down Expand Up @@ -286,6 +300,9 @@ func decodeInt64(b *buffer, x *int64) error {
func decodeInt64s(b *buffer, x *[]int64) error {
if b.typ == 2 {
// Packed encoding
dataLen := decodeVarintsCount(b.data)
*x = slices.Grow(*x, dataLen)

data := b.data
for len(data) > 0 {
var u uint64
Expand Down Expand Up @@ -316,8 +333,11 @@ func decodeUint64(b *buffer, x *uint64) error {

func decodeUint64s(b *buffer, x *[]uint64) error {
if b.typ == 2 {
data := b.data
// Packed encoding
dataLen := decodeVarintsCount(b.data)
*x = slices.Grow(*x, dataLen)

data := b.data
for len(data) > 0 {
var u uint64
var err error
Expand Down