github.com/puzpuzpuz/xsync/v3@v3.1.1-0.20240225193106-cbe4ec1e954f/util.go (about) 1 package xsync 2 3 import ( 4 "runtime" 5 _ "unsafe" 6 ) 7 8 // test-only assert()-like flag 9 var assertionsEnabled = false 10 11 const ( 12 // cacheLineSize is used in paddings to prevent false sharing; 13 // 64B are used instead of 128B as a compromise between 14 // memory footprint and performance; 128B usage may give ~30% 15 // improvement on NUMA machines. 16 cacheLineSize = 64 17 ) 18 19 // nextPowOf2 computes the next highest power of 2 of 32-bit v. 20 // Source: https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 21 func nextPowOf2(v uint32) uint32 { 22 if v == 0 { 23 return 1 24 } 25 v-- 26 v |= v >> 1 27 v |= v >> 2 28 v |= v >> 4 29 v |= v >> 8 30 v |= v >> 16 31 v++ 32 return v 33 } 34 35 func parallelism() uint32 { 36 maxProcs := uint32(runtime.GOMAXPROCS(0)) 37 numCores := uint32(runtime.NumCPU()) 38 if maxProcs < numCores { 39 return maxProcs 40 } 41 return numCores 42 } 43 44 //go:noescape 45 //go:linkname runtime_fastrand runtime.fastrand 46 func runtime_fastrand() uint32