{"id":13595280,"url":"https://github.com/fitzgen/bumpalo","last_synced_at":"2025-05-16T00:00:24.792Z","repository":{"id":38532367,"uuid":"158310651","full_name":"fitzgen/bumpalo","owner":"fitzgen","description":"A fast bump allocation arena for Rust","archived":false,"fork":false,"pushed_at":"2025-04-11T23:06:52.000Z","size":806,"stargazers_count":1703,"open_issues_count":32,"forks_count":121,"subscribers_count":18,"default_branch":"main","last_synced_at":"2025-05-08T22:14:48.605Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://docs.rs/bumpalo","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/fitzgen.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE-APACHE","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,"zenodo":null}},"created_at":"2018-11-20T01:04:24.000Z","updated_at":"2025-05-08T11:04:34.000Z","dependencies_parsed_at":"2023-12-11T20:28:13.877Z","dependency_job_id":"11d5bcbe-ca84-44da-9fde-532d02a5991c","html_url":"https://github.com/fitzgen/bumpalo","commit_stats":{"total_commits":318,"total_committers":54,"mean_commits":5.888888888888889,"dds":0.5471698113207547,"last_synced_commit":"8392c63d956962011a127bfd2f98c64aa36ff24a"},"previous_names":[],"tags_count":47,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fitzgen%2Fbumpalo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fitzgen%2Fbumpalo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fitzgen%2Fbumpalo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fitzgen%2Fbumpalo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fitzgen","download_url":"https://codeload.github.com/fitzgen/bumpalo/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253154977,"owners_count":21862623,"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":[],"created_at":"2024-08-01T16:01:46.972Z","updated_at":"2025-05-08T22:14:51.856Z","avatar_url":"https://github.com/fitzgen.png","language":"Rust","funding_links":[],"categories":["Rust","Memory Allocator"],"sub_categories":[],"readme":"# `bumpalo`\n\n**A fast bump allocation arena for Rust.**\n\n[![](https://docs.rs/bumpalo/badge.svg)](https://docs.rs/bumpalo/)\n[![](https://img.shields.io/crates/v/bumpalo.svg)](https://crates.io/crates/bumpalo)\n[![](https://img.shields.io/crates/d/bumpalo.svg)](https://crates.io/crates/bumpalo)\n[![Build Status](https://github.com/fitzgen/bumpalo/workflows/Rust/badge.svg)](https://github.com/fitzgen/bumpalo/actions?query=workflow%3ARust)\n\n![](https://github.com/fitzgen/bumpalo/raw/main/bumpalo.png)\n\n### Bump Allocation\n\nBump allocation is a fast, but limited approach to allocation. We have a chunk\nof memory, and we maintain a pointer within that memory. Whenever we allocate an\nobject, we do a quick check that we have enough capacity left in our chunk to\nallocate the object and then update the pointer by the object's size. *That's\nit!*\n\nThe disadvantage of bump allocation is that there is no general way to\ndeallocate individual objects or reclaim the memory region for a\nno-longer-in-use object.\n\nThese trade offs make bump allocation well-suited for *phase-oriented*\nallocations. That is, a group of objects that will all be allocated during the\nsame program phase, used, and then can all be deallocated together as a group.\n\n### Deallocation en Masse, but no `Drop`\n\nTo deallocate all the objects in the arena at once, we can simply reset the bump\npointer back to the start of the arena's memory chunk. This makes mass\ndeallocation *extremely* fast, but allocated objects' [`Drop`] implementations are\nnot invoked.\n\n\u003e **However:** [`bumpalo::boxed::Box\u003cT\u003e`][box] can be used to wrap\n\u003e `T` values allocated in the `Bump` arena, and calls `T`'s `Drop`\n\u003e implementation when the `Box\u003cT\u003e` wrapper goes out of scope. This is similar to\n\u003e how [`std::boxed::Box`] works, except without deallocating its backing memory.\n\n[`Drop`]: https://doc.rust-lang.org/std/ops/trait.Drop.html\n[box]: https://docs.rs/bumpalo/latest/bumpalo/boxed/struct.Box.html\n[`std::boxed::Box`]: https://doc.rust-lang.org/std/boxed/struct.Box.html\n\n### What happens when the memory chunk is full?\n\nThis implementation will allocate a new memory chunk from the global allocator\nand then start bump allocating into this new memory chunk.\n\n### Example\n\n```rust\nuse bumpalo::Bump;\n\nstruct Doggo {\n    cuteness: u64,\n    age: u8,\n    scritches_required: bool,\n}\n\n// Create a new arena to bump allocate into.\nlet bump = Bump::new();\n\n// Allocate values into the arena.\nlet scooter = bump.alloc(Doggo {\n    cuteness: u64::MAX,\n    age: 8,\n    scritches_required: true,\n});\n\n// Exclusive, mutable references to the just-allocated value are returned.\nassert!(scooter.scritches_required);\nscooter.age += 1;\n```\n\n### Collections\n\nWhen the `\"collections\"` cargo feature is enabled, a fork of some of the `std`\nlibrary's collections are available in the [`collections`] module. These\ncollection types are modified to allocate their space inside `bumpalo::Bump`\narenas.\n\n[`collections`]: https://docs.rs/bumpalo/latest/bumpalo/collections/index.html\n\n```rust\n#[cfg(feature = \"collections\")]\n{\n    use bumpalo::{Bump, collections::Vec};\n\n    // Create a new bump arena.\n    let bump = Bump::new();\n\n    // Create a vector of integers whose storage is backed by the bump arena. The\n    // vector cannot outlive its backing arena, and this property is enforced with\n    // Rust's lifetime rules.\n    let mut v = Vec::new_in(\u0026bump);\n\n    // Push a bunch of integers onto `v`!\n    for i in 0..100 {\n        v.push(i);\n    }\n}\n```\n\nEventually [all `std` collection types will be parameterized by an\nallocator](https://github.com/rust-lang/rust/issues/42774) and we can remove\nthis `collections` module and use the `std` versions.\n\nFor unstable, nightly-only support for custom allocators in `std`, see the\n`allocator_api` section below.\n\n### `bumpalo::boxed::Box`\n\nWhen the `\"boxed\"` cargo feature is enabled, a fork of `std::boxed::Box`\nis available in the `boxed` module. This `Box` type is modified to allocate its\nspace inside `bumpalo::Bump` arenas.\n\n**A `Box\u003cT\u003e` runs `T`'s drop implementation when the `Box\u003cT\u003e` is dropped.** You\ncan use this to work around the fact that `Bump` does not drop values allocated\nin its space itself.\n\n```rust\n#[cfg(feature = \"boxed\")]\n{\n    use bumpalo::{Bump, boxed::Box};\n    use std::sync::atomic::{AtomicUsize, Ordering};\n\n    static NUM_DROPPED: AtomicUsize = AtomicUsize::new(0);\n\n    struct CountDrops;\n\n    impl Drop for CountDrops {\n        fn drop(\u0026mut self) {\n            NUM_DROPPED.fetch_add(1, Ordering::SeqCst);\n        }\n    }\n\n    // Create a new bump arena.\n    let bump = Bump::new();\n\n    // Create a `CountDrops` inside the bump arena.\n    let mut c = Box::new_in(CountDrops, \u0026bump);\n\n    // No `CountDrops` have been dropped yet.\n    assert_eq!(NUM_DROPPED.load(Ordering::SeqCst), 0);\n\n    // Drop our `Box\u003cCountDrops\u003e`.\n    drop(c);\n\n    // Its `Drop` implementation was run, and so `NUM_DROPS` has been\n    // incremented.\n    assert_eq!(NUM_DROPPED.load(Ordering::SeqCst), 1);\n}\n```\n\n#### Serde\n\nAdding the `serde` feature flag will enable transparent serialization of `Vec`s, `String`s\nand boxed values.\n\n```toml\n[dependencies]\nbumpalo = { version = \"3.9\", features = [\"collections\", \"boxed\", \"serde\"] }\n```\n\n```rust,ignore\nuse bumpalo::{Bump, boxed::Box, collections::Vec};\n\n// Create a new bump arena.\nlet bump = Bump::new();\n\n// Create a `Box`\nlet box = Box::new_in(\"hello\", \u0026bump);\n\n// Serialize with serde_json\nassert_eq!(serde_json::to_string(\u0026box).unwrap(), \"\\\"hello\\\"\");\n\n// Create a `Vec`\nlet vec = Vec::new_in( \u0026bump);\nvec.push(1);\nvec.push(2);\n\n// Serialize with serde_json\nassert_eq!(serde_json::to_string(\u0026vec).unwrap(), \"[1, 2]\");\n```\n\n### `#![no_std]` Support\n\nBumpalo is a `no_std` crate by default. It depends only on the `alloc` and `core` crates.\n\n### `std` Support\n\nYou can optionally decide to enable the `std` feature in order to enable some\nstd only trait implementations for some collections:\n\n* `std::io::Write` for `Vec\u003c'bump, u8\u003e`\n\n### Thread support\n\nThe `Bump` is `!Sync`, which makes it hard to use in certain situations around\nthreads ‒ for example in `rayon`.\n\nThe [`bumpalo-herd`](https://crates.io/crates/bumpalo-herd) crate provides a\npool of `Bump` allocators for use in such situations.\n\n### Nightly Rust `allocator_api` Support\n\nThe unstable, nightly-only Rust `allocator_api` feature defines an [`Allocator`]\ntrait and exposes custom allocators for `std` types. Bumpalo has a matching\n`allocator_api` cargo feature to enable implementing `Allocator` and using\n`Bump` with `std` collections. Note that, as `feature(allocator_api)` is\nunstable and only in nightly Rust, Bumpalo's matching `allocator_api` cargo\nfeature should be considered unstable, and will not follow the semver\nconventions that the rest of the crate does.\n\nFirst, enable the `allocator_api` feature in your `Cargo.toml`:\n\n```toml\n[dependencies]\nbumpalo = { version = \"3\", features = [\"allocator_api\"] }\n```\n\nNext, enable the `allocator_api` nightly Rust feature in your `src/lib.rs` or\n`src/main.rs`:\n\n```rust,ignore\n#![feature(allocator_api)]\n```\n\nFinally, use `std` collections with `Bump`, so that their internal heap\nallocations are made within the given bump arena:\n\n```rust,ignore\nuse bumpalo::Bump;\n\n// Create a new bump arena.\nlet bump = Bump::new();\n\n// Create a `Vec` whose elements are allocated within the bump arena.\nlet mut v = Vec::new_in(\u0026bump);\nv.push(0);\nv.push(1);\nv.push(2);\n```\n\n[`Allocator`]: https://doc.rust-lang.org/std/alloc/trait.Allocator.html\n\n### Using the `Allocator` API on Stable Rust\n\nYou can enable the `allocator-api2` Cargo feature and `bumpalo` will use [the\n`allocator-api2` crate](https://crates.io/crates/allocator-api2) to implement\nthe unstable nightly`Allocator` API on stable Rust. This means that\n`bumpalo::Bump` will be usable with any collection that is generic over\n`allocator_api2::Allocator`.\n\n### Minimum Supported Rust Version (MSRV)\n\nThis crate is guaranteed to compile on stable Rust **1.71.1** and up. It might\ncompile with older versions but that may change in any new patch release.\n\nWe reserve the right to increment the MSRV on minor releases, however we will\nstrive to only do it deliberately and for good reasons.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffitzgen%2Fbumpalo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffitzgen%2Fbumpalo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffitzgen%2Fbumpalo/lists"}