{"id":20394408,"url":"https://github.com/rkuhn/batch_queue","last_synced_at":"2025-08-01T05:35:43.079Z","repository":{"id":57503839,"uuid":"439906736","full_name":"rkuhn/batch_queue","owner":"rkuhn","description":"A single-producer single-consumer Rust queue with smart batching","archived":false,"fork":false,"pushed_at":"2021-12-19T21:19:09.000Z","size":20,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-12T12:12:36.426Z","etag":null,"topics":["concurrency","queue","rust","spsc-queue"],"latest_commit_sha":null,"homepage":"https://docs.rs/batch_queue","language":"Rust","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/rkuhn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE2.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-12-19T16:02:19.000Z","updated_at":"2023-06-15T01:41:05.000Z","dependencies_parsed_at":"2022-08-28T02:00:53.634Z","dependency_job_id":null,"html_url":"https://github.com/rkuhn/batch_queue","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rkuhn%2Fbatch_queue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rkuhn%2Fbatch_queue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rkuhn%2Fbatch_queue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rkuhn%2Fbatch_queue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rkuhn","download_url":"https://codeload.github.com/rkuhn/batch_queue/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248565079,"owners_count":21125417,"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":["concurrency","queue","rust","spsc-queue"],"created_at":"2024-11-15T03:53:00.889Z","updated_at":"2025-04-12T12:12:41.401Z","avatar_url":"https://github.com/rkuhn.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Batching Queue\n\nA library that implements smart batching between a producer and a consumer.\nIn other words, a single-producer single-consumer queue that tries to ensure that the either side runs for a while (to make use of L1 caches) before hibernating again.\n\nOne nice feature of this queue is that all buckets are preallocated, so unless you use the `recv_batch` or `try_recv_batch` methods no allocations are done as part of sending and receiving items.\n\n## Example\n\n```rust\n# tokio_test::block_on(async {\nuse batch_queue::{batch_queue, pipe};\n\nlet (tx, mut rx) = batch_queue::\u003cu32, 128\u003e(20);\nconst N: u32 = 10_000_000;\n\ntokio::spawn(async move {\n    let stream = futures::stream::iter(0..N);\n    pipe(stream, tx).await;\n});\n\nlet mut x = 0;\nwhile let Ok(iter) = rx.recv().await {\n    for y in iter {\n        assert_eq!(y, x);\n        x += 1;\n    }\n}\nassert_eq!(x, N);\n# })\n```\n\nHere, the iterator returned by `recv()` allows you to consume the bucket contents without allocating.\nOn my AMD Hetzner box it takes about 6ns per item.\n\n## How it works\n\nThe queue is modeled as a boxed slice of buckets, which each is an appropriately sized array of `MaybeUninit`-ialized items and a fill level.\nReader and writer maintain a current position each, which includes the bucket index as well as a cycle counter (to detect when the writer is exactly one full round ahead of the reader).\n\nInserting an item:\n\n- check whether the writer’s bucket is available for writing (i.e. it is not the reader’s bucket in the previous cycle)\n- if so, insert into next slot in bucket; if full, move write bucket position\n\nDeclaring end of current batch:\n\n- check whether current write bucket has elements ⇒ move write bucket position\n\nTaking a bucket:\n\n- check whether the writer’s bucket is ahead of the reader’s bucket ⇒ take it and move read bucket position\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frkuhn%2Fbatch_queue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frkuhn%2Fbatch_queue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frkuhn%2Fbatch_queue/lists"}