Commit graph

56 commits

Author SHA1 Message Date
qiulaidongfeng
506c70f973 errgroup: propagate panic and Goexit through Wait
Recovered panic values are wrapped and saved in Group.
Goexits are detected by a sentinel value set after the given function
returns normally. Wait propagates the first instance of a panic or
Goexit.

According to the runtime.Goexit after the code will not be executed,
with a bool, if f not call runtime.Goexit, is true,
determine whether to propagate runtime.Goexit.

Fixes golang/go#53757

Change-Id: Ic6426fc014fd1c4368ebaceef5b0d6163770a099
Reviewed-on: https://go-review.googlesource.com/c/sync/+/644575
Reviewed-by: Sean Liao <sean@liao.dev>
Auto-Submit: Alan Donovan <adonovan@google.com>
Commit-Queue: Alan Donovan <adonovan@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-04-07 09:20:50 -07:00
Sean Liao
396f3a06ea errgroup: document calling Go before Wait
Fixes golang/go#70284

Change-Id: I31d60ea182226c032b0ffbddcbb5b53675dfd5e6
Reviewed-on: https://go-review.googlesource.com/c/sync/+/660075
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Auto-Submit: Damien Neil <dneil@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
2025-04-02 08:05:34 -07:00
Ian Lance Taylor
b637f27e40 errgroup: drop support for Go versions before 1.20
Change-Id: I7de5dfae21c4ffe31d6c16e3df0fed3e2269cb16
Reviewed-on: https://go-review.googlesource.com/c/sync/+/654421
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Commit-Queue: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
2025-03-04 15:33:19 -08:00
Gopher Robot
960bf1fb13 all: upgrade go directive to at least 1.23.0 [generated]
By now Go 1.24.0 has been released, and Go 1.22 is no longer supported
per the Go Release Policy (https://go.dev/doc/devel/release#policy).

For golang/go#69095.

[git-generate]
(cd . && go get go@1.23.0 && go mod tidy && go fix ./... && go mod edit -toolchain=none)

Change-Id: I7e3b1e073a0a64e82e2b9e49387d5da0afd9d58b
Reviewed-on: https://go-review.googlesource.com/c/sync/+/649835
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Gopher Robot <gobot@golang.org>
2025-02-14 13:41:05 -08:00
berbreik
fe3591bd8a sync/errgroup: improve documentation for semaphore limit behavior
Updated the documentation for `SetLimit` and `Go` functions in the
`errgroup` package. the note clarifies the behavior when the semaphore
limit is set to zero, which could lead to a deadlock if not handled
properly. the updated documentation warns users to set a positive
semaphore limit to avoid blocking goroutines and potential deadlock

this change improves the clarity of API and helps users understand how
to properly use the semaphore limit to prevent issues in concurrent
execution

Change-Id: Ie62acc38ef804b3590c04bddba00a3cce21e8cef
Reviewed-on: https://go-review.googlesource.com/c/sync/+/642215
Reviewed-by: Michael Knyszek <mknyszek@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
2025-01-17 07:49:37 -08:00
catatsuy
913fb63af2 singleflight: fix typo in singleflight_test.go
Change-Id: Ia2160fdc8f1938d784a4e1595151c00bc521f5c3
Reviewed-on: https://go-review.googlesource.com/c/sync/+/626795
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com>
Commit-Queue: Ian Lance Taylor <iant@google.com>
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Emmanuel Odeke <emmanuel@orijtech.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
2024-11-13 01:18:28 +00:00
Ian Lance Taylor
151027eb56 README: don't recommend go get
These days people will just import the packages and the go tool will
do the right thing. We don't need to explain it.

Add a pointer to the git repo, though.

For golang/go#62645

Change-Id: Icbe33e05f96e1b32f6fcc99fb5fb0b2ecb213a08
Reviewed-on: https://go-review.googlesource.com/c/sync/+/623820
Commit-Queue: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
2024-11-01 00:46:18 +00:00
Russ Cox
411f99ef12 LICENSE: update per Google Legal
Very minor tweaks:
 - Remove (c) pseudosymbol.
 - Remove "All Rights Reserved."
 - Change "Google Inc." (no longer exists) to "Google LLC".

[git-generate]
echo '
,s/\(c\) //
,s/ All rights reserved.//
,s/Google Inc./Google LLC/
w
q
' | sam -d LICENSE

Change-Id: Ifc1b84adca131018a48ba47e741a682a0ed9bb49
Reviewed-on: https://go-review.googlesource.com/c/sync/+/598584
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Russ Cox <rsc@golang.org>
2024-07-16 16:06:58 +00:00
Branden Brown
14be23e5b4 semaphore: cancel acquisition with a done context
When acquiring from a semaphore could proceed without contention, the
previous behavior was to always do so, even when the provided context
was done. This was the documented behavior, but it could lead to
confusion. It isn't much more expensive to check the context error, so
cancel acquisition if it's done.

Fixes golang/go#63615.

goos: linux
goarch: amd64
pkg: golang.org/x/sync/semaphore
cpu: 12th Gen Intel(R) Core(TM) i5-1235U
                                        │  old.bench  │             new.bench              │
                                        │   sec/op    │   sec/op     vs base               │
AcquireSeq/Weighted-acquire-1-1-1-12      26.45n ± 2%   27.25n ± 3%  +3.04% (p=0.001 n=20)
AcquireSeq/Weighted-acquire-2-1-1-12      26.96n ± 1%   27.12n ± 1%       ~ (p=0.104 n=20)
AcquireSeq/Weighted-acquire-16-1-1-12     26.07n ± 3%   27.48n ± 1%  +5.45% (p=0.000 n=20)
AcquireSeq/Weighted-acquire-128-1-1-12    26.19n ± 2%   27.24n ± 1%  +4.01% (p=0.000 n=20)
AcquireSeq/Weighted-acquire-2-2-1-12      25.61n ± 1%   25.99n ± 2%       ~ (p=0.066 n=20)
AcquireSeq/Weighted-acquire-16-2-8-12     209.6n ± 2%   211.0n ± 3%       ~ (p=0.280 n=20)
AcquireSeq/Weighted-acquire-128-2-64-12   1.669µ ± 1%   1.721µ ± 2%  +3.09% (p=0.000 n=20)
AcquireSeq/Weighted-acquire-2-1-2-12      51.08n ± 1%   53.03n ± 2%  +3.82% (p=0.000 n=20)
AcquireSeq/Weighted-acquire-16-8-2-12     52.48n ± 2%   53.66n ± 2%  +2.26% (p=0.028 n=20)
AcquireSeq/Weighted-acquire-128-64-2-12   52.27n ± 1%   53.71n ± 2%  +2.75% (p=0.000 n=20)
geomean                                   60.06n        61.69n       +2.71%

Change-Id: I0ae1a0bb6c027461ac1a9ee71c51efd8427ab308
Reviewed-on: https://go-review.googlesource.com/c/sync/+/536275
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-03-04 17:26:02 +00:00
Olivier Mengué
59c1ca1e46 errgroup: add reference to sync.WaitGroup
Add doc link to sync.WaitGroup in the package documentation.

Rationale:
The reference to sync.WaitGroup might be obvious to the authors of this
package, but it wasn't to me, so despites I had seen this package
multiple times I had never made the link to sync.WaitGroup. So when
playing with WaitGroup I was missing remembering about errgroup.
In hope this will help others as well as my future self.

Change-Id: I2c56976958ad9ab94c1596c96fa86c00dd73d2fb
Reviewed-on: https://go-review.googlesource.com/c/sync/+/538335
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2023-12-07 16:58:19 +00:00
Dmitri Shuralyov
10739b037d all: update go directive to 1.18
Done with:

go get go@1.18
go mod tidy
go fix ./...

Using go1.21.3.

While here, simplify package syncmap by taking advantage of knowing
that all supported Go versions will have the go1.9 build constraint
satisfied.

For golang/go#60268.

Change-Id: Ic0f24ab13ada6839573e55beee5516c1a6c7f3cc
Reviewed-on: https://go-review.googlesource.com/c/sync/+/534220
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2023-10-11 14:04:17 +00:00
Eric Wohltman
22ba2078e1 singleflight: add panicError.Unwrap method
Currently when singleflight recovers from a panic, it wraps it with the private
error type panicError. This change adds an `Unwrap` method to panicError to
allow wrapped errors to be returned.

Updates golang/go#62511

Change-Id: Ia510ad7d5881207ef71f9eb89c1766835af19b6b
Reviewed-on: https://go-review.googlesource.com/c/sync/+/526171
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2023-09-26 16:46:49 +00:00
Jon Johnson
93782cc822 errgroup: use WithCancelCause to cancel context
Fixes golang/go#59355

Change-Id: Ib6a88e7e5fefe7b0d5672035af16d109aabcbf1e
Reviewed-on: https://go-review.googlesource.com/c/sync/+/481255
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
2023-06-01 20:35:10 +00:00
Sean Liao
4966af63bb singleflight: example for using Group
Fixes golang/go#60208

Change-Id: I422a45c0f139ba47045f47cec1c96d300a2a424f
Reviewed-on: https://go-review.googlesource.com/c/sync/+/496535
Run-TryBot: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
2023-05-23 18:54:36 +00:00
Bryan C. Mills
a6666c150e go.mod: upgrade to go 1.17
This change was produced using 'go mod tidy -go=1.17'
with a go command built at CL 315210.

This activates lazy loading, and updates the go.mod file to maintain
the lazy-loading invariants (namely, including an explicit requirement
for every package transitively imported by the main module).

Note that this does *not* prevent users with earlier go versions from
successfully building packages from this module.

For golang/go#36460.

Change-Id: Idb3d8d056d41bb254d0b5d9b2699ac33a51081f3
Reviewed-on: https://go-review.googlesource.com/c/sync/+/316130
Auto-Submit: Bryan Mills <bcmills@google.com>
Commit-Queue: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
2023-05-17 13:21:07 +00:00
Bryan C. Mills
1ea32573dd singleflight: make the check for exec support in TestPanicDoChan platform-agnostic
The new wasip1 GOOS does not support exec, but some ios environments
(like Corellium) might. Update the test to exec itself with -test.list
as a control case.

For golang/go#58141.

Change-Id: Id69950fc394910620f6c73cb437ca75c09ad8c29
Reviewed-on: https://go-review.googlesource.com/c/sync/+/485980
Run-TryBot: Bryan Mills <bcmills@google.com>
Commit-Queue: Bryan Mills <bcmills@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
2023-04-19 16:11:59 +00:00
Cuong Manh Le
8fcdb60fdc singleflight: avoid race between multiple Do calls
This is port of CL 436437, but without the test. ForgotUnshared has not
been ported here yet.

Change-Id: Id54d0c41d1a7948bf008e458c44b21670ada81e4
Reviewed-on: https://go-review.googlesource.com/c/sync/+/436495
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2022-09-29 20:41:14 +00:00
Cuong Manh Le
7f9b1623fa singleflight: remove forgotten field
Port from CL 433315.

Change-Id: Iaf6ab4676dd10a73443c3c9981ffc77233b4631c
Reviewed-on: https://go-review.googlesource.com/c/sync/+/433555
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-09-23 20:29:41 +00:00
cui fliter
f12130a528 syncmap: remove redundant type conversion
Change-Id: I32c367338e1ea95aaaaa8e891f5dfe4ab6c03913
GitHub-Last-Rev: a2b25df29ad89452b484bda107a0834d79f09310
GitHub-Pull-Request: golang/sync#18
Reviewed-on: https://go-review.googlesource.com/c/sync/+/429058
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
2022-09-07 14:00:24 +00:00
Anderson Queiroz
7fc1605a5d x/sync/errgroup: clarify docs for Go
Even thought Go cancels the context in g, if it's not nil, it'll call
any subsequent function, regardless if a previous call of f returned an error or not.

Fixes golang/go#54045

Change-Id: Ie6059925f45458cf393bf1fc8949ce16be8e6f2a
Reviewed-on: https://go-review.googlesource.com/c/sync/+/424634
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
2022-08-19 03:09:29 +00:00
Russ Cox
886fb9371e A+C: delete AUTHORS and CONTRIBUTORS
In 2009, Google's open-source lawyers asked us to create the AUTHORS
file to define "The Go Authors", and the CONTRIBUTORS file was in
keeping with open source best practices of the time.

Re-reviewing our repos now in 2022, the open-source lawyers are
comfortable with source control history taking the place of the
AUTHORS file, and most open source projects no longer maintain
CONTRIBUTORS files.

To ease maintenance, remove AUTHORS and CONTRIBUTORS from all repos.

For golang/go#53961.

Change-Id: I182181f0c116a7218ad9aba1c7a645fe0190762a
Reviewed-on: https://go-review.googlesource.com/c/sync/+/418922
Reviewed-by: David Chase <drchase@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
2022-07-22 15:52:55 +00:00
Jean de Klerk
0de741cfad x/sync/errgroup: make note of the default errgroup goroutine limit (unlimited)
Change-Id: I1926e6faf821a7adb6c1365d18432abb0f856367
Reviewed-on: https://go-review.googlesource.com/c/sync/+/409774
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Changkun Ou <mail@changkun.de>
Run-TryBot: Changkun Ou <mail@changkun.de>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-06-01 15:02:17 +00:00
Changkun Ou
0976fa681c x/sync/errgroup: add TryGo and SetLimit to control concurrency
This benchmark shows the difference between two implementations.
Using explicit waiter with mutex (old, before PS3) or channel (new,
since PS4). There is no significant difference at a measure:

name  old time/op    new time/op    delta
Go-8     247ns ±10%     245ns ±10%     ~     (p=0.571 n=5+10)

name  old alloc/op   new alloc/op   delta
Go-8     48.0B ± 0%     40.0B ± 0%  -16.67%  (p=0.000 n=5+10)

name  old allocs/op  new allocs/op  delta
Go-8      2.00 ± 0%      2.00 ± 0%     ~     (all equal)

Fixes golang/go#27837

Change-Id: I60247f1a2a1cdce2b180f10b409e37de8b82341e
Reviewed-on: https://go-review.googlesource.com/c/sync/+/405174
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Changkun Ou <mail@changkun.de>
Auto-Submit: Bryan Mills <bcmills@google.com>
2022-05-13 21:05:16 +00:00
Russ Cox
036812b2e8 all: go fmt ./...
Make all our package sources use Go 1.17 gofmt format
(adding //go:build lines).

Not strictly necessary but will avoid spurious changes
as files are edited.

Part of //go:build change (#41184).
See https://golang.org/design/draft-gobuild

Change-Id: Ie3f0a5f80ab53b95a25db9ccfd9a3cc4ea7ff465
Reviewed-on: https://go-review.googlesource.com/c/sync/+/294370
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-02-20 03:29:51 +00:00
Julie Qiu
09787c993a README.md: add badge to pkg.go.dev
Change-Id: I4ea3e103c290e5d713a791178b35a41509e87275
Reviewed-on: https://go-review.googlesource.com/c/sync/+/275934
Trust: Julie Qiu <julie@golang.org>
Run-TryBot: Julie Qiu <julie@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
2020-12-07 23:25:20 +00:00
Cuong Manh Le
67f06af15b singleflight: fix flaky TestForget
There can be a race condition in current TestForget, that said when
"close(secondCh)" is executed, the second goroutine will be finished
immediately, causing the third "g.Do" is evaluated.

To fix this, we change to use "g.DoChan" for both second and third. In
second, we block to make sure it's still running at the time we call
third. after then we unblock second and verify the result.

Fixes golang/go#42092

Change-Id: I980fdf109a531e2b7a74c8149b4fcaa338775e08
Reviewed-on: https://go-review.googlesource.com/c/sync/+/263877
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
2020-10-20 16:03:32 +00:00
Bryan C. Mills
b3e1573b75 singleflight: skip tests involving exec on js/wasm
The js port does not yet support os/exec.

Updates golang/go#37100
Updates golang/go#33519

Change-Id: I9608b7febfdc274dc1b9f34a92d00ef7bea4e13c
Reviewed-on: https://go-review.googlesource.com/c/sync/+/260717
Trust: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
2020-10-08 14:14:35 +00:00
hanliang.kirk
30421366ff singleflight: fix hangs after first Do panic
When first Do panic, the related wait group will never be done,
and all the subsequent calls would block on the same wait group forever.

Fixes golang/go#41133

Change-Id: I0ad9bfb387b6133b10766a34fc0040f200eae27e
Reviewed-on: https://go-review.googlesource.com/c/sync/+/251677
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Trust: Ian Lance Taylor <iant@golang.org>
Trust: Bryan C. Mills <bcmills@google.com>
2020-09-30 13:27:11 +00:00
Cholerae Hu
6e8e738ad2 errgroup: fix go vet "copy lock value" warning
Change-Id: I55256db02fe52ced96fe394574b81ced090999fe
Reviewed-on: https://go-review.googlesource.com/c/sync/+/236477
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
2020-06-25 20:38:02 +00:00
徐志强
43a5402ce7 semaphore: unblock waiters when the front waiter cancels
When `Release`, if the remaining tokens are not enough for the front waiter, no waiters will be notified.

So if the canceled waiter is the front one, it should try to notify following waiters if there are remaining tokens.

I found this bug when implementing a cancelable rwmutex based on semaphore:

https://github.com/zhiqiangxu/util/blob/master/mutex/crwmutex.go

This bug can be verified by this test:

https://github.com/zhiqiangxu/util/blob/master/mutex/crwmutex_test.go#L43

Change-Id: Id8564976bd375a82c4fbc6cb08b0bb83118a346c
GitHub-Last-Rev: 29b6ff26bf779d23239cfe64a395378a9e41d1fc
GitHub-Pull-Request: golang/sync#10
Reviewed-on: https://go-review.googlesource.com/c/sync/+/223418
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
2020-03-17 01:50:54 +00:00
Eric Rutherford
cd5d95a43a syncmap: use type alias for Map
sync.Map was introduced in Go 1.9, this change updates the syncmap package to use a type alias
to sync.Map for 1.9 and later. It also updates the comments to reflect this change.

Fixes golang/go#33867

Change-Id: Ia58ad2bc409f8fcb8a7539ef165148315da5cfb1
Reviewed-on: https://go-review.googlesource.com/c/sync/+/192737
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-09-11 18:51:00 +00:00
Jean de Klerk
112230192c semaphore: better error message when releasing more than held
Change-Id: Ibe69007b2d7565a33d9e64ae51142478f495509a
Reviewed-on: https://go-review.googlesource.com/c/sync/+/173377
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-04-23 02:48:10 +00:00
LE Manh Cuong
56d357773e singleflight: fix duplicate deleting key when Forget called
When Forget was called, we delete key associated with current call from
map. When that call is done, it does delete key again, causing the same
key set by other call after Forget lost.

To fix it, adding a boolean value to check whether the call is forgotten,
the call only does delete key if Forget is not called.

Fixes golang/go#31420

Change-Id: I9708352ca3ff76c77f659916b37a496fdeb480d2
Reviewed-on: https://go-review.googlesource.com/c/sync/+/171897
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-04-12 18:36:30 +00:00
Michael Matloob
e225da77a7 all: add a go.mod file
This change adds a go.mod file to this repo, following the
requirements stated in bcmills's comment here:
https://golang.org/issue/28136#issuecomment-462971974. It's
important to note that we will not be
adding versions to the repo for now.

This change does not add a go.sum because this repo has no dependencies.

The change was generated by running "go mod init" and "go mod tidy" using Go
1.11.5 in the top-level directory of the repository.

Updates golang/go#28136

Change-Id: Ie4f1270a0dfefd4239badf86daa0d7a5b17fd0d5
Reviewed-on: https://go-review.googlesource.com/c/162823
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
2019-02-27 15:59:43 +00:00
Jean de Klerk
37e7f081c4 semaphore: add more Acquire documentation
Change-Id: Ie64db438145a307ec8aef8fd412d445a2fe74d40
Reviewed-on: https://go-review.googlesource.com/c/136995
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-12-21 19:32:16 +00:00
Jean de Klerk
42b317875d all: remove x/net/context in favour of context
Change-Id: Idbfe40e7cb9f99ca3dc7d4c214de6b9e028eefb7
Reviewed-on: https://go-review.googlesource.com/c/148277
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-11-08 01:04:31 +00:00
Andrew Bonventre
1d60e4601c CONTRIBUTING.md: remove note about not accepting Pull Requests
Updates golang/go#24185

Change-Id: I892f30f02f9ae40554790e9c177cb79d193bffd5
Reviewed-on: https://go-review.googlesource.com/100696
Reviewed-by: Andrew Bonventre <andybons@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2018-03-14 18:01:46 +00:00
Bryan C. Mills
fd80eb99c8 semaphore: add worker-pool example
I've commented several times in various forums that basically every
time I've seen the “worker goroutine” pattern in Go, there has turned
out to be a cleaner implementation using semaphores.

This change adds a simple such example. (For more complex usage, I
would generally pair the semaphore with an errgroup.Group.)

Change-Id: Ibf69ee761d14ba59c1acc6a2d595b4fcf0d8f6d6
Reviewed-on: https://go-review.googlesource.com/75170
Reviewed-by: Ross Light <light@google.com>
2017-11-01 21:47:15 +00:00
Kevin Burke
8e0aa688b6 README: switch to Markdown, add better links
Move the README to README.md so Gerrit can render it; currently
Gerrit only renders files named exactly "README.md" (for example at
https://go.googlesource.com/go).

Add more links to the README explaining how to file issues,
how to submit code changes, where to download the code to and
how to get it. Hopefully this should help people who go to
https://go.googlesource.com/sync or https://github.com/golang/sync
figure out how to get started with development.

Change-Id: Iafdbec407bd6f61459aabe6e2c4703132d038b69
Reviewed-on: https://go-review.googlesource.com/49872
Reviewed-by: Kevin Burke <kev@inburke.com>
2017-09-27 05:41:12 +00:00
Jonathan Amsterdam
f52d1811a6 semaphore: make test time independent of GOMAXPROCS
TestWeighted's runtime was roughly proportional to the number of CPUs,
so it ran a long time on PPCs (160 CPUs).

Make the number of loops in the test an inverse function of the number
of CPUs, to keep the test short.

Change-Id: Id853dbb5e5e2f9fb95966d19ef0c511e3f8080e0
Reviewed-on: https://go-review.googlesource.com/43632
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-05-17 21:12:32 +00:00
Bryan C. Mills
57af736625 syncmap: release m.mu during (*RWMutexMap).Range callbacks
The mainline syncmap.Map has allowed mutations within Range callbacks
since https://golang.org/cl/37342. The reference
implementations used in map_bench_test need to do the same.

Change-Id: Id73d254fa01cc64a1f00eb1903488796e1282423
Reviewed-on: https://go-review.googlesource.com/42956
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-05-10 20:14:52 +00:00
Bryan C. Mills
305c7b3edb syncmap: backport CL 42095
See https://go-review.googlesource.com/c/42095/.

Change-Id: I9f15a785bb4bd28ab02f17de65c63ad4b6e083c9
Reviewed-on: https://go-review.googlesource.com/42594
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-05-04 18:26:18 +00:00
Jonathan Amsterdam
de49d9dcd2 semaphore: avoid loop variable capture by closure
Change-Id: I21c5dc48ffe43aa27c50a42f9c9a01ac1f0c747c
Reviewed-on: https://go-review.googlesource.com/40991
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Glenn Lewis <gmlewis@google.com>
2017-04-18 21:08:38 +00:00
Jonathan Amsterdam
5a06fca2c3 semaphore: add a weighted semaphore implementation
This package provides a weighted semaphore that is context-aware.

The code is derived from a similar package inside Google.

Change-Id: Id1dad96d79e8ccfd289e4299e8265aa5bdad3a5b
Reviewed-on: https://go-review.googlesource.com/38298
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-03-17 23:16:01 +00:00
Bryan C. Mills
a60ad46e0e syncmap: remove blocking for all operations on existing keys
In the previous version, Loads of keys with unchanged values would
block on the Mutex if the map was dirty, leading to potentially long
stalls while the read-only map is being copied to the dirty map.

This change adds a double pointer indirection to each entry and an
extra atomic load on the fast path. In exchange, it removes the need
to lock the map for nearly all operations on existing keys.

Each entry is represented by an atomically-updated pointer to an
interface{} value. The same entries are stored in both the read-only
and dirty maps. Keys deleted before the dirty map was last copied are
marked as "expunged" and omitted from the dirty map until the next
Store to that key.

Newly-stored values exist only in the dirty map. The existence of new
keys in the dirty map is indicated by an "amended" bit attached to the
read-only map, allowing Load or Delete calls for nonexistent keys to
sometimes return immediately (without acquiring the Mutex).

This trades off a bit of steady-state throughput in the "mostly hits"
case in exchange for less contention (and lower Load tail latencies)
for mixed Load/Store/Delete usage.

Unfortunately, the latency impact is currently difficult to measure
(#19128).

updates golang/go#19128
updates golang/go#18177

https://perf.golang.org/search?q=upload:20170315.5

name                                               old time/op    new time/op    delta
LoadMostlyHits/*syncmap_test.DeepCopyMap             70.3ns ± 6%    70.2ns ± 4%      ~     (p=0.886 n=4+4)
LoadMostlyHits/*syncmap_test.DeepCopyMap-48          10.9ns ± 4%    13.6ns ±24%      ~     (p=0.314 n=4+4)
LoadMostlyHits/*syncmap_test.RWMutexMap              86.0ns ± 9%    86.0ns ± 4%      ~     (p=1.000 n=4+4)
LoadMostlyHits/*syncmap_test.RWMutexMap-48            140ns ± 3%     139ns ± 2%      ~     (p=0.686 n=4+4)
LoadMostlyHits/*syncmap.Map                          70.6ns ± 8%    70.3ns ± 1%      ~     (p=0.571 n=4+4)
LoadMostlyHits/*syncmap.Map-48                       10.3ns ± 7%    11.2ns ± 5%      ~     (p=0.114 n=4+4)
LoadMostlyMisses/*syncmap_test.DeepCopyMap           59.4ns ± 4%    59.4ns ± 4%      ~     (p=1.000 n=4+4)
LoadMostlyMisses/*syncmap_test.DeepCopyMap-48        10.4ns ±30%    12.6ns ±29%      ~     (p=0.486 n=4+4)
LoadMostlyMisses/*syncmap_test.RWMutexMap            64.8ns ± 6%    63.8ns ± 4%      ~     (p=0.686 n=4+4)
LoadMostlyMisses/*syncmap_test.RWMutexMap-48          138ns ± 3%     139ns ± 2%      ~     (p=0.800 n=4+4)
LoadMostlyMisses/*syncmap.Map                        51.5ns ± 7%    50.4ns ± 2%      ~     (p=0.371 n=4+4)
LoadMostlyMisses/*syncmap.Map-48                     9.37ns ± 3%    9.40ns ± 3%      ~     (p=0.886 n=4+4)
LoadOrStoreBalanced/*syncmap_test.RWMutexMap          820ns ±17%     834ns ±14%      ~     (p=1.000 n=4+4)
LoadOrStoreBalanced/*syncmap_test.RWMutexMap-48      1.38µs ± 4%    1.38µs ± 3%      ~     (p=0.886 n=4+4)
LoadOrStoreBalanced/*syncmap.Map                      709ns ±13%    1085ns ± 9%   +53.09%  (p=0.029 n=4+4)
LoadOrStoreBalanced/*syncmap.Map-48                  1.30µs ±13%    1.08µs ± 8%   -17.40%  (p=0.029 n=4+4)
LoadOrStoreUnique/*syncmap_test.RWMutexMap           1.26µs ±14%    1.16µs ±29%      ~     (p=0.343 n=4+4)
LoadOrStoreUnique/*syncmap_test.RWMutexMap-48        1.82µs ±15%    1.98µs ± 6%      ~     (p=0.143 n=4+4)
LoadOrStoreUnique/*syncmap.Map                       1.06µs ± 7%    1.86µs ±11%   +76.09%  (p=0.029 n=4+4)
LoadOrStoreUnique/*syncmap.Map-48                    2.00µs ± 4%    1.62µs ± 4%   -19.20%  (p=0.029 n=4+4)
LoadOrStoreCollision/*syncmap_test.DeepCopyMap       32.9ns ± 8%    32.6ns ± 9%      ~     (p=0.686 n=4+4)
LoadOrStoreCollision/*syncmap_test.DeepCopyMap-48   2.26ns ±136%    3.41ns ±62%      ~     (p=0.114 n=4+4)
LoadOrStoreCollision/*syncmap_test.RWMutexMap        58.0ns ±20%    54.6ns ±17%      ~     (p=0.343 n=4+4)
LoadOrStoreCollision/*syncmap_test.RWMutexMap-48      458ns ± 2%     445ns ± 6%      ~     (p=0.229 n=4+4)
LoadOrStoreCollision/*syncmap.Map                    35.8ns ± 5%    39.6ns ± 9%      ~     (p=0.114 n=4+4)
LoadOrStoreCollision/*syncmap.Map-48                 1.24ns ± 2%    1.58ns ± 4%   +27.16%  (p=0.029 n=4+4)
Range/*syncmap_test.DeepCopyMap                      19.7µs ± 4%    19.8µs ± 3%      ~     (p=0.686 n=4+4)
Range/*syncmap_test.DeepCopyMap-48                    763ns ± 1%     864ns ± 3%   +13.24%  (p=0.029 n=4+4)
Range/*syncmap_test.RWMutexMap                       20.9µs ± 3%    20.4µs ± 4%      ~     (p=0.343 n=4+4)
Range/*syncmap_test.RWMutexMap-48                     764ns ± 1%     870ns ± 1%   +13.77%  (p=0.029 n=4+4)
Range/*syncmap.Map                                   20.4µs ± 5%    22.7µs ± 5%   +10.98%  (p=0.029 n=4+4)
Range/*syncmap.Map-48                                 776ns ± 3%     954ns ± 4%   +22.95%  (p=0.029 n=4+4)
AdversarialAlloc/*syncmap_test.DeepCopyMap            206ns ± 5%     199ns ± 2%      ~     (p=0.200 n=4+4)
AdversarialAlloc/*syncmap_test.DeepCopyMap-48        8.94µs ± 5%    9.21µs ± 4%      ~     (p=0.200 n=4+4)
AdversarialAlloc/*syncmap_test.RWMutexMap            63.4ns ± 4%    63.8ns ± 3%      ~     (p=0.686 n=4+4)
AdversarialAlloc/*syncmap_test.RWMutexMap-48          184ns ±10%     198ns ±11%      ~     (p=0.343 n=4+4)
AdversarialAlloc/*syncmap.Map                         213ns ± 3%     264ns ± 3%   +23.97%  (p=0.029 n=4+4)
AdversarialAlloc/*syncmap.Map-48                      556ns ± 5%    1389ns ± 8%  +149.93%  (p=0.029 n=4+4)
AdversarialDelete/*syncmap_test.DeepCopyMap           300ns ± 6%     304ns ± 7%      ~     (p=0.886 n=4+4)
AdversarialDelete/*syncmap_test.DeepCopyMap-48        647ns ± 3%     646ns ± 3%      ~     (p=0.943 n=4+4)
AdversarialDelete/*syncmap_test.RWMutexMap           69.1ns ± 1%    69.2ns ± 6%      ~     (p=0.686 n=4+4)
AdversarialDelete/*syncmap_test.RWMutexMap-48         289ns ±15%     300ns ±17%      ~     (p=0.829 n=4+4)
AdversarialDelete/*syncmap.Map                        198ns ± 5%     264ns ± 2%   +33.17%  (p=0.029 n=4+4)
AdversarialDelete/*syncmap.Map-48                     291ns ± 9%     173ns ± 8%   -40.50%  (p=0.029 n=4+4)

name                                               old alloc/op   new alloc/op   delta
LoadMostlyHits/*syncmap_test.DeepCopyMap              7.00B ± 0%     7.00B ± 0%      ~     (all equal)
LoadMostlyHits/*syncmap_test.DeepCopyMap-48           7.00B ± 0%     7.00B ± 0%      ~     (all equal)
LoadMostlyHits/*syncmap_test.RWMutexMap               7.00B ± 0%     7.00B ± 0%      ~     (all equal)
LoadMostlyHits/*syncmap_test.RWMutexMap-48            7.00B ± 0%     7.00B ± 0%      ~     (all equal)
LoadMostlyHits/*syncmap.Map                           7.00B ± 0%     7.00B ± 0%      ~     (all equal)
LoadMostlyHits/*syncmap.Map-48                        7.00B ± 0%     7.00B ± 0%      ~     (all equal)
LoadMostlyMisses/*syncmap_test.DeepCopyMap            7.00B ± 0%     7.00B ± 0%      ~     (all equal)
LoadMostlyMisses/*syncmap_test.DeepCopyMap-48         7.00B ± 0%     7.00B ± 0%      ~     (all equal)
LoadMostlyMisses/*syncmap_test.RWMutexMap             7.00B ± 0%     7.00B ± 0%      ~     (all equal)
LoadMostlyMisses/*syncmap_test.RWMutexMap-48          7.00B ± 0%     7.00B ± 0%      ~     (all equal)
LoadMostlyMisses/*syncmap.Map                         7.00B ± 0%     7.00B ± 0%      ~     (all equal)
LoadMostlyMisses/*syncmap.Map-48                      7.00B ± 0%     7.00B ± 0%      ~     (all equal)
LoadOrStoreBalanced/*syncmap_test.RWMutexMap          95.0B ± 0%     95.0B ± 0%      ~     (all equal)
LoadOrStoreBalanced/*syncmap_test.RWMutexMap-48       95.0B ± 0%     95.0B ± 0%      ~     (all equal)
LoadOrStoreBalanced/*syncmap.Map                      95.0B ± 0%     88.0B ± 0%    -7.37%  (p=0.029 n=4+4)
LoadOrStoreBalanced/*syncmap.Map-48                   95.0B ± 0%     88.0B ± 0%    -7.37%  (p=0.029 n=4+4)
LoadOrStoreUnique/*syncmap_test.RWMutexMap             175B ± 0%      175B ± 0%      ~     (all equal)
LoadOrStoreUnique/*syncmap_test.RWMutexMap-48          175B ± 0%      175B ± 0%      ~     (all equal)
LoadOrStoreUnique/*syncmap.Map                         175B ± 0%      161B ± 0%    -8.00%  (p=0.029 n=4+4)
LoadOrStoreUnique/*syncmap.Map-48                      175B ± 0%      161B ± 0%    -8.00%  (p=0.029 n=4+4)
LoadOrStoreCollision/*syncmap_test.DeepCopyMap        0.00B          0.00B           ~     (all equal)
LoadOrStoreCollision/*syncmap_test.DeepCopyMap-48     0.00B          0.00B           ~     (all equal)
LoadOrStoreCollision/*syncmap_test.RWMutexMap         0.00B          0.00B           ~     (all equal)
LoadOrStoreCollision/*syncmap_test.RWMutexMap-48      0.00B          0.00B           ~     (all equal)
LoadOrStoreCollision/*syncmap.Map                     0.00B          0.00B           ~     (all equal)
LoadOrStoreCollision/*syncmap.Map-48                  0.00B          0.00B           ~     (all equal)
Range/*syncmap_test.DeepCopyMap                       0.00B          0.00B           ~     (all equal)
Range/*syncmap_test.DeepCopyMap-48                    0.00B          0.00B           ~     (all equal)
Range/*syncmap_test.RWMutexMap                        0.00B          0.00B           ~     (all equal)
Range/*syncmap_test.RWMutexMap-48                     0.00B          0.00B           ~     (all equal)
Range/*syncmap.Map                                    0.00B          0.00B           ~     (all equal)
Range/*syncmap.Map-48                                 0.00B          0.00B           ~     (all equal)
AdversarialAlloc/*syncmap_test.DeepCopyMap            74.0B ± 0%     74.0B ± 0%      ~     (all equal)
AdversarialAlloc/*syncmap_test.DeepCopyMap-48        3.15kB ± 0%    3.15kB ± 0%      ~     (p=1.000 n=4+4)
AdversarialAlloc/*syncmap_test.RWMutexMap             8.00B ± 0%     8.00B ± 0%      ~     (all equal)
AdversarialAlloc/*syncmap_test.RWMutexMap-48          8.00B ± 0%     8.00B ± 0%      ~     (all equal)
AdversarialAlloc/*syncmap.Map                         74.0B ± 0%     55.0B ± 0%   -25.68%  (p=0.029 n=4+4)
AdversarialAlloc/*syncmap.Map-48                      8.00B ± 0%    56.25B ± 1%  +603.12%  (p=0.029 n=4+4)
AdversarialDelete/*syncmap_test.DeepCopyMap            155B ± 0%      155B ± 0%      ~     (all equal)
AdversarialDelete/*syncmap_test.DeepCopyMap-48         156B ± 0%      156B ± 0%      ~     (p=1.000 n=4+4)
AdversarialDelete/*syncmap_test.RWMutexMap            8.00B ± 0%     8.00B ± 0%      ~     (all equal)
AdversarialDelete/*syncmap_test.RWMutexMap-48         8.00B ± 0%     8.00B ± 0%      ~     (all equal)
AdversarialDelete/*syncmap.Map                        81.0B ± 0%     65.0B ± 0%   -19.75%  (p=0.029 n=4+4)
AdversarialDelete/*syncmap.Map-48                     23.0B ± 9%     15.2B ± 5%   -33.70%  (p=0.029 n=4+4)

name                                               old allocs/op  new allocs/op  delta
LoadMostlyHits/*syncmap_test.DeepCopyMap               0.00           0.00           ~     (all equal)
LoadMostlyHits/*syncmap_test.DeepCopyMap-48            0.00           0.00           ~     (all equal)
LoadMostlyHits/*syncmap_test.RWMutexMap                0.00           0.00           ~     (all equal)
LoadMostlyHits/*syncmap_test.RWMutexMap-48             0.00           0.00           ~     (all equal)
LoadMostlyHits/*syncmap.Map                            0.00           0.00           ~     (all equal)
LoadMostlyHits/*syncmap.Map-48                         0.00           0.00           ~     (all equal)
LoadMostlyMisses/*syncmap_test.DeepCopyMap             0.00           0.00           ~     (all equal)
LoadMostlyMisses/*syncmap_test.DeepCopyMap-48          0.00           0.00           ~     (all equal)
LoadMostlyMisses/*syncmap_test.RWMutexMap              0.00           0.00           ~     (all equal)
LoadMostlyMisses/*syncmap_test.RWMutexMap-48           0.00           0.00           ~     (all equal)
LoadMostlyMisses/*syncmap.Map                          0.00           0.00           ~     (all equal)
LoadMostlyMisses/*syncmap.Map-48                       0.00           0.00           ~     (all equal)
LoadOrStoreBalanced/*syncmap_test.RWMutexMap           2.00 ± 0%      2.00 ± 0%      ~     (all equal)
LoadOrStoreBalanced/*syncmap_test.RWMutexMap-48        2.00 ± 0%      2.00 ± 0%      ~     (all equal)
LoadOrStoreBalanced/*syncmap.Map                       2.00 ± 0%      3.00 ± 0%   +50.00%  (p=0.029 n=4+4)
LoadOrStoreBalanced/*syncmap.Map-48                    2.00 ± 0%      3.00 ± 0%   +50.00%  (p=0.029 n=4+4)
LoadOrStoreUnique/*syncmap_test.RWMutexMap             2.00 ± 0%      2.00 ± 0%      ~     (all equal)
LoadOrStoreUnique/*syncmap_test.RWMutexMap-48          2.00 ± 0%      2.00 ± 0%      ~     (all equal)
LoadOrStoreUnique/*syncmap.Map                         2.00 ± 0%      4.00 ± 0%  +100.00%  (p=0.029 n=4+4)
LoadOrStoreUnique/*syncmap.Map-48                      2.00 ± 0%      4.00 ± 0%  +100.00%  (p=0.029 n=4+4)
LoadOrStoreCollision/*syncmap_test.DeepCopyMap         0.00           0.00           ~     (all equal)
LoadOrStoreCollision/*syncmap_test.DeepCopyMap-48      0.00           0.00           ~     (all equal)
LoadOrStoreCollision/*syncmap_test.RWMutexMap          0.00           0.00           ~     (all equal)
LoadOrStoreCollision/*syncmap_test.RWMutexMap-48       0.00           0.00           ~     (all equal)
LoadOrStoreCollision/*syncmap.Map                      0.00           0.00           ~     (all equal)
LoadOrStoreCollision/*syncmap.Map-48                   0.00           0.00           ~     (all equal)
Range/*syncmap_test.DeepCopyMap                        0.00           0.00           ~     (all equal)
Range/*syncmap_test.DeepCopyMap-48                     0.00           0.00           ~     (all equal)
Range/*syncmap_test.RWMutexMap                         0.00           0.00           ~     (all equal)
Range/*syncmap_test.RWMutexMap-48                      0.00           0.00           ~     (all equal)
Range/*syncmap.Map                                     0.00           0.00           ~     (all equal)
Range/*syncmap.Map-48                                  0.00           0.00           ~     (all equal)
AdversarialAlloc/*syncmap_test.DeepCopyMap             1.00 ± 0%      1.00 ± 0%      ~     (all equal)
AdversarialAlloc/*syncmap_test.DeepCopyMap-48          1.00 ± 0%      1.00 ± 0%      ~     (all equal)
AdversarialAlloc/*syncmap_test.RWMutexMap              1.00 ± 0%      1.00 ± 0%      ~     (all equal)
AdversarialAlloc/*syncmap_test.RWMutexMap-48           1.00 ± 0%      1.00 ± 0%      ~     (all equal)
AdversarialAlloc/*syncmap.Map                          1.00 ± 0%      1.00 ± 0%      ~     (all equal)
AdversarialAlloc/*syncmap.Map-48                       1.00 ± 0%      1.00 ± 0%      ~     (all equal)
AdversarialDelete/*syncmap_test.DeepCopyMap            1.00 ± 0%      1.00 ± 0%      ~     (all equal)
AdversarialDelete/*syncmap_test.DeepCopyMap-48         1.00 ± 0%      1.00 ± 0%      ~     (all equal)
AdversarialDelete/*syncmap_test.RWMutexMap             1.00 ± 0%      1.00 ± 0%      ~     (all equal)
AdversarialDelete/*syncmap_test.RWMutexMap-48          1.00 ± 0%      1.00 ± 0%      ~     (all equal)
AdversarialDelete/*syncmap.Map                         1.00 ± 0%      1.00 ± 0%      ~     (all equal)
AdversarialDelete/*syncmap.Map-48                      1.00 ± 0%      1.00 ± 0%      ~     (all equal)

Change-Id: I93c9458505d84238cc8e9016a7dfd285868af236
Reviewed-on: https://go-review.googlesource.com/37342
Reviewed-by: Russ Cox <rsc@golang.org>
2017-03-15 18:56:49 +00:00
Bryan C. Mills
86ddc858aa syncmap: make quick-check output more readable
Add a test for Range with concurrent Loads and Stores.

The previous quick-check tests generated long, mostly-non-ASCII
strings that were hard to debug on failure; this change makes the keys
and values short and human-readable, which also tends to produce more
key collisions in the test as a side-effect.

updates golang/go#18177

Change-Id: Ie56a64ec9fe295435682b90c3e6466ed5b349bf9
Reviewed-on: https://go-review.googlesource.com/37150
Reviewed-by: Russ Cox <rsc@golang.org>
2017-02-16 19:22:36 +00:00
Bryan C. Mills
37569ff763 syncmap: add benchmark for Range
adjust BenchmarkAdversarialDelete to be somewhat more adversarial.

updates golang/go#18177

Change-Id: Id01ed1077a0447dcfc6ea3929c22baaddbc9d6ee
Reviewed-on: https://go-review.googlesource.com/37151
Reviewed-by: Russ Cox <rsc@golang.org>
2017-02-16 19:20:33 +00:00
Bryan C. Mills
54b13b0b03 syncmap: add a synchronized map implementation.
This is a draft for the sync.Map API proposed in golang/go#18177.

It supports fast-path loads via an atomic variable, falling back to a
Mutex for stores. In order to keep stores amortized to O(1), loads
following a store follow the Mutex path until enough loads have
occurred to offset the cost of a deep copy.

For mostly-read loads, such as the maps in the reflect package in the
standard library, this significantly reduces cache-line contention vs.
a plain RWMutex with a map.

goos: linux
goarch: amd64
pkg: golang.org/x/sync/syncmap
BenchmarkLoadMostlyHits/*syncmap_test.DeepCopyMap               20000000                73.1 ns/op
BenchmarkLoadMostlyHits/*syncmap_test.DeepCopyMap-48            100000000               13.8 ns/op
BenchmarkLoadMostlyHits/*syncmap_test.RWMutexMap                20000000                87.7 ns/op
BenchmarkLoadMostlyHits/*syncmap_test.RWMutexMap-48             10000000               154 ns/op
BenchmarkLoadMostlyHits/*syncmap.Map                            20000000                72.1 ns/op
BenchmarkLoadMostlyHits/*syncmap.Map-48                         100000000               11.2 ns/op
BenchmarkLoadMostlyMisses/*syncmap_test.DeepCopyMap             20000000                63.2 ns/op
BenchmarkLoadMostlyMisses/*syncmap_test.DeepCopyMap-48          200000000               14.2 ns/op
BenchmarkLoadMostlyMisses/*syncmap_test.RWMutexMap              20000000                72.7 ns/op
BenchmarkLoadMostlyMisses/*syncmap_test.RWMutexMap-48           10000000               150 ns/op
BenchmarkLoadMostlyMisses/*syncmap.Map                          30000000                56.4 ns/op
BenchmarkLoadMostlyMisses/*syncmap.Map-48                       200000000                9.77 ns/op
BenchmarkLoadOrStoreBalanced/*syncmap_test.RWMutexMap            2000000               683 ns/op
BenchmarkLoadOrStoreBalanced/*syncmap_test.RWMutexMap-48         1000000              1394 ns/op
BenchmarkLoadOrStoreBalanced/*syncmap.Map                        2000000               645 ns/op
BenchmarkLoadOrStoreBalanced/*syncmap.Map-48                     1000000              1253 ns/op
BenchmarkLoadOrStoreUnique/*syncmap_test.RWMutexMap              1000000              1015 ns/op
BenchmarkLoadOrStoreUnique/*syncmap_test.RWMutexMap-48           1000000              1911 ns/op
BenchmarkLoadOrStoreUnique/*syncmap.Map                          1000000              1018 ns/op
BenchmarkLoadOrStoreUnique/*syncmap.Map-48                       1000000              1776 ns/op
BenchmarkLoadOrStoreCollision/*syncmap_test.DeepCopyMap         50000000                30.2 ns/op
BenchmarkLoadOrStoreCollision/*syncmap_test.DeepCopyMap-48      2000000000               1.24 ns/op
BenchmarkLoadOrStoreCollision/*syncmap_test.RWMutexMap          30000000                50.1 ns/op
BenchmarkLoadOrStoreCollision/*syncmap_test.RWMutexMap-48        5000000               451 ns/op
BenchmarkLoadOrStoreCollision/*syncmap.Map                      30000000                36.8 ns/op
BenchmarkLoadOrStoreCollision/*syncmap.Map-48                   2000000000               1.24 ns/op
BenchmarkAdversarialAlloc/*syncmap_test.DeepCopyMap             10000000               213 ns/op
BenchmarkAdversarialAlloc/*syncmap_test.DeepCopyMap-48           1000000              5012 ns/op
BenchmarkAdversarialAlloc/*syncmap_test.RWMutexMap              20000000                68.8 ns/op
BenchmarkAdversarialAlloc/*syncmap_test.RWMutexMap-48            5000000               429 ns/op
BenchmarkAdversarialAlloc/*syncmap.Map                           5000000               229 ns/op
BenchmarkAdversarialAlloc/*syncmap.Map-48                        2000000               600 ns/op
BenchmarkAdversarialDelete/*syncmap_test.DeepCopyMap             5000000               314 ns/op
BenchmarkAdversarialDelete/*syncmap_test.DeepCopyMap-48          2000000               726 ns/op
BenchmarkAdversarialDelete/*syncmap_test.RWMutexMap             20000000                63.2 ns/op
BenchmarkAdversarialDelete/*syncmap_test.RWMutexMap-48           5000000               469 ns/op
BenchmarkAdversarialDelete/*syncmap.Map                         10000000               203 ns/op
BenchmarkAdversarialDelete/*syncmap.Map-48                      10000000               253 ns/op

goos: linux
goarch: ppc64le
pkg: golang.org/x/sync/syncmap
BenchmarkLoadMostlyHits/*syncmap_test.DeepCopyMap                5000000               253 ns/op
BenchmarkLoadMostlyHits/*syncmap_test.DeepCopyMap-48            50000000                26.2 ns/op
BenchmarkLoadMostlyHits/*syncmap_test.RWMutexMap                 5000000               505 ns/op
BenchmarkLoadMostlyHits/*syncmap_test.RWMutexMap-48              3000000               443 ns/op
BenchmarkLoadMostlyHits/*syncmap.Map                            10000000               200 ns/op
BenchmarkLoadMostlyHits/*syncmap.Map-48                         100000000               18.1 ns/op
BenchmarkLoadMostlyMisses/*syncmap_test.DeepCopyMap             10000000               162 ns/op
BenchmarkLoadMostlyMisses/*syncmap_test.DeepCopyMap-48          100000000               23.8 ns/op
BenchmarkLoadMostlyMisses/*syncmap_test.RWMutexMap              10000000               195 ns/op
BenchmarkLoadMostlyMisses/*syncmap_test.RWMutexMap-48            3000000               531 ns/op
BenchmarkLoadMostlyMisses/*syncmap.Map                          10000000               182 ns/op
BenchmarkLoadMostlyMisses/*syncmap.Map-48                       100000000               15.8 ns/op
BenchmarkLoadOrStoreBalanced/*syncmap_test.RWMutexMap            1000000              1664 ns/op
BenchmarkLoadOrStoreBalanced/*syncmap_test.RWMutexMap-48         1000000              1768 ns/op
BenchmarkLoadOrStoreBalanced/*syncmap.Map                        1000000              2128 ns/op
BenchmarkLoadOrStoreBalanced/*syncmap.Map-48                     1000000              1903 ns/op
BenchmarkLoadOrStoreUnique/*syncmap_test.RWMutexMap              1000000              2657 ns/op
BenchmarkLoadOrStoreUnique/*syncmap_test.RWMutexMap-48           1000000              2577 ns/op
BenchmarkLoadOrStoreUnique/*syncmap.Map                          1000000              1714 ns/op
BenchmarkLoadOrStoreUnique/*syncmap.Map-48                       1000000              2484 ns/op
BenchmarkLoadOrStoreCollision/*syncmap_test.DeepCopyMap         10000000               130 ns/op
BenchmarkLoadOrStoreCollision/*syncmap_test.DeepCopyMap-48      100000000               11.3 ns/op
BenchmarkLoadOrStoreCollision/*syncmap_test.RWMutexMap           3000000               426 ns/op
BenchmarkLoadOrStoreCollision/*syncmap_test.RWMutexMap-48        2000000               930 ns/op
BenchmarkLoadOrStoreCollision/*syncmap.Map                      10000000               131 ns/op
BenchmarkLoadOrStoreCollision/*syncmap.Map-48                   300000000                4.07 ns/op
BenchmarkAdversarialAlloc/*syncmap_test.DeepCopyMap              3000000               447 ns/op
BenchmarkAdversarialAlloc/*syncmap_test.DeepCopyMap-48            300000              4159 ns/op
BenchmarkAdversarialAlloc/*syncmap_test.RWMutexMap              10000000               191 ns/op
BenchmarkAdversarialAlloc/*syncmap_test.RWMutexMap-48            3000000               535 ns/op
BenchmarkAdversarialAlloc/*syncmap.Map                           2000000               525 ns/op
BenchmarkAdversarialAlloc/*syncmap.Map-48                        1000000              1000 ns/op
BenchmarkAdversarialDelete/*syncmap_test.DeepCopyMap             2000000               711 ns/op
BenchmarkAdversarialDelete/*syncmap_test.DeepCopyMap-48          2000000               900 ns/op
BenchmarkAdversarialDelete/*syncmap_test.RWMutexMap              3000000               354 ns/op
BenchmarkAdversarialDelete/*syncmap_test.RWMutexMap-48           3000000               473 ns/op
BenchmarkAdversarialDelete/*syncmap.Map                          2000000              1357 ns/op
BenchmarkAdversarialDelete/*syncmap.Map-48                       5000000               334 ns/op

Updates golang/go#18177

Change-Id: I8d561b617b1cd2ca03a8e68a5d5a28a519a0ce38
Reviewed-on: https://go-review.googlesource.com/33912
Reviewed-by: Russ Cox <rsc@golang.org>
2017-02-15 17:32:38 +00:00
Josh Bleecher Snyder
450f422ab2 .: add codereview.cfg
This makes git-codereview review issue references
to refer to the main Go repo.

Change-Id: I17003885f15358f776a475a76a31dac179de9676
Reviewed-on: https://go-review.googlesource.com/33908
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-12-06 01:46:32 +00:00
Bryan C. Mills
81567d9de7 errgroup: fix misleading comment in MD5All example
Change-Id: I0c2f0ce9b6006dac13c5f5faed752a2475992805
Reviewed-on: https://go-review.googlesource.com/33851
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-12-01 22:51:26 +00:00