Source file src/sync/counter_bench_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package sync_test
     6  
     7  import (
     8  	"math"
     9  	"runtime"
    10  	"sync/atomic"
    11  	"testing"
    12  	"unsafe"
    13  
    14  	"internal/cpu"
    15  	"sync"
    16  )
    17  
    18  // Sub-benchmarks use the "key=value" naming convention so benchstat can
    19  // group and compare results across dimensions:
    20  //   - impl:  counter implementation ("atomic" or "counter")
    21  //   - op:    operation, can include a mixture of ops
    22  //     ("Add", "Value", "value:add=1:99", "value:add=10:90")
    23  //   - value: counter value type ("int64", "uint64", or "float64")
    24  //   - par:   parallelism ("single" goroutine or "multi" via RunParallel)
    25  func BenchmarkCounter(b *testing.B) {
    26  	b.Run("impl=atomic/op=Add/value=int64/par=single", func(b *testing.B) {
    27  		var c atomic.Int64
    28  		b.ResetTimer()
    29  		for range b.N {
    30  			c.Add(1)
    31  		}
    32  	})
    33  
    34  	b.Run("impl=counter/op=Add/value=int64/par=single", func(b *testing.B) {
    35  		var c sync.Counter[int64]
    36  		b.ResetTimer()
    37  		for range b.N {
    38  			c.Add(1)
    39  		}
    40  	})
    41  
    42  	b.Run("impl=atomic/op=Add/value=uint64/par=single", func(b *testing.B) {
    43  		var c atomic.Uint64
    44  		b.ResetTimer()
    45  		for range b.N {
    46  			c.Add(1)
    47  		}
    48  	})
    49  
    50  	b.Run("impl=counter/op=Add/value=uint64/par=single", func(b *testing.B) {
    51  		var c sync.Counter[uint64]
    52  		b.ResetTimer()
    53  		for range b.N {
    54  			c.Add(1)
    55  		}
    56  	})
    57  
    58  	b.Run("impl=atomic/op=Add/value=float64/par=single", func(b *testing.B) {
    59  		var c atomicFloat64
    60  		b.ResetTimer()
    61  		for range b.N {
    62  			c.Add(1)
    63  		}
    64  	})
    65  
    66  	b.Run("impl=counter/op=Add/value=float64/par=single", func(b *testing.B) {
    67  		var c sync.Counter[float64]
    68  		b.ResetTimer()
    69  		for range b.N {
    70  			c.Add(1)
    71  		}
    72  	})
    73  
    74  	b.Run("impl=atomic/op=Value/value=int64/par=single", func(b *testing.B) {
    75  		var c atomic.Int64
    76  		c.Add(1)
    77  		var sink int64
    78  		b.ResetTimer()
    79  		for range b.N {
    80  			sink = c.Load()
    81  		}
    82  		runtime.KeepAlive(sink)
    83  	})
    84  
    85  	b.Run("impl=counter/op=Value/value=int64/par=single", func(b *testing.B) {
    86  		var c sync.Counter[int64]
    87  		c.Add(1)
    88  		var sink int64
    89  		b.ResetTimer()
    90  		for range b.N {
    91  			sink = c.Value()
    92  		}
    93  		runtime.KeepAlive(sink)
    94  	})
    95  
    96  	b.Run("impl=atomic/op=Value/value=uint64/par=single", func(b *testing.B) {
    97  		var c atomic.Uint64
    98  		c.Add(1)
    99  		var sink uint64
   100  		b.ResetTimer()
   101  		for range b.N {
   102  			sink = c.Load()
   103  		}
   104  		runtime.KeepAlive(sink)
   105  	})
   106  
   107  	b.Run("impl=counter/op=Value/value=uint64/par=single", func(b *testing.B) {
   108  		var c sync.Counter[uint64]
   109  		c.Add(1)
   110  		var sink uint64
   111  		b.ResetTimer()
   112  		for range b.N {
   113  			sink = c.Value()
   114  		}
   115  		runtime.KeepAlive(sink)
   116  	})
   117  
   118  	b.Run("impl=atomic/op=Value/value=float64/par=single", func(b *testing.B) {
   119  		var c atomicFloat64
   120  		c.Add(1)
   121  		var sink float64
   122  		b.ResetTimer()
   123  		for range b.N {
   124  			sink = c.Value()
   125  		}
   126  		runtime.KeepAlive(sink)
   127  	})
   128  
   129  	b.Run("impl=counter/op=Value/value=float64/par=single", func(b *testing.B) {
   130  		var c sync.Counter[float64]
   131  		c.Add(1)
   132  		var sink float64
   133  		b.ResetTimer()
   134  		for range b.N {
   135  			sink = c.Value()
   136  		}
   137  		runtime.KeepAlive(sink)
   138  	})
   139  }
   140  
   141  func BenchmarkCounterParallel(b *testing.B) {
   142  	b.Run("impl=atomic/op=Add/value=int64/par=multi", func(b *testing.B) {
   143  		var c atomic.Int64
   144  		b.ResetTimer()
   145  		b.RunParallel(func(pb *testing.PB) {
   146  			for pb.Next() {
   147  				c.Add(1)
   148  			}
   149  		})
   150  	})
   151  
   152  	b.Run("impl=counter/op=Add/value=int64/par=multi", func(b *testing.B) {
   153  		var c sync.Counter[int64]
   154  		b.ResetTimer()
   155  		b.RunParallel(func(pb *testing.PB) {
   156  			for pb.Next() {
   157  				c.Add(1)
   158  			}
   159  		})
   160  	})
   161  
   162  	b.Run("impl=atomic/op=Add/value=uint64/par=multi", func(b *testing.B) {
   163  		var c atomic.Uint64
   164  		b.ResetTimer()
   165  		b.RunParallel(func(pb *testing.PB) {
   166  			for pb.Next() {
   167  				c.Add(1)
   168  			}
   169  		})
   170  	})
   171  
   172  	b.Run("impl=counter/op=Add/value=uint64/par=multi", func(b *testing.B) {
   173  		var c sync.Counter[uint64]
   174  		b.ResetTimer()
   175  		b.RunParallel(func(pb *testing.PB) {
   176  			for pb.Next() {
   177  				c.Add(1)
   178  			}
   179  		})
   180  	})
   181  
   182  	b.Run("impl=atomic/op=Add/value=float64/par=multi", func(b *testing.B) {
   183  		var c atomicFloat64
   184  		b.ResetTimer()
   185  		b.RunParallel(func(pb *testing.PB) {
   186  			for pb.Next() {
   187  				c.Add(1)
   188  			}
   189  		})
   190  	})
   191  
   192  	b.Run("impl=counter/op=Add/value=float64/par=multi", func(b *testing.B) {
   193  		var c sync.Counter[float64]
   194  		b.ResetTimer()
   195  		b.RunParallel(func(pb *testing.PB) {
   196  			for pb.Next() {
   197  				c.Add(1)
   198  			}
   199  		})
   200  	})
   201  
   202  	b.Run("impl=atomic/op=Value/value=int64/par=multi", func(b *testing.B) {
   203  		var c atomic.Int64
   204  		c.Add(1)
   205  		b.ResetTimer()
   206  		b.RunParallel(func(pb *testing.PB) {
   207  			var sink int64
   208  			for pb.Next() {
   209  				sink = c.Load()
   210  			}
   211  			runtime.KeepAlive(sink)
   212  		})
   213  	})
   214  
   215  	b.Run("impl=counter/op=Value/value=int64/par=multi", func(b *testing.B) {
   216  		var c sync.Counter[int64]
   217  		c.Add(1)
   218  		b.ResetTimer()
   219  		b.RunParallel(func(pb *testing.PB) {
   220  			var sink int64
   221  			for pb.Next() {
   222  				sink = c.Value()
   223  			}
   224  			runtime.KeepAlive(sink)
   225  		})
   226  	})
   227  
   228  	b.Run("impl=atomic/op=Value/value=uint64/par=multi", func(b *testing.B) {
   229  		var c atomic.Uint64
   230  		c.Add(1)
   231  		b.ResetTimer()
   232  		b.RunParallel(func(pb *testing.PB) {
   233  			var sink uint64
   234  			for pb.Next() {
   235  				sink = c.Load()
   236  			}
   237  			runtime.KeepAlive(sink)
   238  		})
   239  	})
   240  
   241  	b.Run("impl=counter/op=Value/value=uint64/par=multi", func(b *testing.B) {
   242  		var c sync.Counter[uint64]
   243  		c.Add(1)
   244  		b.ResetTimer()
   245  		b.RunParallel(func(pb *testing.PB) {
   246  			var sink uint64
   247  			for pb.Next() {
   248  				sink = c.Value()
   249  			}
   250  			runtime.KeepAlive(sink)
   251  		})
   252  	})
   253  
   254  	b.Run("impl=atomic/op=Value/value=float64/par=multi", func(b *testing.B) {
   255  		var c atomicFloat64
   256  		c.Add(1)
   257  		b.ResetTimer()
   258  		b.RunParallel(func(pb *testing.PB) {
   259  			var sink float64
   260  			for pb.Next() {
   261  				sink = c.Value()
   262  			}
   263  			runtime.KeepAlive(sink)
   264  		})
   265  	})
   266  
   267  	b.Run("impl=counter/op=Value/value=float64/par=multi", func(b *testing.B) {
   268  		var c sync.Counter[float64]
   269  		c.Add(1)
   270  		b.ResetTimer()
   271  		b.RunParallel(func(pb *testing.PB) {
   272  			var sink float64
   273  			for pb.Next() {
   274  				sink = c.Value()
   275  			}
   276  			runtime.KeepAlive(sink)
   277  		})
   278  	})
   279  }
   280  
   281  func BenchmarkCounterMixed(b *testing.B) {
   282  	b.Run("impl=atomic/op=value:add=1:99/value=int64/par=multi", func(b *testing.B) {
   283  		var c atomic.Int64
   284  		b.ResetTimer()
   285  		b.RunParallel(func(pb *testing.PB) {
   286  			var i uint32
   287  			for pb.Next() {
   288  				i++
   289  				if i%100 != 0 {
   290  					c.Add(1)
   291  				} else {
   292  					c.Load()
   293  				}
   294  			}
   295  		})
   296  	})
   297  
   298  	b.Run("impl=counter/op=value:add=1:99/value=int64/par=multi", func(b *testing.B) {
   299  		var c sync.Counter[int64]
   300  		b.ResetTimer()
   301  		b.RunParallel(func(pb *testing.PB) {
   302  			var i uint32
   303  			for pb.Next() {
   304  				i++
   305  				if i%100 != 0 {
   306  					c.Add(1)
   307  				} else {
   308  					c.Value()
   309  				}
   310  			}
   311  		})
   312  	})
   313  
   314  	b.Run("impl=atomic/op=value:add=1:99/value=uint64/par=multi", func(b *testing.B) {
   315  		var c atomic.Uint64
   316  		b.ResetTimer()
   317  		b.RunParallel(func(pb *testing.PB) {
   318  			var i uint32
   319  			for pb.Next() {
   320  				i++
   321  				if i%100 != 0 {
   322  					c.Add(1)
   323  				} else {
   324  					c.Load()
   325  				}
   326  			}
   327  		})
   328  	})
   329  
   330  	b.Run("impl=counter/op=value:add=1:99/value=uint64/par=multi", func(b *testing.B) {
   331  		var c sync.Counter[uint64]
   332  		b.ResetTimer()
   333  		b.RunParallel(func(pb *testing.PB) {
   334  			var i uint32
   335  			for pb.Next() {
   336  				i++
   337  				if i%100 != 0 {
   338  					c.Add(1)
   339  				} else {
   340  					c.Value()
   341  				}
   342  			}
   343  		})
   344  	})
   345  
   346  	b.Run("impl=atomic/op=value:add=1:99/value=float64/par=multi", func(b *testing.B) {
   347  		var c atomicFloat64
   348  		b.ResetTimer()
   349  		b.RunParallel(func(pb *testing.PB) {
   350  			var i uint32
   351  			for pb.Next() {
   352  				i++
   353  				if i%100 != 0 {
   354  					c.Add(1)
   355  				} else {
   356  					c.Value()
   357  				}
   358  			}
   359  		})
   360  	})
   361  
   362  	b.Run("impl=counter/op=value:add=1:99/value=float64/par=multi", func(b *testing.B) {
   363  		var c sync.Counter[float64]
   364  		b.ResetTimer()
   365  		b.RunParallel(func(pb *testing.PB) {
   366  			var i uint32
   367  			for pb.Next() {
   368  				i++
   369  				if i%100 != 0 {
   370  					c.Add(1)
   371  				} else {
   372  					c.Value()
   373  				}
   374  			}
   375  		})
   376  	})
   377  
   378  	b.Run("impl=atomic/op=value:add=10:90/value=int64/par=multi", func(b *testing.B) {
   379  		var c atomic.Int64
   380  		b.ResetTimer()
   381  		b.RunParallel(func(pb *testing.PB) {
   382  			var i uint32
   383  			for pb.Next() {
   384  				i++
   385  				if i%100 < 90 {
   386  					c.Add(1)
   387  				} else {
   388  					c.Load()
   389  				}
   390  			}
   391  		})
   392  	})
   393  
   394  	b.Run("impl=counter/op=value:add=10:90/value=int64/par=multi", func(b *testing.B) {
   395  		var c sync.Counter[int64]
   396  		b.ResetTimer()
   397  		b.RunParallel(func(pb *testing.PB) {
   398  			var i uint32
   399  			for pb.Next() {
   400  				i++
   401  				if i%100 < 90 {
   402  					c.Add(1)
   403  				} else {
   404  					c.Value()
   405  				}
   406  			}
   407  		})
   408  	})
   409  
   410  	b.Run("impl=atomic/op=value:add=10:90/value=uint64/par=multi", func(b *testing.B) {
   411  		var c atomic.Uint64
   412  		b.ResetTimer()
   413  		b.RunParallel(func(pb *testing.PB) {
   414  			var i uint32
   415  			for pb.Next() {
   416  				i++
   417  				if i%100 < 90 {
   418  					c.Add(1)
   419  				} else {
   420  					c.Load()
   421  				}
   422  			}
   423  		})
   424  	})
   425  
   426  	b.Run("impl=counter/op=value:add=10:90/value=uint64/par=multi", func(b *testing.B) {
   427  		var c sync.Counter[uint64]
   428  		b.ResetTimer()
   429  		b.RunParallel(func(pb *testing.PB) {
   430  			var i uint32
   431  			for pb.Next() {
   432  				i++
   433  				if i%100 < 90 {
   434  					c.Add(1)
   435  				} else {
   436  					c.Value()
   437  				}
   438  			}
   439  		})
   440  	})
   441  
   442  	b.Run("impl=atomic/op=value:add=10:90/value=float64/par=multi", func(b *testing.B) {
   443  		var c atomicFloat64
   444  		b.ResetTimer()
   445  		b.RunParallel(func(pb *testing.PB) {
   446  			var i uint32
   447  			for pb.Next() {
   448  				i++
   449  				if i%100 < 90 {
   450  					c.Add(1)
   451  				} else {
   452  					c.Value()
   453  				}
   454  			}
   455  		})
   456  	})
   457  
   458  	b.Run("impl=counter/op=value:add=10:90/value=float64/par=multi", func(b *testing.B) {
   459  		var c sync.Counter[float64]
   460  		b.ResetTimer()
   461  		b.RunParallel(func(pb *testing.PB) {
   462  			var i uint32
   463  			for pb.Next() {
   464  				i++
   465  				if i%100 < 90 {
   466  					c.Add(1)
   467  				} else {
   468  					c.Value()
   469  				}
   470  			}
   471  		})
   472  	})
   473  }
   474  
   475  func BenchmarkCounterRandom(b *testing.B) {
   476  	const countPerCacheLine = int(max(unsafe.Sizeof(cpu.CacheLinePad{}), uintptr(64)) / unsafe.Sizeof(uint64(0)))
   477  
   478  	b.Run("impl=atomic/op=value:add=1:99/value=int64/par=multi", func(b *testing.B) {
   479  		c := make([]atomic.Int64, countPerCacheLine)
   480  		b.ResetTimer()
   481  		b.RunParallel(func(pb *testing.PB) {
   482  			var i uint32 = 1
   483  			for pb.Next() {
   484  				i ^= i << 13
   485  				i ^= i >> 17
   486  				i ^= i << 5
   487  				if i%100 != 0 {
   488  					c[int(i)%countPerCacheLine].Add(1)
   489  				} else {
   490  					c[int(i)%countPerCacheLine].Load()
   491  				}
   492  			}
   493  		})
   494  	})
   495  
   496  	b.Run("impl=counter/op=value:add=1:99/value=int64/par=multi", func(b *testing.B) {
   497  		c := make([]sync.Counter[int64], countPerCacheLine)
   498  		b.ResetTimer()
   499  		b.RunParallel(func(pb *testing.PB) {
   500  			var i uint32 = 1
   501  			for pb.Next() {
   502  				i ^= i << 13
   503  				i ^= i >> 17
   504  				i ^= i << 5
   505  				if i%100 != 0 {
   506  					c[int(i)%countPerCacheLine].Add(1)
   507  				} else {
   508  					c[int(i)%countPerCacheLine].Value()
   509  				}
   510  			}
   511  		})
   512  	})
   513  
   514  	b.Run("impl=atomic/op=value:add=1:99/value=uint64/par=multi", func(b *testing.B) {
   515  		c := make([]atomic.Uint64, countPerCacheLine)
   516  		b.ResetTimer()
   517  		b.RunParallel(func(pb *testing.PB) {
   518  			var i uint32 = 1
   519  			for pb.Next() {
   520  				i ^= i << 13
   521  				i ^= i >> 17
   522  				i ^= i << 5
   523  				if i%100 != 0 {
   524  					c[int(i)%countPerCacheLine].Add(1)
   525  				} else {
   526  					c[int(i)%countPerCacheLine].Load()
   527  				}
   528  			}
   529  		})
   530  	})
   531  
   532  	b.Run("impl=counter/op=value:add=1:99/value=uint64/par=multi", func(b *testing.B) {
   533  		c := make([]sync.Counter[uint64], countPerCacheLine)
   534  		b.ResetTimer()
   535  		b.RunParallel(func(pb *testing.PB) {
   536  			var i uint32 = 1
   537  			for pb.Next() {
   538  				i ^= i << 13
   539  				i ^= i >> 17
   540  				i ^= i << 5
   541  				if i%100 != 0 {
   542  					c[int(i)%countPerCacheLine].Add(1)
   543  				} else {
   544  					c[int(i)%countPerCacheLine].Value()
   545  				}
   546  			}
   547  		})
   548  	})
   549  
   550  	b.Run("impl=atomic/op=value:add=1:99/value=float64/par=multi", func(b *testing.B) {
   551  		c := make([]atomicFloat64, countPerCacheLine)
   552  		b.ResetTimer()
   553  		b.RunParallel(func(pb *testing.PB) {
   554  			var i uint32 = 1
   555  			for pb.Next() {
   556  				i ^= i << 13
   557  				i ^= i >> 17
   558  				i ^= i << 5
   559  				if i%100 != 0 {
   560  					c[int(i)%countPerCacheLine].Add(1)
   561  				} else {
   562  					c[int(i)%countPerCacheLine].Value()
   563  				}
   564  			}
   565  		})
   566  	})
   567  
   568  	b.Run("impl=counter/op=value:add=1:99/value=float64/par=multi", func(b *testing.B) {
   569  		c := make([]sync.Counter[float64], countPerCacheLine)
   570  		b.ResetTimer()
   571  		b.RunParallel(func(pb *testing.PB) {
   572  			var i uint32 = 1
   573  			for pb.Next() {
   574  				i ^= i << 13
   575  				i ^= i >> 17
   576  				i ^= i << 5
   577  				if i%100 != 0 {
   578  					c[int(i)%countPerCacheLine].Add(1)
   579  				} else {
   580  					c[int(i)%countPerCacheLine].Value()
   581  				}
   582  			}
   583  		})
   584  	})
   585  
   586  	b.Run("impl=atomic/op=value:add=10:90/value=int64/par=multi", func(b *testing.B) {
   587  		c := make([]atomic.Int64, countPerCacheLine)
   588  		b.ResetTimer()
   589  		b.RunParallel(func(pb *testing.PB) {
   590  			var i uint32 = 1
   591  			for pb.Next() {
   592  				i ^= i << 13
   593  				i ^= i >> 17
   594  				i ^= i << 5
   595  				if i%100 < 90 {
   596  					c[int(i)%countPerCacheLine].Add(1)
   597  				} else {
   598  					c[int(i)%countPerCacheLine].Load()
   599  				}
   600  			}
   601  		})
   602  	})
   603  
   604  	b.Run("impl=counter/op=value:add=10:90/value=int64/par=multi", func(b *testing.B) {
   605  		c := make([]sync.Counter[int64], countPerCacheLine)
   606  		b.ResetTimer()
   607  		b.RunParallel(func(pb *testing.PB) {
   608  			var i uint32 = 1
   609  			for pb.Next() {
   610  				i ^= i << 13
   611  				i ^= i >> 17
   612  				i ^= i << 5
   613  				if i%100 < 90 {
   614  					c[int(i)%countPerCacheLine].Add(1)
   615  				} else {
   616  					c[int(i)%countPerCacheLine].Value()
   617  				}
   618  			}
   619  		})
   620  	})
   621  
   622  	b.Run("impl=atomic/op=value:add=10:90/value=uint64/par=multi", func(b *testing.B) {
   623  		c := make([]atomic.Uint64, countPerCacheLine)
   624  		b.ResetTimer()
   625  		b.RunParallel(func(pb *testing.PB) {
   626  			var i uint32 = 1
   627  			for pb.Next() {
   628  				i ^= i << 13
   629  				i ^= i >> 17
   630  				i ^= i << 5
   631  				if i%100 < 90 {
   632  					c[int(i)%countPerCacheLine].Add(1)
   633  				} else {
   634  					c[int(i)%countPerCacheLine].Load()
   635  				}
   636  			}
   637  		})
   638  	})
   639  
   640  	b.Run("impl=counter/op=value:add=10:90/value=uint64/par=multi", func(b *testing.B) {
   641  		c := make([]sync.Counter[uint64], countPerCacheLine)
   642  		b.ResetTimer()
   643  		b.RunParallel(func(pb *testing.PB) {
   644  			var i uint32 = 1
   645  			for pb.Next() {
   646  				i ^= i << 13
   647  				i ^= i >> 17
   648  				i ^= i << 5
   649  				if i%100 < 90 {
   650  					c[int(i)%countPerCacheLine].Add(1)
   651  				} else {
   652  					c[int(i)%countPerCacheLine].Value()
   653  				}
   654  			}
   655  		})
   656  	})
   657  
   658  	b.Run("impl=atomic/op=value:add=10:90/value=float64/par=multi", func(b *testing.B) {
   659  		c := make([]atomicFloat64, countPerCacheLine)
   660  		b.ResetTimer()
   661  		b.RunParallel(func(pb *testing.PB) {
   662  			var i uint32 = 1
   663  			for pb.Next() {
   664  				i ^= i << 13
   665  				i ^= i >> 17
   666  				i ^= i << 5
   667  				if i%100 < 90 {
   668  					c[int(i)%countPerCacheLine].Add(1)
   669  				} else {
   670  					c[int(i)%countPerCacheLine].Value()
   671  				}
   672  			}
   673  		})
   674  	})
   675  
   676  	b.Run("impl=counter/op=value:add=10:90/value=float64/par=multi", func(b *testing.B) {
   677  		c := make([]sync.Counter[float64], countPerCacheLine)
   678  		b.ResetTimer()
   679  		b.RunParallel(func(pb *testing.PB) {
   680  			var i uint32 = 1
   681  			for pb.Next() {
   682  				i ^= i << 13
   683  				i ^= i >> 17
   684  				i ^= i << 5
   685  				if i%100 < 90 {
   686  					c[int(i)%countPerCacheLine].Add(1)
   687  				} else {
   688  					c[int(i)%countPerCacheLine].Value()
   689  				}
   690  			}
   691  		})
   692  	})
   693  }
   694  
   695  type atomicFloat64 struct {
   696  	val atomic.Uint64
   697  }
   698  
   699  func (af *atomicFloat64) Add(delta float64) {
   700  	for {
   701  		old := af.val.Load()
   702  		newVal := math.Float64bits(math.Float64frombits(old) + delta)
   703  		if af.val.CompareAndSwap(old, newVal) {
   704  			break
   705  		}
   706  	}
   707  }
   708  
   709  func (af *atomicFloat64) Value() float64 {
   710  	return math.Float64frombits(af.val.Load())
   711  }
   712  

View as plain text