Reading repeated numeric fields through List.Get allocates once per element
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/hyperpbv0.1.3google.golang.org/protobufv1.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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di bufbuild/hyperpb-go
-
# Decoding a message-valued map entry with an omitted value failsForse già presa @MaxWang007 l’ha presa 6 giorni fa. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 55/100
bufbuild/hyperpb-go#50 ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 45/100
bufbuild/hyperpb-go#47 · 1 commento ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 45/100
bufbuild/hyperpb-go#30 · 1 commento · 1 reazione ·
Tutte le issue di bufbuild/hyperpb-go
Issue simili
-
Linux 主机 macOS 主机
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
Lee-xeo/Claude-Cage#1455 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 72/100
slavakurilyak/awesome-ai-agents#1137 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
-
Make the HTTP server's keep-alive and headers timeouts configurableForse già presa @mattgodbolt l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
compiler-explorer/compiler-explorer#9902 ·
I maintainer di solito rispondono entro 1 giorno
-
enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno