Skip to content

metadata.FromOutgoingContext is too slow #8860

@maralla

Description

@maralla

The pr #6765 made the metadata.FromOutgoingContextRaw to be internal only. And now the only way to get outgoing metdata is metadata.FromOutgoingContext. But here is the benchmark:

goos: linux
goarch: amd64
pkg: md
cpu: 11th Gen Intel(R) Core(TM) i7-11700 @ 2.50GHz
BenchmarkFromOutgoingContext-16          1000000              1172 ns/op             496 B/op          8 allocs/op
BenchmarkFromOutgoingContextRaw-16      288412128                3.989 ns/op           0 B/op          0 allocs/op
PASS
ok      md      2.753s

from the following code:

func prepareData() context.Context {
        ctx := metadata.NewOutgoingContext(context.TODO(), metadata.New(map[string]string{
                "foo-bar":                              "bar",
                "61e61f68-8a36-4ca8-b993-079210836049": "123",
                "bfe9968be134":                         "foo",
                "36ad":                                 "bar",
        }))

        ctx = metadata.AppendToOutgoingContext(ctx, "df63a9e9", "1")
        ctx = metadata.AppendToOutgoingContext(ctx, "350e070d4958", "2")

        return ctx
}

func BenchmarkFromOutgoingContext(b *testing.B) {
        ctx := prepareData()

        b.ReportAllocs()

        var md metadata.MD

        for i := 0; i < b.N; i++ {
                md, _ = metadata.FromOutgoingContext(ctx)
        }

        if md.Get("foo-bar")[0] != "bar" {
                b.Error(md)
        }
}

func BenchmarkFromOutgoingContextRaw(b *testing.B) {
        ctx := prepareData()

        b.ReportAllocs()

        var md metadata.MD

        for i := 0; i < b.N; i++ {
                md, _, _ = metadata.FromOutgoingContextRaw(ctx)
        }

        if md.Get("foo-bar")[0] != "bar" {
                b.Error(md)
        }
}

Compare to FromOutgoingContextRaw the function FromOutgoingContext is very slow which makes it no easy way to get metadata with no overhead. Our projects (internal only) rely heavily on metadata handling. I'm asking, do we have any chance to add back the "raw" function? Or, does it have any plan to make FromOutgoingContext fast?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions