diff options
author | Michael Anthony Knyszek <mknyszek@google.com> | 2024-04-04 04:51:24 +0000 |
---|---|---|
committer | Michael Knyszek <mknyszek@google.com> | 2024-04-22 18:14:07 +0000 |
commit | a088e230d4e7892b15851babe161bbd1766738a1 (patch) | |
tree | b42bda131142e51a16e6327bb505849a4e60d474 /src/unique/handle_bench_test.go | |
parent | 654c3368e53c923acff5fd5a1eaf4175bb6834d6 (diff) | |
download | go-a088e230d4e7892b15851babe161bbd1766738a1.tar.gz go-a088e230d4e7892b15851babe161bbd1766738a1.zip |
unique: add unique package and implement Make/Handle
This change adds the unique package for canonicalizing values, as
described by the proposal in #62483.
Fixes #62483.
Change-Id: I1dc3d34ec12351cb4dc3838a8ea29a5368d59e99
Reviewed-on: https://go-review.googlesource.com/c/go/+/574355
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ingo Oeser <nightlyone@googlemail.com>
Reviewed-by: David Chase <drchase@google.com>
Diffstat (limited to 'src/unique/handle_bench_test.go')
-rw-r--r-- | src/unique/handle_bench_test.go | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/unique/handle_bench_test.go b/src/unique/handle_bench_test.go new file mode 100644 index 0000000000..51f94c3f91 --- /dev/null +++ b/src/unique/handle_bench_test.go @@ -0,0 +1,63 @@ +// Copyright 2024 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package unique + +import ( + "fmt" + "runtime" + "testing" +) + +func BenchmarkMake(b *testing.B) { + benchmarkMake(b, []string{"foo"}) +} + +func BenchmarkMakeMany(b *testing.B) { + benchmarkMake(b, testData[:]) +} + +func BenchmarkMakeManyMany(b *testing.B) { + benchmarkMake(b, testDataLarge[:]) +} + +func benchmarkMake(b *testing.B, testData []string) { + handles := make([]Handle[string], 0, len(testData)) + for i := range testData { + handles = append(handles, Make(testData[i])) + } + + b.ReportAllocs() + b.ResetTimer() + + b.RunParallel(func(pb *testing.PB) { + i := 0 + for pb.Next() { + _ = Make(testData[i]) + i++ + if i >= len(testData) { + i = 0 + } + } + }) + + b.StopTimer() + + runtime.GC() + runtime.GC() +} + +var ( + testData [128]string + testDataLarge [128 << 10]string +) + +func init() { + for i := range testData { + testData[i] = fmt.Sprintf("%b", i) + } + for i := range testDataLarge { + testDataLarge[i] = fmt.Sprintf("%b", i) + } +} |