Skip to content

Commit 5e7e89c

Browse files
GODRIVER-1563 Reduce allocations in bsoncore.Document.LookupErr (#376)
1 parent ec937f3 commit 5e7e89c

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

benchmark/harness.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func WrapCase(bench BenchCase) BenchFunction {
3838
return func(b *testing.B) {
3939
ctx := context.Background()
4040
b.ResetTimer()
41+
b.ReportAllocs()
4142
err := bench(ctx, b, b.N)
4243
require.NoError(b, err, "case='%s'", name)
4344
}

x/bsonx/bsoncore/document.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ func (d Document) LookupErr(key ...string) (Value, error) {
181181
if !ok {
182182
return Value{}, NewInsufficientBytesError(d, rem)
183183
}
184-
if elem.Key() != key[0] {
184+
// We use `KeyBytes` rather than `Key` to avoid a needless string alloc.
185+
if string(elem.KeyBytes()) != key[0] {
185186
continue
186187
}
187188
if len(key) > 1 {

0 commit comments

Comments
 (0)