github.com/aaabigfish/gopkg@v1.1.0/syncx/README.md (about) 1 # syncx 2 3 ## 1. syncx.Pool 4 ### Defects of sync.Pool 5 1. Get/Put func is unbalanced on P. 6 1. Usually Get and Put are not fired on the same goroutine, which will result in a difference in the fired times of Get and Put on each P. The difference part will trigger the steal operation(using CAS). 7 2. poolLocal.private has only one. 8 1. Get/Put operations are very frequent. In most cases, Get/Put needs to operate poolLocal.shared (using CAS) instead of the lock-free poolLocal.private. 9 3. Frequent GC. 10 1. sync.Pool is designed to serve objects with a short life cycle, but we just want to reuse, don’t want frequent GC. 11 12 ### Optimize of syncx.Pool 13 1. Transform poolLocal.private into an array to increase the frequency of poolLocal.private use. 14 2. Batch operation poolLocal.shared, reduce CAS calls. 15 3. Allow No GC. 16 17 ### Not Recommended 18 1. A single object is too large. 19 1. syncx.Pool permanently holds up to runtime.GOMAXPROC(0)*256 reusable objects. 20 2. For example, under a 4-core docker, a 4KB object will cause about 4MB of memory usage. 21 3. please evaluate it yourself. 22 23 ### Performance 24 | benchmark | sync ns/op | syncx ns/op | delta | 25 | :---------- | :-----------: | :-----------: | :-----------: | 26 | BenchmarkSyncPoolParallel-4(p=16) | 877 | 620 | -29.30% | 27 | BenchmarkSyncPoolParallel-4(p=1024) | 49385 | 33465 | -32.24% | 28 | BenchmarkSyncPoolParallel-4(p=4096) | 255671 | 149522 | -41.52% | 29 30 ### Example 31 ```go 32 var pool = &syncx.Pool{ 33 New: func() interface{} { 34 return &struct{} 35 }, 36 NoGC: true, 37 } 38 39 func getput() { 40 var obj = pool.Get().(*struct) 41 pool.Put(obj) 42 } 43 ``` 44 45 ## 2. syncx.RWMutex