https://github.com/webbmaffian/go-prio-queue
Zero-allocation priority queue for Golang.
https://github.com/webbmaffian/go-prio-queue
go golang priority-queue zero-allocation
Last synced: 2 months ago
JSON representation
Zero-allocation priority queue for Golang.
- Host: GitHub
- URL: https://github.com/webbmaffian/go-prio-queue
- Owner: webbmaffian
- License: mit
- Created: 2022-09-15T17:11:26.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-01-05T09:58:58.000Z (about 3 years ago)
- Last Synced: 2024-06-20T10:04:29.382Z (almost 2 years ago)
- Topics: go, golang, priority-queue, zero-allocation
- Language: Go
- Homepage:
- Size: 22.5 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Go Prio Queue
A highly optimized [priority queue](https://en.wikipedia.org/wiki/Priority_queue) with generics. See [benchmark](#benchmark).
- Support for min, max and custom priority comparator.
- 100% Go.
- Zero dependencies.
- Zero allocations for push/pop operations.
- Around 3 ns for push (up to 4000 values).
- Around 2 ns for pop.
## Install
```
go get github.com/webbmaffian/go-prio-queue
```
## Usage
### Min (ascending) priority queue
```go
q := NewMinQueue[string, float64]()
q.Push("a", 56)
q.Push("b", 12)
q.Push("c", 34)
for !q.Empty() {
log.Println(q.Pop())
}
// b 12
// c 34
// a 56
```
### Max (descending) priority queue
```go
q := NewMaxQueue[string, float64]()
q.Push("a", 56)
q.Push("b", 12)
q.Push("c", 34)
for !q.Empty() {
log.Println(q.Pop())
}
// a 56
// c 34
// b 12
```
### Priority queue with custom comparator
The `compare` function should return `true` if `a` has higher priority than `b`.
```go
// Prioritize even numbers
func compare(a, b float64) bool {
if a % 2 == 0 && b % 2 == 1 {
return true
}
return a < b
}
q := NewQueue[string, float64](compare)
q.Push("a", 1)
q.Push("b", 2)
q.Push("c", 3)
q.Push("d", 4)
q.Push("e", 5)
for !q.Empty() {
log.Println(q.Pop())
}
// b 2
// d 4
// a 1
// c 3
// e 5
```
## Benchmark
```
goos: darwin
goarch: arm64
pkg: github.com/webbmaffian/go-prio-queue
BenchmarkSmallQueueNew/255-10 4459081 269.000 ns/op 2688 B/op 1 allocs/op
BenchmarkSmallQueuePush/255-10 349781355 3.427 ns/op 0 B/op 0 allocs/op
BenchmarkSmallQueuePop/255-10 586978732 2.041 ns/op 0 B/op 0 allocs/op
BenchmarkCustomQueueNew/16-10 14742232 75.690 ns/op 240 B/op 3 allocs/op
BenchmarkCustomQueueNew/32-10 15763598 75.530 ns/op 240 B/op 3 allocs/op
BenchmarkCustomQueueNew/64-10 15500386 75.620 ns/op 240 B/op 3 allocs/op
BenchmarkCustomQueueNew/128-10 16165234 75.990 ns/op 240 B/op 3 allocs/op
BenchmarkCustomQueueNew/256-10 15374652 75.800 ns/op 240 B/op 3 allocs/op
BenchmarkCustomQueueNew/512-10 16020327 76.480 ns/op 240 B/op 3 allocs/op
BenchmarkCustomQueueNew/1024-10 15808869 76.040 ns/op 240 B/op 3 allocs/op
BenchmarkCustomQueueNew/2048-10 15815224 76.110 ns/op 240 B/op 3 allocs/op
BenchmarkCustomQueueNew/4096-10 14650374 75.960 ns/op 240 B/op 3 allocs/op
BenchmarkCustomQueueNew/8192-10 16067761 76.890 ns/op 240 B/op 3 allocs/op
BenchmarkCustomQueueNew/16384-10 16040492 78.120 ns/op 240 B/op 3 allocs/op
BenchmarkCustomQueuePush/16-10 441543746 2.720 ns/op 0 B/op 0 allocs/op
BenchmarkCustomQueuePush/32-10 438644319 2.734 ns/op 0 B/op 0 allocs/op
BenchmarkCustomQueuePush/64-10 433402447 2.770 ns/op 0 B/op 0 allocs/op
BenchmarkCustomQueuePush/128-10 427948100 2.806 ns/op 0 B/op 0 allocs/op
BenchmarkCustomQueuePush/256-10 429108579 2.770 ns/op 0 B/op 0 allocs/op
BenchmarkCustomQueuePush/512-10 427958403 2.761 ns/op 0 B/op 0 allocs/op
BenchmarkCustomQueuePush/1024-10 389328944 2.797 ns/op 0 B/op 0 allocs/op
BenchmarkCustomQueuePush/2048-10 386863712 2.961 ns/op 0 B/op 0 allocs/op
BenchmarkCustomQueuePush/4096-10 298121041 3.472 ns/op 0 B/op 0 allocs/op
BenchmarkCustomQueuePush/8192-10 58608999 17.650 ns/op 0 B/op 0 allocs/op
BenchmarkCustomQueuePush/16384-10 114636 11915.000 ns/op 0 B/op 0 allocs/op
BenchmarkCustomQueuePop/16-10 573327946 2.063 ns/op 0 B/op 0 allocs/op
BenchmarkCustomQueuePop/32-10 583133943 2.043 ns/op 0 B/op 0 allocs/op
BenchmarkCustomQueuePop/64-10 581798670 2.059 ns/op 0 B/op 0 allocs/op
BenchmarkCustomQueuePop/128-10 583278972 2.049 ns/op 0 B/op 0 allocs/op
BenchmarkCustomQueuePop/256-10 581352746 2.062 ns/op 0 B/op 0 allocs/op
BenchmarkCustomQueuePop/512-10 582200670 2.056 ns/op 0 B/op 0 allocs/op
BenchmarkCustomQueuePop/1024-10 577774112 2.056 ns/op 0 B/op 0 allocs/op
BenchmarkCustomQueuePop/2048-10 578327653 2.057 ns/op 0 B/op 0 allocs/op
BenchmarkCustomQueuePop/4096-10 583535660 2.059 ns/op 0 B/op 0 allocs/op
BenchmarkCustomQueuePop/8192-10 584634346 2.062 ns/op 0 B/op 0 allocs/op
BenchmarkCustomQueuePop/16384-10 583455508 2.061 ns/op 0 B/op 0 allocs/op
```