Skip to content

Commit fef6ccd

Browse files
author
Noam Preil
committed
guid: kill indirection, use == operator for Equal()
This just... works. And is slightly faster than unsafe conversion, too.
1 parent b416779 commit fef6ccd

File tree

2 files changed

+50
-12
lines changed

2 files changed

+50
-12
lines changed

sttp/guid/Guid.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"encoding/binary"
2828
"errors"
2929
"strconv"
30-
"unsafe"
3130

3231
"github.com/google/uuid"
3332
)
@@ -45,25 +44,17 @@ func New() Guid {
4544

4645
// IsZero determines if the Guid value is its zero value, i.e., empty.
4746
func (g Guid) IsZero() bool {
48-
return Equal(g, Empty)
47+
return g == Empty
4948
}
5049

5150
// Equal returns true if this Guid and other Guid values are equal.
5251
func (g Guid) Equal(other Guid) bool {
53-
a1 := (*uint64)(unsafe.Pointer(&g[0]))
54-
a2 := (*uint64)(unsafe.Pointer(&g[8]))
55-
b1 := (*uint64)(unsafe.Pointer(&other[0]))
56-
b2 := (*uint64)(unsafe.Pointer(&other[8]))
57-
return *a1 == *b1 && *a2 == *b2
52+
return g == other
5853
}
5954

6055
// Equal returns true if the a and b Guid values are equal.
6156
func Equal(a, b Guid) bool {
62-
a1 := (*uint64)(unsafe.Pointer(&a[0]))
63-
a2 := (*uint64)(unsafe.Pointer(&a[8]))
64-
b1 := (*uint64)(unsafe.Pointer(&b[0]))
65-
b2 := (*uint64)(unsafe.Pointer(&b[8]))
66-
return *a1 == *b1 && *a2 == *b2
57+
return a == b
6758
}
6859

6960
// Compare returns an integer comparing this Guid (g) to other Guid. The result will be 0 if g==other, -1 if this g < other, and +1 if g > other.

sttp/guid/Guid_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,53 @@ func testGuidToFromBytes(g Guid, gs string, swapEndianness bool, t *testing.T) {
310310
}
311311
}
312312

313+
func (g Guid) EqualIndirectBaseline(other Guid) bool {
314+
return EqualBaseline(g, other)
315+
}
316+
317+
func (g Guid) EqualIndirect(other Guid) bool {
318+
return Equal(g, other)
319+
}
320+
321+
func EqualBaseline(a, b Guid) bool {
322+
for k := range 16 {
323+
if a[k] != b[k] {
324+
return false
325+
break
326+
}
327+
}
328+
return true
329+
330+
}
331+
332+
func BenchmarkEqualityIndirect(b *testing.B) {
333+
list := []string{gs1, gs2, gs3, gs4, gs5, gs6, gsz}
334+
glist := [7]Guid{}
335+
for i := range list {
336+
glist[i], _ = Parse(list[i])
337+
}
338+
b.ResetTimer()
339+
for range b.N {
340+
a, b := glist[0], glist[1]
341+
equal := a.EqualIndirect(b)
342+
_ = equal
343+
}
344+
}
345+
346+
func BenchmarkEqualityIndirectBaseline(b *testing.B) {
347+
list := []string{gs1, gs2, gs3, gs4, gs5, gs6, gsz}
348+
glist := [7]Guid{}
349+
for i := range list {
350+
glist[i], _ = Parse(list[i])
351+
}
352+
b.ResetTimer()
353+
for range b.N {
354+
a, b := glist[0], glist[1]
355+
equal := a.EqualIndirectBaseline(b)
356+
_ = equal
357+
}
358+
}
359+
313360
func BenchmarkEqualityBaseline(b *testing.B) {
314361
list := []string{gs1, gs2, gs3, gs4, gs5, gs6, gsz}
315362
glist := [7]Guid{}

0 commit comments

Comments
 (0)