Reading repeated numeric fields through List.Get allocates once per element

Aperta
#49 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
1-2 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
go
Ambito
performance

Direzione di ricerca

Start with the reproducer, which uses testing.AllocsPerRun around the loop calling list.Get(i).Uint() on a packed uint64 field decoded by hyperpb. Read how the list's Get method boxes each element into a protoreflect.Value, since that is where the per-element allocation most likely happens. Save the reproducer as a benchmark or test. Done when the scan reports allocs/scan=0 and the sum is unchanged.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

Scanning an already-decoded packed uint64 field through protoreflect.List.Get(i).Uint() allocates one heap object per element in the reproducer below. The allocations occur during the scan, after unmarshalling and retrieving the list. In a scan of one million elements, they add about 8 MB of heap allocation.

Versions / platform

  • buf.build/go/hyperpb v0.1.3
  • google.golang.org/protobuf v1.36.9
  • Go 1.27.4 on darwin/arm64 (Apple M1)

Reproducer

Save as main.go in an empty directory and run the following commands.

go mod init repro
go get buf.build/go/[email protected] google.golang.org/[email protected]
go run .
package main

import (
        "fmt"
        "testing"

        "buf.build/go/hyperpb"
        "google.golang.org/protobuf/encoding/protowire"
        "google.golang.org/protobuf/proto"
        "google.golang.org/protobuf/reflect/protodesc"
        "google.golang.org/protobuf/types/descriptorpb"
)

func main() {
        const count = 256
        file, err := protodesc.NewFile(&descriptorpb.FileDescriptorProto{
                Name:   proto.String("repro.proto"),
                Syntax: proto.String("proto3"),
                MessageType: []*descriptorpb.DescriptorProto{{
                        Name: proto.String("Sample"),
                        Field: []*descriptorpb.FieldDescriptorProto{{
                                Name:   proto.String("values"),
                                Number: proto.Int32(1),
                                Label:  descriptorpb.FieldDescriptorProto_LABEL_REPEATED.Enum(),
                                Type:   descriptorpb.FieldDescriptorProto_TYPE_UINT64.Enum(),
                        }},
                }},
        }, nil)
        if err != nil {
                panic(err)
        }
        desc := file.Messages().Get(0)

        packed := make([]byte, 0, 2*count)
        for i := range count {
                packed = protowire.AppendVarint(packed, uint64(256+i))
        }
        payload := protowire.AppendTag(nil, 1, protowire.BytesType)
        payload = protowire.AppendBytes(payload, packed)

        shared := new(hyperpb.Shared)
        defer shared.Free()
        msg := shared.NewMessage(hyperpb.CompileMessageDescriptor(desc))
        if err := msg.Unmarshal(payload); err != nil {
                panic(err)
        }
        list := msg.ProtoReflect().Get(desc.Fields().ByNumber(1)).List()
        if list.Len() != count {
                panic("unexpected list length")
        }

        var sum uint64
        allocs := testing.AllocsPerRun(10, func() {
                sum = 0
                for i := range count {
                        sum += list.Get(i).Uint()
                }
        })
        fmt.Printf("sum=%d allocs/scan=%.0f\n", sum, allocs)
}

Observed

sum=98176 allocs/scan=256

Expected

The scan returns the same sum with zero heap allocations.

sum=98176 allocs/scan=0
Lingua principale
Go
Stelle
691
Fork
19
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di bufbuild/hyperpb-go

Tutte le issue di bufbuild/hyperpb-go

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.