Skip to content

Commit d23dec4

Browse files
authored
GODRIVER-2655 Optimize ObjectIDFromHex a bit. (#1125)
1 parent 43382f0 commit d23dec4

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

bson/primitive/objectid.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,12 @@ func ObjectIDFromHex(s string) (ObjectID, error) {
8282
return NilObjectID, ErrInvalidHex
8383
}
8484

85-
b, err := hex.DecodeString(s)
85+
var oid [12]byte
86+
_, err := hex.Decode(oid[:], []byte(s))
8687
if err != nil {
8788
return NilObjectID, err
8889
}
8990

90-
var oid [12]byte
91-
copy(oid[:], b)
92-
9391
return oid, nil
9492
}
9593

bson/primitive/objectid_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ func BenchmarkHex(b *testing.B) {
3535
}
3636
}
3737

38+
func BenchmarkObjectIDFromHex(b *testing.B) {
39+
id := NewObjectID().Hex()
40+
for i := 0; i < b.N; i++ {
41+
_, _ = ObjectIDFromHex(id)
42+
}
43+
}
44+
3845
func TestFromHex_RoundTrip(t *testing.T) {
3946
before := NewObjectID()
4047
after, err := ObjectIDFromHex(before.Hex())

0 commit comments

Comments
 (0)