github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/collection/lscq/readme.md (about) 1 # LSCQ 2 3 LSCQ is a scalable, unbounded, multiple-producer and multiple-consumer FIFO queue in Go language. 4 5 In the benchmark(AMD 3700x, running at 3.6 GHZ, -cpu=16), the LSCQ outperforms lock-based linked queue **5x ~ 6x** in most cases. Since built-in channel is a bounded queue, we can only compared it in EnqueueDequeuePair, the LSCQ outperforms built-in channel **8x ~ 9x** in this case. 6 7 The ideas behind the LSCQ are [A Scalable, Portable, and Memory-Efficient Lock-Free FIFO Queue](https://arxiv.org/abs/1908.04511) and [Fast Concurrent Queues for x86 Processors](https://www.cs.tau.ac.il/~mad/publications/ppopp2013-x86queues.pdf). 8 9 10 11 ## QuickStart 12 13 ```go 14 package main 15 16 import ( 17 "github.com/bytedance/gopkg/collection/lscq" 18 ) 19 20 func main() { 21 q := lscq.NewUint64() 22 q.Enqueue(100) 23 println(q.Dequeue()) 24 } 25 ``` 26 27 28 29 ## Benchmark 30 31 - Go version: go1.16.2 linux/amd64 32 - OS: ubuntu 18.04 33 - CPU: AMD 3700x(8C16T), running at 3.6 GHZ (disable CPU turbo boost) 34 - MEMORY: 16G x 2 DDR4 memory, running at 3200 MHZ 35 36 37 38 ### CPU=100 39 40 ```bash 41 go test -bench=. -cpu=100 -run=NOTEST -benchtime=1000000x 42 ``` 43 44 ![benchmarkcpu100](https://raw.githubusercontent.com/zhangyunhao116/public-data/master/lscq-benchmark-cpu100.png) 45 46 ``` 47 Default/EnqueueOnly/LSCQ-100 38.9ns ±14% 48 Default/EnqueueOnly/linkedQ-100 209ns ± 3% 49 Default/EnqueueOnly/msqueue-100 379ns ± 2% 50 Default/DequeueOnlyEmpty/LSCQ-100 10.0ns ±31% 51 Default/DequeueOnlyEmpty/linkedQ-100 79.2ns ± 4% 52 Default/DequeueOnlyEmpty/msqueue-100 7.59ns ±44% 53 Default/Pair/LSCQ-100 58.7ns ± 7% 54 Default/Pair/linkedQ-100 324ns ± 5% 55 Default/Pair/msqueue-100 393ns ± 2% 56 Default/50Enqueue50Dequeue/LSCQ-100 34.9ns ± 8% 57 Default/50Enqueue50Dequeue/linkedQ-100 183ns ± 7% 58 Default/50Enqueue50Dequeue/msqueue-100 191ns ± 3% 59 Default/30Enqueue70Dequeue/LSCQ-100 78.5ns ± 4% 60 Default/30Enqueue70Dequeue/linkedQ-100 148ns ± 8% 61 Default/30Enqueue70Dequeue/msqueue-100 136ns ± 4% 62 Default/70Enqueue30Dequeue/LSCQ-100 36.2ns ±13% 63 Default/70Enqueue30Dequeue/linkedQ-100 195ns ± 4% 64 Default/70Enqueue30Dequeue/msqueue-100 267ns ± 2% 65 ``` 66 67 68 69 ### CPU=16 70 71 ```bash 72 go test -bench=. -cpu=16 -run=NOTEST -benchtime=1000000x 73 ``` 74 75 ![benchmarkcpu16](https://raw.githubusercontent.com/zhangyunhao116/public-data/master/lscq-benchmark-cpu16.png) 76 77 ``` 78 Default/EnqueueOnly/LSCQ-16 33.7ns ± 5% 79 Default/EnqueueOnly/linkedQ-16 177ns ± 2% 80 Default/EnqueueOnly/msqueue-16 370ns ± 1% 81 Default/DequeueOnlyEmpty/LSCQ-16 3.27ns ±47% 82 Default/DequeueOnlyEmpty/linkedQ-16 91.1ns ± 2% 83 Default/DequeueOnlyEmpty/msqueue-16 3.23ns ±46% 84 Default/Pair/LSCQ-16 56.1ns ± 3% 85 Default/Pair/linkedQ-16 290ns ± 1% 86 Default/Pair/msqueue-16 367ns ± 1% 87 Default/50Enqueue50Dequeue/LSCQ-16 31.8ns ± 3% 88 Default/50Enqueue50Dequeue/linkedQ-16 157ns ± 8% 89 Default/50Enqueue50Dequeue/msqueue-16 188ns ± 4% 90 Default/30Enqueue70Dequeue/LSCQ-16 73.8ns ± 2% 91 Default/30Enqueue70Dequeue/linkedQ-16 149ns ± 5% 92 Default/30Enqueue70Dequeue/msqueue-16 123ns ± 2% 93 Default/70Enqueue30Dequeue/LSCQ-16 28.8ns ± 4% 94 Default/70Enqueue30Dequeue/linkedQ-16 176ns ± 3% 95 Default/70Enqueue30Dequeue/msqueue-16 261ns ± 2% 96 ``` 97 98 99 100 ### CPU=1 101 102 ```bash 103 go test -bench=. -cpu=1 -run=NOTEST -benchtime=1000000x 104 ``` 105 106 ![benchmarkcpu1](https://raw.githubusercontent.com/zhangyunhao116/public-data/master/lscq-benchmark-cpu1.png) 107 108 ``` 109 name time/op 110 Default/EnqueueOnly/LSCQ 17.3ns ± 1% 111 Default/EnqueueOnly/linkedQ 59.9ns ± 6% 112 Default/EnqueueOnly/msqueue 67.1ns ± 2% 113 Default/DequeueOnlyEmpty/LSCQ 4.77ns ± 1% 114 Default/DequeueOnlyEmpty/linkedQ 11.3ns ± 2% 115 Default/DequeueOnlyEmpty/msqueue 3.14ns ± 1% 116 Default/Pair/LSCQ 36.7ns ± 0% 117 Default/Pair/linkedQ 56.2ns ± 6% 118 Default/Pair/msqueue 60.2ns ± 2% 119 Default/50Enqueue50Dequeue/LSCQ 23.1ns ± 2% 120 Default/50Enqueue50Dequeue/linkedQ 34.1ns ± 3% 121 Default/50Enqueue50Dequeue/msqueue 40.8ns ± 9% 122 Default/30Enqueue70Dequeue/LSCQ 26.5ns ± 2% 123 Default/30Enqueue70Dequeue/linkedQ 27.0ns ±28% 124 Default/30Enqueue70Dequeue/msqueue 26.7ns ± 7% 125 Default/70Enqueue30Dequeue/LSCQ 25.2ns ± 5% 126 Default/70Enqueue30Dequeue/linkedQ 47.3ns ± 5% 127 Default/70Enqueue30Dequeue/msqueue 55.2ns ± 8% 128 ``` 129