Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ type Key uint64

// String returns key in the text form used by JaWS.
func (key Key) String() string {
return string(Append(nil, key))
if key == 0 {
return ""
}
return strconv.FormatUint(uint64(key), 32)
}

// Parse parses a JaWS key prefix from its text form.
Expand Down
10 changes: 10 additions & 0 deletions lib/key/key_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

var appendBenchSink []byte
var stringBenchSink string

// BenchmarkAppend guards the base-32 encode hot path; it must stay
// allocation-light when appending into an existing buffer.
Expand All @@ -20,3 +21,12 @@ func BenchmarkAppend(b *testing.B) {
appendBenchSink = key.Append(appendBenchSink[:0], benchmarkKey)
}
}

// BenchmarkString guards the allocation cost of Key.String.
func BenchmarkString(b *testing.B) {
const benchmarkKey key.Key = 0x1234abcd
b.ReportAllocs()
for b.Loop() {
stringBenchSink = benchmarkKey.String()
}
}
Loading