Repository: puzpuzpuz/xsync
Pull request #74 (2022-10-30 07:06:11)

Fix out of range error in Counter on GOMAXPROCS change

codecov-commenter: # [Codecov](https://codecov.io/gh/puzpuzpuz/xsync/pull/74?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andrei+Pechkurov) Report
Base: **96.78**% // Head: **96.78**% // No change to project coverage :thumbsup:
> Coverage data is based on head [(`141c794`)](https://codecov.io/gh/puzpuzpuz/xsync/pull/74?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andrei+Pechkurov) compared to base [(`89fe645`)](https://codecov.io/gh/puzpuzpuz/xsync/commit/89fe6454a4771fc2b87f250a3cc7f3392ecdf12e?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andrei+Pechkurov).
> Patch coverage: 100.00% of modified lines in pull request are covered.

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main      #74   +/-   ##
=======================================
  Coverage   96.78%   96.78%           
=======================================
  Files           6        6           
  Lines         932      932           
=======================================
  Hits          902      902           
  Misses         24       24           
  Partials        6        6           
```


| [Impacted Files](https://codecov.io/gh/puzpuzpuz/xsync/pull/74?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andrei+Pechkurov) | Coverage Δ | |
|---|---|---|
| [counter.go](https://codecov.io/gh/puzpuzpuz/xsync/pull/74/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andrei+Pechkurov#diff-Y291bnRlci5nbw==) | `100.00% <100.00%> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andrei+Pechkurov). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andrei+Pechkurov)

</details>

[:umbrella: View full report at Codecov](https://codecov.io/gh/puzpuzpuz/xsync/pull/74?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andrei+Pechkurov).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Andrei+Pechkurov).


Patch:
diff --git a/counter.go b/counter.go
index bd078ec..5f4989d 100644
--- a/counter.go
+++ b/counter.go
@@ -59,16 +59,16 @@ func (c *Counter) Add(delta int64) {
 	t, ok := ptokenPool.Get().(*ptoken)
 	if !ok {
 		t = new(ptoken)
-		t.idx = fastrand() & c.mask
+		t.idx = fastrand()
 	}
 	for {
-		stripe := &c.stripes[t.idx]
+		stripe := &c.stripes[t.idx&c.mask]
 		cnt := atomic.LoadInt64(&stripe.c)
 		if atomic.CompareAndSwapInt64(&stripe.c, cnt, cnt+delta) {
 			break
 		}
 		// Give a try with another randomly selected stripe.
-		t.idx = fastrand() & c.mask
+		t.idx = fastrand()
 	}
 	ptokenPool.Put(t)
 }


Test patch:
diff --git a/counter_test.go b/counter_test.go
index 2564852..7f2faee 100644
--- a/counter_test.go
+++ b/counter_test.go
@@ -51,7 +51,7 @@ func TestCounterReset(t *testing.T) {
 }
 
 func parallelIncrementor(c *Counter, numIncs int, cdone chan bool) {
-	for i := 0; i < 10000; i++ {
+	for i := 0; i < numIncs; i++ {
 		c.Inc()
 	}
 	cdone <- true
@@ -61,7 +61,7 @@ func doTestParallelIncrementors(t *testing.T, numModifiers, gomaxprocs int) {
 	runtime.GOMAXPROCS(gomaxprocs)
 	c := NewCounter()
 	cdone := make(chan bool)
-	numIncs := 10000
+	numIncs := 10_000
 	for i := 0; i < numModifiers; i++ {
 		go parallelIncrementor(c, numIncs, cdone)
 	}