Skip to content

Commit 87d5159

Browse files
committed
bump
1 parent 7ae976b commit 87d5159

File tree

2 files changed

+20
-58
lines changed

2 files changed

+20
-58
lines changed

base58/base58.go

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -69,77 +69,50 @@ func UnmarshalString(dst []byte, str string) error {
6969
return UnmarshalBytes(dst, []byte(str))
7070
}
7171

72-
func UnmarshalBytesOld(dst, src []byte) error {
73-
outi := make([]uint32, 4) // (uuidSize + 3) / 4
74-
75-
for i := 0; i < len(src); i++ {
76-
c := decode[src[i]]
77-
78-
for j := len(outi) - 1; j >= 0; j-- {
79-
t := uint64(outi[j])*58 + c
80-
c = t >> 32
81-
outi[j] = uint32(t & 0xffffffff)
82-
}
83-
}
84-
85-
mask := uint32(24) // (((uuidSize%4) * 8) || 32) - 8
86-
outLen := 0
87-
for j := 0; j < len(outi); j++ {
88-
for mask < 32 {
89-
dst[outLen] = byte(outi[j] >> mask)
90-
mask -= 8
91-
outLen++
92-
}
93-
mask = 24
94-
}
95-
96-
return nil
97-
}
98-
9972
func UnmarshalBytes(dst, src []byte) error {
10073
// Use stack allocation for better performance
10174
var outi [4]uint32
102-
75+
10376
// Optimized for the common case of 22-byte base58 UUID
10477
if len(src) == 22 {
10578
// Unrolled loop for base58 decoding
10679
// Process all 22 characters with partially unrolled loop
10780
var c uint64
108-
81+
10982
// Unroll by 2 for better performance
11083
for i := 0; i < 22; i += 2 {
11184
// First character
11285
c = decode[src[i]]
11386
t3 := uint64(outi[3])*58 + c
11487
c = t3 >> 32
11588
outi[3] = uint32(t3)
116-
89+
11790
t2 := uint64(outi[2])*58 + c
11891
c = t2 >> 32
11992
outi[2] = uint32(t2)
120-
93+
12194
t1 := uint64(outi[1])*58 + c
12295
c = t1 >> 32
12396
outi[1] = uint32(t1)
124-
97+
12598
t0 := uint64(outi[0])*58 + c
12699
outi[0] = uint32(t0)
127-
100+
128101
// Second character (if exists)
129102
if i+1 < 22 {
130103
c = decode[src[i+1]]
131104
t3 = uint64(outi[3])*58 + c
132105
c = t3 >> 32
133106
outi[3] = uint32(t3)
134-
107+
135108
t2 = uint64(outi[2])*58 + c
136109
c = t2 >> 32
137110
outi[2] = uint32(t2)
138-
111+
139112
t1 = uint64(outi[1])*58 + c
140113
c = t1 >> 32
141114
outi[1] = uint32(t1)
142-
115+
143116
t0 = uint64(outi[0])*58 + c
144117
outi[0] = uint32(t0)
145118
}
@@ -148,31 +121,31 @@ func UnmarshalBytes(dst, src []byte) error {
148121
// Fallback for non-standard lengths
149122
for i := 0; i < len(src); i++ {
150123
c := decode[src[i]]
151-
124+
152125
for j := 3; j >= 0; j-- {
153126
t := uint64(outi[j])*58 + c
154127
c = t >> 32
155128
outi[j] = uint32(t)
156129
}
157130
}
158131
}
159-
132+
160133
// Unrolled output conversion
161134
dst[0] = byte(outi[0] >> 24)
162135
dst[1] = byte(outi[0] >> 16)
163136
dst[2] = byte(outi[0] >> 8)
164137
dst[3] = byte(outi[0])
165-
138+
166139
dst[4] = byte(outi[1] >> 24)
167140
dst[5] = byte(outi[1] >> 16)
168141
dst[6] = byte(outi[1] >> 8)
169142
dst[7] = byte(outi[1])
170-
143+
171144
dst[8] = byte(outi[2] >> 24)
172145
dst[9] = byte(outi[2] >> 16)
173146
dst[10] = byte(outi[2] >> 8)
174147
dst[11] = byte(outi[2])
175-
148+
176149
dst[12] = byte(outi[3] >> 24)
177150
dst[13] = byte(outi[3] >> 16)
178151
dst[14] = byte(outi[3] >> 8)

base58/base58_test.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ var testCases = []string{
7070
func BenchmarkUnmarshalBytesOld(b *testing.B) {
7171
dst := make([]byte, 16)
7272
src := []byte(testCases[0])
73-
73+
7474
b.ResetTimer()
7575
for i := 0; i < b.N; i++ {
7676
_ = UnmarshalBytesOld(dst, src)
@@ -80,27 +80,16 @@ func BenchmarkUnmarshalBytesOld(b *testing.B) {
8080
func BenchmarkUnmarshalBytesNew(b *testing.B) {
8181
dst := make([]byte, 16)
8282
src := []byte(testCases[0])
83-
84-
b.ResetTimer()
85-
for i := 0; i < b.N; i++ {
86-
_ = UnmarshalBytes(dst, src)
87-
}
88-
}
8983

90-
func BenchmarkUnmarshalBytesOldMultiple(b *testing.B) {
91-
dst := make([]byte, 16)
92-
9384
b.ResetTimer()
9485
for i := 0; i < b.N; i++ {
95-
for _, tc := range testCases {
96-
_ = UnmarshalBytesOld(dst, []byte(tc))
97-
}
86+
_ = UnmarshalBytes(dst, src)
9887
}
9988
}
10089

10190
func BenchmarkUnmarshalBytesNewMultiple(b *testing.B) {
10291
dst := make([]byte, 16)
103-
92+
10493
b.ResetTimer()
10594
for i := 0; i < b.N; i++ {
10695
for _, tc := range testCases {
@@ -114,14 +103,14 @@ func TestUnmarshalBytesConsistency(t *testing.T) {
114103
src := []byte(tc)
115104
dst1 := make([]byte, 16)
116105
dst2 := make([]byte, 16)
117-
106+
118107
err1 := UnmarshalBytesOld(dst1, src)
119108
err2 := UnmarshalBytes(dst2, src)
120-
109+
121110
if err1 != err2 {
122111
t.Fatalf("Error mismatch for %q: old=%v, new=%v", tc, err1, err2)
123112
}
124-
113+
125114
for i := range dst1 {
126115
if dst1[i] != dst2[i] {
127116
t.Fatalf("Result mismatch for %q at byte %d: old=%x, new=%x", tc, i, dst1, dst2)

0 commit comments

Comments
 (0)