aboutsummaryrefslogtreecommitdiff
path: root/src/sync/map_test.go
AgeCommit message (Collapse)Author
2025-07-28all: omit unnecessary type conversionsJes Cok
Found by github.com/mdempsky/unconvert Change-Id: Ib78cceb718146509d96dbb6da87b27dbaeba1306 GitHub-Last-Rev: dedf354811701ce8920c305b6f7aa78914a4171c GitHub-Pull-Request: golang/go#74771 Reviewed-on: https://go-review.googlesource.com/c/go/+/690735 Reviewed-by: Mark Freeman <mark@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <khr@google.com> Auto-Submit: Keith Randall <khr@golang.org> Reviewed-by: Keith Randall <khr@golang.org>
2025-02-14sync: use runtime.AddCleanup instead of runtime.SetFinalizerCarlos Amedee
This changes the use of finalizers to the cleanup implementation in tests. Updates #70907 Change-Id: I7d7289999a83fa53f538698f34294f7d9651c921 Reviewed-on: https://go-review.googlesource.com/c/go/+/640735 Reviewed-by: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-11-18sync: make HashTrieMap[any, any] the default implementation of MapMichael Anthony Knyszek
This change adds a GOEXPERIMENT, synchashtriemap, which replaces the internals of a sync.Map with internal/sync.HashTrieMap[any, any]. The main purpose behind this change is improved performance. Across almost every benchmark, HashTrieMap[any, any] performs better than Map. Also, relax TestMapClearNoAllocations to allow for one allocation. Currently, the HashTrieMap allocates a new empty root node and stores it: that's the whole clear operation. At the cost of some complexity, we could allow Clear to have zero allocations by clearing the root node. The complexity comes down to allowing threads to race to install a new root node *or* creating a top-level mutex for installing a root node. But I'm not sure this is worth it. Whether Clear or some other operation takes the hit for allocating a single node almost certainly doesn't matter. And Clear is still much, much faster in the new implementation than the old, so I don't consider this a regression. Change-Id: I939aa70a0edf2e850cedbea239aaf29a11a77b79 Reviewed-on: https://go-review.googlesource.com/c/go/+/608335 Auto-Submit: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: David Chase <drchase@google.com>
2024-11-18sync: add HashTrieMap to Map tests and benchmarksMichael Anthony Knyszek
Also, rename Map benchmarks to make them easier to single out via regexp. Change-Id: I4dcb066745aba1c340f56050d08539ae2976274d Reviewed-on: https://go-review.googlesource.com/c/go/+/606461 Reviewed-by: David Chase <drchase@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Michael Knyszek <mknyszek@google.com>
2024-02-01sync: add Map.ClearHiro
Fixes #61696 Change-Id: I0a31afd3bc433fc84280d56f2798bda10da61eba GitHub-Last-Rev: 17bedc864f1685178a42b59f7083677a6124f831 GitHub-Pull-Request: golang/go#61702 Reviewed-on: https://go-review.googlesource.com/c/go/+/515015 Auto-Submit: Bryan Mills <bcmills@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: qiulaidongfeng <2645477756@qq.com> Reviewed-by: Bryan Mills <bcmills@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2023-11-20src: a/an grammar fixesVille Skyttä
Change-Id: I179b50ae8e73677d4d408b83424afbbfe6aa17a1 GitHub-Last-Rev: 2e2d9c1e45556155d02db4df381b99f2d1bc5c0e GitHub-Pull-Request: golang/go#63478 Reviewed-on: https://go-review.googlesource.com/c/go/+/534015 Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2023-09-01sync: prevent (*Map).Range from always escapingMauri de Souza Meneguzzo
After the change from CL 426074 the Range method on Map always escape the read variable, leading to an allocation. Since the compiler doesn't do live-range splitting for local variables we need to use some hints to only escape in that particular branch. Fixes #62404 Change-Id: I938a5e593647455fa827e3dd3ed8ea22c7365df1 GitHub-Last-Rev: fcbedb467c7b4e6f1d49e299d243cad70deb34e9 GitHub-Pull-Request: golang/go#62408 Reviewed-on: https://go-review.googlesource.com/c/go/+/524976 Auto-Submit: Bryan Mills <bcmills@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Run-TryBot: Bryan Mills <bcmills@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-11-15sync: add new Map method Swap, CompareAndSwap, CompareAndDeleteChangkun Ou
name time/op SwapCollision/*sync_test.DeepCopyMap-8 235ns ± 0% SwapCollision/*sync_test.RWMutexMap-8 145ns ± 0% SwapCollision/*sync.Map-8 153ns ± 0% SwapMostlyHits/*sync_test.DeepCopyMap-8 48.2µs ± 0% SwapMostlyHits/*sync_test.RWMutexMap-8 190ns ± 0% SwapMostlyHits/*sync.Map-8 28.3ns ± 0% SwapMostlyMisses/*sync_test.DeepCopyMap-8 681ns ± 0% SwapMostlyMisses/*sync_test.RWMutexMap-8 336ns ± 0% SwapMostlyMisses/*sync.Map-8 523ns ± 0% CompareAndSwapCollision/*sync_test.DeepCopyMap-8 3.99ns ± 0% CompareAndSwapCollision/*sync_test.RWMutexMap-8 151ns ± 0% CompareAndSwapCollision/*sync.Map-8 21.6ns ± 0% CompareAndSwapNoExistingKey/*sync_test.DeepCopyMap-8 3.95ns ± 0% CompareAndSwapNoExistingKey/*sync_test.RWMutexMap-8 126ns ± 0% CompareAndSwapNoExistingKey/*sync.Map-8 6.11ns ± 0% CompareAndSwapValueNotEqual/*sync_test.DeepCopyMap-8 2.15ns ± 0% CompareAndSwapValueNotEqual/*sync_test.RWMutexMap-8 132ns ± 0% CompareAndSwapValueNotEqual/*sync.Map-8 5.32ns ± 0% CompareAndSwapMostlyHits/*sync_test.RWMutexMap-8 219ns ± 0% CompareAndSwapMostlyHits/*sync.Map-8 27.1ns ± 0% CompareAndSwapMostlyMisses/*sync_test.DeepCopyMap-8 13.0ns ± 0% CompareAndSwapMostlyMisses/*sync_test.RWMutexMap-8 147ns ± 0% CompareAndSwapMostlyMisses/*sync.Map-8 19.6ns ± 0% CompareAndDeleteCollision/*sync_test.DeepCopyMap-8 2.23ns ± 0% CompareAndDeleteCollision/*sync_test.RWMutexMap-8 131ns ± 0% CompareAndDeleteCollision/*sync.Map-8 16.2ns ± 0% CompareAndDeleteMostlyHits/*sync_test.RWMutexMap-8 367ns ± 0% CompareAndDeleteMostlyHits/*sync.Map-8 33.1ns ± 0% CompareAndDeleteMostlyMisses/*sync_test.DeepCopyMap-8 8.75ns ± 0% CompareAndDeleteMostlyMisses/*sync_test.RWMutexMap-8 134ns ± 0% CompareAndDeleteMostlyMisses/*sync.Map-8 10.9ns ± 0% name alloc/op SwapCollision/*sync_test.DeepCopyMap-8 336B ± 0% SwapCollision/*sync_test.RWMutexMap-8 0.00B SwapCollision/*sync.Map-8 16.0B ± 0% SwapMostlyHits/*sync_test.DeepCopyMap-8 82.1kB ± 0% SwapMostlyHits/*sync_test.RWMutexMap-8 12.0B ± 0% SwapMostlyHits/*sync.Map-8 28.0B ± 0% SwapMostlyMisses/*sync_test.DeepCopyMap-8 713B ± 0% SwapMostlyMisses/*sync_test.RWMutexMap-8 23.0B ± 0% SwapMostlyMisses/*sync.Map-8 129B ± 0% CompareAndSwapCollision/*sync_test.DeepCopyMap-8 0.00B CompareAndSwapCollision/*sync_test.RWMutexMap-8 0.00B CompareAndSwapCollision/*sync.Map-8 3.00B ± 0% CompareAndSwapNoExistingKey/*sync_test.DeepCopyMap-8 8.00B ± 0% CompareAndSwapNoExistingKey/*sync_test.RWMutexMap-8 8.00B ± 0% CompareAndSwapNoExistingKey/*sync.Map-8 8.00B ± 0% CompareAndSwapValueNotEqual/*sync_test.DeepCopyMap-8 0.00B CompareAndSwapValueNotEqual/*sync_test.RWMutexMap-8 0.00B CompareAndSwapValueNotEqual/*sync.Map-8 0.00B CompareAndSwapMostlyHits/*sync_test.RWMutexMap-8 18.0B ± 0% CompareAndSwapMostlyHits/*sync.Map-8 33.0B ± 0% CompareAndSwapMostlyMisses/*sync_test.DeepCopyMap-8 24.0B ± 0% CompareAndSwapMostlyMisses/*sync_test.RWMutexMap-8 23.0B ± 0% CompareAndSwapMostlyMisses/*sync.Map-8 23.0B ± 0% CompareAndDeleteCollision/*sync_test.DeepCopyMap-8 0.00B CompareAndDeleteCollision/*sync_test.RWMutexMap-8 0.00B CompareAndDeleteCollision/*sync.Map-8 0.00B CompareAndDeleteMostlyHits/*sync_test.RWMutexMap-8 23.0B ± 0% CompareAndDeleteMostlyHits/*sync.Map-8 39.0B ± 0% CompareAndDeleteMostlyMisses/*sync_test.DeepCopyMap-8 16.0B ± 0% CompareAndDeleteMostlyMisses/*sync_test.RWMutexMap-8 15.0B ± 0% CompareAndDeleteMostlyMisses/*sync.Map-8 15.0B ± 0% name allocs/op SwapCollision/*sync_test.DeepCopyMap-8 2.00 ± 0% SwapCollision/*sync_test.RWMutexMap-8 0.00 SwapCollision/*sync.Map-8 1.00 ± 0% SwapMostlyHits/*sync_test.DeepCopyMap-8 4.00 ± 0% SwapMostlyHits/*sync_test.RWMutexMap-8 1.00 ± 0% SwapMostlyHits/*sync.Map-8 2.00 ± 0% SwapMostlyMisses/*sync_test.DeepCopyMap-8 6.00 ± 0% SwapMostlyMisses/*sync_test.RWMutexMap-8 2.00 ± 0% SwapMostlyMisses/*sync.Map-8 6.00 ± 0% CompareAndSwapCollision/*sync_test.DeepCopyMap-8 0.00 CompareAndSwapCollision/*sync_test.RWMutexMap-8 0.00 CompareAndSwapCollision/*sync.Map-8 0.00 CompareAndSwapNoExistingKey/*sync_test.DeepCopyMap-8 1.00 ± 0% CompareAndSwapNoExistingKey/*sync_test.RWMutexMap-8 0.00 CompareAndSwapNoExistingKey/*sync.Map-8 1.00 ± 0% CompareAndSwapValueNotEqual/*sync_test.DeepCopyMap-8 0.00 CompareAndSwapValueNotEqual/*sync_test.RWMutexMap-8 0.00 CompareAndSwapValueNotEqual/*sync.Map-8 0.00 CompareAndSwapMostlyHits/*sync_test.RWMutexMap-8 2.00 ± 0% CompareAndSwapMostlyHits/*sync.Map-8 3.00 ± 0% CompareAndSwapMostlyMisses/*sync_test.DeepCopyMap-8 2.00 ± 0% CompareAndSwapMostlyMisses/*sync_test.RWMutexMap-8 2.00 ± 0% CompareAndSwapMostlyMisses/*sync.Map-8 2.00 ± 0% CompareAndDeleteCollision/*sync_test.DeepCopyMap-8 0.00 CompareAndDeleteCollision/*sync_test.RWMutexMap-8 0.00 CompareAndDeleteCollision/*sync.Map-8 0.00 CompareAndDeleteMostlyHits/*sync_test.RWMutexMap-8 2.00 ± 0% CompareAndDeleteMostlyHits/*sync.Map-8 3.00 ± 0% CompareAndDeleteMostlyMisses/*sync_test.DeepCopyMap-8 2.00 ± 0% CompareAndDeleteMostlyMisses/*sync_test.RWMutexMap-8 1.00 ± 0% CompareAndDeleteMostlyMisses/*sync.Map-8 1.00 ± 0% Fixes #51972 Change-Id: I469e71033592997832c3e8ebdad1b8950a70c99c Reviewed-on: https://go-review.googlesource.com/c/go/+/399094 Run-TryBot: Changkun Ou <mail@changkun.de> Reviewed-by: Joedian Reid <joedian@golang.org> Auto-Submit: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com>
2021-12-13all: gofmt -w -r 'interface{} -> any' srcRuss Cox
And then revert the bootstrap cmd directories and certain testdata. And adjust tests as needed. Not reverting the changes in std that are bootstrapped, because some of those changes would appear in API docs, and we want to use any consistently. Instead, rewrite 'any' to 'interface{}' in cmd/dist for those directories when preparing the bootstrap copy. A few files changed as a result of running gofmt -w not because of interface{} -> any but because they hadn't been updated for the new //go:build lines. Fixes #49884. Change-Id: Ie8045cba995f65bd79c694ec77a1b3d1fe01bb09 Reviewed-on: https://go-review.googlesource.com/c/go/+/368254 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
2021-11-11sync: clarify the validity to call Map methods inside RangeChangkun Ou
This change clarifies that calling all Map methods inside the callback of Range is allowed. For further assurance, a nested range call test is also added. Fixes #46399 Change-Id: I0a766a5c1470e6b573ec35df1ccd62b2e46f1561 Reviewed-on: https://go-review.googlesource.com/c/go/+/337389 Reviewed-by: Bryan C. Mills <bcmills@google.com> Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Ian Lance Taylor <iant@golang.org>
2020-08-24sync: delete dirty keys inside Map.LoadAndDeleteChangkun Ou
Fixes #40999 Change-Id: Ie32427e5cb5ed512b976b554850f50be156ce9f2 Reviewed-on: https://go-review.googlesource.com/c/go/+/250197 Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
2020-02-25sync: add new Map method LoadAndDeleteChangkun Ou
This CL implements a LoadAndDelete method in sync.Map. Benchmark: name time/op LoadAndDeleteBalanced/*sync_test.RWMutexMap-12 98.8ns ± 1% LoadAndDeleteBalanced/*sync.Map-12 10.3ns ±11% LoadAndDeleteUnique/*sync_test.RWMutexMap-12 99.2ns ± 2% LoadAndDeleteUnique/*sync.Map-12 6.63ns ±10% LoadAndDeleteCollision/*sync_test.DeepCopyMap-12 140ns ± 0% LoadAndDeleteCollision/*sync_test.RWMutexMap-12 75.2ns ± 2% LoadAndDeleteCollision/*sync.Map-12 5.21ns ± 5% In addition, Delete is bounded and more efficient if many collisions: DeleteCollision/*sync_test.DeepCopyMap-12 120ns ± 2% 125ns ± 1% +3.80% (p=0.000 n=10+9) DeleteCollision/*sync_test.RWMutexMap-12 73.5ns ± 3% 79.5ns ± 1% +8.03% (p=0.000 n=10+9) DeleteCollision/*sync.Map-12 97.8ns ± 3% 5.9ns ± 4% -94.00% (p=0.000 n=10+10) Fixes #33762 Change-Id: Ic8469a7861d27ab0edeface0078aad8af9b26c2f Reviewed-on: https://go-review.googlesource.com/c/go/+/205899 Reviewed-by: Bryan C. Mills <bcmills@google.com> Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-04-30sync: really test DeepCopyMapIngo Oeser
since there current version suffers from a copy'n'paste error. Change-Id: I1ef97a4413198dde10c5c926a2a9806724cd6d44 Reviewed-on: https://go-review.googlesource.com/42095 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-04-26sync: import Map from x/sync/syncmapBryan C. Mills
This is a direct port of the version from commit a60ad46e0ed33d02e09bda439efaf9c9727dbc6c (https://go-review.googlesource.com/c/37342/). updates #17973 updates #18177 Change-Id: I63fa5ef6951b1edd39f84927d1181a4df9b15385 Reviewed-on: https://go-review.googlesource.com/36617 Reviewed-by: Russ Cox <rsc@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>