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

Đang mở
#49 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
go
Lĩnh vực
performance

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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
Ngôn ngữ chính
Go
Star
691
Fork
19
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của bufbuild/hyperpb-go

Tất cả issue của bufbuild/hyperpb-go

Issue tương tự

Thêm issue về Go

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.