Skip to content

Commit 1a3ce0f

Browse files
committed
Reduce repetition in binary to hex conversion
This version runs ~5% slower. Signed-off-by: Bryan Boreham <[email protected]>
1 parent a831909 commit 1a3ce0f

File tree

1 file changed

+18
-40
lines changed

1 file changed

+18
-40
lines changed

trace/trace.go

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -46,37 +46,17 @@ func (t TraceID) IsValid() bool {
4646
// as a hex string.
4747
func (t TraceID) MarshalJSON() ([]byte, error) {
4848
b := [32 + 2]byte{0: '"', 33: '"'}
49-
h := t.hexBytes()
50-
copy(b[1:], h[:])
49+
binTohex8(t[0:8], b[1:17])
50+
binTohex8(t[8:16], b[17:33])
5151
return b[:], nil
5252
}
5353

5454
// String returns the hex string representation form of a TraceID.
5555
func (t TraceID) String() string {
56-
h := t.hexBytes()
57-
return string(h[:])
58-
}
59-
60-
// hexBytes returns the hex string representation form of a TraceID.
61-
func (t TraceID) hexBytes() [32]byte {
62-
return [32]byte{
63-
hexLU[t[0x0]>>4], hexLU[t[0x0]&0xf],
64-
hexLU[t[0x1]>>4], hexLU[t[0x1]&0xf],
65-
hexLU[t[0x2]>>4], hexLU[t[0x2]&0xf],
66-
hexLU[t[0x3]>>4], hexLU[t[0x3]&0xf],
67-
hexLU[t[0x4]>>4], hexLU[t[0x4]&0xf],
68-
hexLU[t[0x5]>>4], hexLU[t[0x5]&0xf],
69-
hexLU[t[0x6]>>4], hexLU[t[0x6]&0xf],
70-
hexLU[t[0x7]>>4], hexLU[t[0x7]&0xf],
71-
hexLU[t[0x8]>>4], hexLU[t[0x8]&0xf],
72-
hexLU[t[0x9]>>4], hexLU[t[0x9]&0xf],
73-
hexLU[t[0xa]>>4], hexLU[t[0xa]&0xf],
74-
hexLU[t[0xb]>>4], hexLU[t[0xb]&0xf],
75-
hexLU[t[0xc]>>4], hexLU[t[0xc]&0xf],
76-
hexLU[t[0xd]>>4], hexLU[t[0xd]&0xf],
77-
hexLU[t[0xe]>>4], hexLU[t[0xe]&0xf],
78-
hexLU[t[0xf]>>4], hexLU[t[0xf]&0xf],
79-
}
56+
var b [32]byte
57+
binTohex8(t[0:8], b[0:16])
58+
binTohex8(t[8:16], b[16:32])
59+
return string(b[:])
8060
}
8161

8262
// SpanID is a unique identity of a span in a trace.
@@ -97,28 +77,26 @@ func (s SpanID) IsValid() bool {
9777
// as a hex string.
9878
func (s SpanID) MarshalJSON() ([]byte, error) {
9979
b := [16 + 2]byte{0: '"', 17: '"'}
100-
h := s.hexBytes()
101-
copy(b[1:], h[:])
80+
binTohex8(s[:], b[1:16])
10281
return b[:], nil
10382
}
10483

10584
// String returns the hex string representation form of a SpanID.
10685
func (s SpanID) String() string {
107-
b := s.hexBytes()
86+
var b [16]byte
87+
binTohex8(s[:], b[:])
10888
return string(b[:])
10989
}
11090

111-
func (s SpanID) hexBytes() [16]byte {
112-
return [16]byte{
113-
hexLU[s[0]>>4], hexLU[s[0]&0xf],
114-
hexLU[s[1]>>4], hexLU[s[1]&0xf],
115-
hexLU[s[2]>>4], hexLU[s[2]&0xf],
116-
hexLU[s[3]>>4], hexLU[s[3]&0xf],
117-
hexLU[s[4]>>4], hexLU[s[4]&0xf],
118-
hexLU[s[5]>>4], hexLU[s[5]&0xf],
119-
hexLU[s[6]>>4], hexLU[s[6]&0xf],
120-
hexLU[s[7]>>4], hexLU[s[7]&0xf],
121-
}
91+
func binTohex8(s []byte, d []byte) {
92+
d[0], d[1] = hexLU[s[0]>>4], hexLU[s[0]&0xf]
93+
d[2], d[3] = hexLU[s[1]>>4], hexLU[s[1]&0xf]
94+
d[4], d[5] = hexLU[s[2]>>4], hexLU[s[2]&0xf]
95+
d[6], d[7] = hexLU[s[3]>>4], hexLU[s[3]&0xf]
96+
d[8], d[9] = hexLU[s[4]>>4], hexLU[s[4]&0xf]
97+
d[10], d[11] = hexLU[s[5]>>4], hexLU[s[5]&0xf]
98+
d[12], d[13] = hexLU[s[6]>>4], hexLU[s[6]&0xf]
99+
d[14], d[15] = hexLU[s[7]>>4], hexLU[s[7]&0xf]
122100
}
123101

124102
// TraceIDFromHex returns a TraceID from a hex string if it is compliant with

0 commit comments

Comments
 (0)