{"id":13713900,"url":"https://github.com/GavinClarke0/lockless-generic-ring-buffer","last_synced_at":"2025-05-07T00:33:00.886Z","repository":{"id":41952274,"uuid":"440205879","full_name":"GavinClarke0/lockless-generic-ring-buffer","owner":"GavinClarke0","description":"Single producer and multi-reader lockless ring buffer in go using generics from the go 1.18.x release. It is significantly faster than channels with the added type safety of generics compared to ring buffers using interfaces.","archived":false,"fork":false,"pushed_at":"2023-09-17T16:48:30.000Z","size":38,"stargazers_count":156,"open_issues_count":1,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-08-09T10:50:29.684Z","etag":null,"topics":["go","golang","lockless","ringbuffer"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/GavinClarke0.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-12-20T14:49:46.000Z","updated_at":"2024-08-04T10:36:04.000Z","dependencies_parsed_at":"2024-06-19T00:08:15.256Z","dependency_job_id":"043c4ed3-a2aa-4979-9904-1870ef8bfcf5","html_url":"https://github.com/GavinClarke0/lockless-generic-ring-buffer","commit_stats":null,"previous_names":["gavinclarke0/locklessgenericringbuffer"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GavinClarke0%2Flockless-generic-ring-buffer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GavinClarke0%2Flockless-generic-ring-buffer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GavinClarke0%2Flockless-generic-ring-buffer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GavinClarke0%2Flockless-generic-ring-buffer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GavinClarke0","download_url":"https://codeload.github.com/GavinClarke0/lockless-generic-ring-buffer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224547004,"owners_count":17329413,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","golang","lockless","ringbuffer"],"created_at":"2024-08-02T23:01:47.258Z","updated_at":"2024-11-14T00:31:46.110Z","avatar_url":"https://github.com/GavinClarke0.png","language":"Go","funding_links":[],"categories":["Repositories","Go"],"sub_categories":[],"readme":"# LocklessGenericRingBuffer\n\nLocklessGenericRingBuffer is a single producer, multi reader lockless ring buffer utilizing the new generics available in \n`go 1.18+`. Instead of passing typeless `interface{}` or byte arrays we are able to pass serialized structs between go routines in a type safe manner. \n\n### What is a lockless ringbuffer ?\n\nA ring buffer, also known as a circular buffer, is a fixed-size buffer that can be efficiently appended to and read from. This implementation allows for multiple goroutines to concurrently read and a single goroutine to write to the buffer without the need for locks, ensuring maximum throughput and minimal latency. \n\n\n## Benefits\n\n- [x] Zero Heap Allocations\n- [x] Cache Friendly as underlying structures are continuous in memory\n- [x] Faster then channels for highly contended workloads (See Benchmarks)\n- [x] Zero Dependencies \n\n## Requirements\n- `golang 1.18.x or above`\n\n## Getting started\n\n**Note: writers and consumers are NOT thread safe, i.e. only use a consumer in a single go routine** \n\n### Install \n\n```\ngo get github.com/GavinClarke0/lockless-generic-ring-buffer\n```\n\n### Create and Consume \n```go\nvar buffer, _ = CreateBuffer[int](16) // buffer size must be to the power 2\n\nmessages := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}\nconsumer, _ := buffer.CreateConsumer()\n\nfor _, value := range messages {\n\tbuffer.Write(value)\n}\n\nfor _, _ = range messages {\n\t_ = consumer.Get()\n}\n```\n\n### Remove a Consumer\n```go\nvar consumer, _ = buffer.CreateConsumer()\nconsumer.Remove()\n```\n\n## Benchmarks \n\n### Comparison against channels \n\nBenchmarks are ran on a **M1 Macbook Air (16gb ram)**.\n\n**Note: each benchmark does not include creation time of the consumers/channels.**\n\n```sql\nBenchmarkConsumerSequentialReadWriteLarge-8           20          55602675 ns/op               0 B/op          0 allocs/op\nBenchmarkChannelsSequentialReadWriteLarge-8            8         133155344 ns/op               0 B/op          0 allocs/op\nBenchmarkConsumerSequentialReadWriteMedium-8        1063           1123298 ns/op               0 B/op          0 allocs/op\nBenchmarkChannelsSequentialReadWriteMedium-8         451           2650842 ns/op               0 B/op          0 allocs/op\nBenchmarkConsumerSequentialReadWriteSmall-8        99393             12099 ns/op               0 B/op          0 allocs/op\nBenchmarkChannelsSequentialReadWriteSmall-8        41755             28758 ns/op               0 B/op          0 allocs/op\nBenchmarkConsumerConcurrentReadWriteLarge-8            5         223985800 ns/op             345 B/op          2 allocs/op\nBenchmarkChannelsConcurrentReadWriteLarge-8            2         858931292 ns/op             144 B/op          2 allocs/op\nBenchmarkConsumerConcurrentReadWriteMedium-8         278           4554057 ns/op             217 B/op          2 allocs/op\nBenchmarkChannelsConcurrentReadWriteMedium-8          90          17578294 ns/op             169 B/op          2 allocs/op\nBenchmarkConsumerConcurrentReadWriteSmall-8        36378             33837 ns/op              96 B/op          2 allocs/op\nBenchmarkChannelsConcurrentReadWriteSmall-8        25004             47466 ns/op              97 B/op          2 allocs/op\n\n```\n\nIn sequential benchmarks it is about `2x` the read write speed of channels. \n\nIn concurrent benchmarks, where operations can block, it is about `2x` faster than the channel implementation. \n\n## Testing \n\nTo run current tests: `go test`\n\nTo detect race conditions run with `go test -race`, which as of the latest commit (January 24, 2021) with the current test cases it \npasses. \n\n**Note** this does not mean it is race condition free. \nAdditional tests, especially on creating and removing consumers in concurrent environments are needed. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGavinClarke0%2Flockless-generic-ring-buffer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FGavinClarke0%2Flockless-generic-ring-buffer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGavinClarke0%2Flockless-generic-ring-buffer/lists"}