Debian Codegraph @

debian/rust-vendor/bumpalo/README.md

raw
1 # `bumpalo`
2
3 **A fast bump allocation arena for Rust.**
4
5 [![](https://docs.rs/bumpalo/badge.svg)](https://docs.rs/bumpalo/)
6 [![](https://img.shields.io/crates/v/bumpalo.svg)](https://crates.io/crates/bumpalo)
7 [![](https://img.shields.io/crates/d/bumpalo.svg)](https://crates.io/crates/bumpalo)
8 [![Build Status](https://github.com/fitzgen/bumpalo/workflows/Rust/badge.svg)](https://github.com/fitzgen/bumpalo/actions?query=workflow%3ARust)
9
10 ![](https://github.com/fitzgen/bumpalo/raw/main/bumpalo.png)
11
12 ### Bump Allocation
13
14 Bump allocation is a fast, but limited approach to allocation. We have a chunk
15 of memory, and we maintain a pointer within that memory. Whenever we allocate an
16 object, we do a quick check that we have enough capacity left in our chunk to
17 allocate the object and then update the pointer by the object's size. *That's
18 it!*
19
20 The disadvantage of bump allocation is that there is no general way to
21 deallocate individual objects or reclaim the memory region for a
22 no-longer-in-use object.
23
24 These trade offs make bump allocation well-suited for *phase-oriented*
25 allocations. That is, a group of objects that will all be allocated during the
26 same program phase, used, and then can all be deallocated together as a group.
27
28 ### Deallocation en Masse, but no `Drop`
29
30 To deallocate all the objects in the arena at once, we can simply reset the bump
31 pointer back to the start of the arena's memory chunk. This makes mass
32 deallocation *extremely* fast, but allocated objects' [`Drop`] implementations are
33 not invoked.
34
35 > **However:** [`bumpalo::boxed::Box<T>`][box] can be used to wrap
36 > `T` values allocated in the `Bump` arena, and calls `T`'s `Drop`
37 > implementation when the `Box<T>` wrapper goes out of scope. This is similar to
38 > how [`std::boxed::Box`] works, except without deallocating its backing memory.
39
40 [`Drop`]: https://doc.rust-lang.org/std/ops/trait.Drop.html
41 [box]: https://docs.rs/bumpalo/latest/bumpalo/boxed/struct.Box.html
42 [`std::boxed::Box`]: https://doc.rust-lang.org/std/boxed/struct.Box.html
43
44 ### What happens when the memory chunk is full?
45
46 This implementation will allocate a new memory chunk from the global allocator
47 and then start bump allocating into this new memory chunk.
48
49 ### Example
50
51 ```rust
52 use bumpalo::Bump;
53
54 struct Doggo {
55 cuteness: u64,
56 age: u8,
57 scritches_required: bool,
58 }
59
60 // Create a new arena to bump allocate into.
61 let bump = Bump::new();
62
63 // Allocate values into the arena.
64 let scooter = bump.alloc(Doggo {
65 cuteness: u64::MAX,
66 age: 8,
67 scritches_required: true,
68 });
69
70 // Exclusive, mutable references to the just-allocated value are returned.
71 assert!(scooter.scritches_required);
72 scooter.age += 1;
73 ```
74
75 ### Collections
76
77 When the `"collections"` cargo feature is enabled, a fork of some of the `std`
78 library's collections are available in the [`collections`] module. These
79 collection types are modified to allocate their space inside `bumpalo::Bump`
80 arenas.
81
82 [`collections`]: https://docs.rs/bumpalo/latest/bumpalo/collections/index.html
83
84 ```rust
85 #[cfg(feature = "collections")]
86 {
87 use bumpalo::{Bump, collections::Vec};
88
89 // Create a new bump arena.
90 let bump = Bump::new();
91
92 // Create a vector of integers whose storage is backed by the bump arena. The
93 // vector cannot outlive its backing arena, and this property is enforced with
94 // Rust's lifetime rules.
95 let mut v = Vec::new_in(&bump);
96
97 // Push a bunch of integers onto `v`!
98 for i in 0..100 {
99 v.push(i);
100 }
101 }
102 ```
103
104 Eventually [all `std` collection types will be parameterized by an
105 allocator](https://github.com/rust-lang/rust/issues/42774) and we can remove
106 this `collections` module and use the `std` versions.
107
108 For unstable, nightly-only support for custom allocators in `std`, see the
109 `allocator_api` section below.
110
111 ### `bumpalo::boxed::Box`
112
113 When the `"boxed"` cargo feature is enabled, a fork of `std::boxed::Box`
114 is available in the `boxed` module. This `Box` type is modified to allocate its
115 space inside `bumpalo::Bump` arenas.
116
117 **A `Box<T>` runs `T`'s drop implementation when the `Box<T>` is dropped.** You
118 can use this to work around the fact that `Bump` does not drop values allocated
119 in its space itself.
120
121 ```rust
122 #[cfg(feature = "boxed")]
123 {
124 use bumpalo::{Bump, boxed::Box};
125 use std::sync::atomic::{AtomicUsize, Ordering};
126
127 static NUM_DROPPED: AtomicUsize = AtomicUsize::new(0);
128
129 struct CountDrops;
130
131 impl Drop for CountDrops {
132 fn drop(&mut self) {
133 NUM_DROPPED.fetch_add(1, Ordering::SeqCst);
134 }
135 }
136
137 // Create a new bump arena.
138 let bump = Bump::new();
139
140 // Create a `CountDrops` inside the bump arena.
141 let mut c = Box::new_in(CountDrops, &bump);
142
143 // No `CountDrops` have been dropped yet.
144 assert_eq!(NUM_DROPPED.load(Ordering::SeqCst), 0);
145
146 // Drop our `Box<CountDrops>`.
147 drop(c);
148
149 // Its `Drop` implementation was run, and so `NUM_DROPS` has been
150 // incremented.
151 assert_eq!(NUM_DROPPED.load(Ordering::SeqCst), 1);
152 }
153 ```
154
155 #### Serde
156
157 Adding the `serde` feature flag will enable transparent serialization of `Vec`s, `String`s
158 and boxed values.
159
160 ```toml
161 [dependencies]
162 bumpalo = { version = "3.18", features = ["collections", "boxed", "serde"] }
163 ```
164
165 ```rust,ignore
166 use bumpalo::{Bump, boxed::Box, collections::Vec};
167
168 // Create a new bump arena.
169 let bump = Bump::new();
170
171 // Create a `Box`
172 let box = Box::new_in("hello", &bump);
173
174 // Serialize with serde_json
175 assert_eq!(serde_json::to_string(&box).unwrap(), "\"hello\"");
176
177 // Create a `Vec`
178 let vec = Vec::new_in( &bump);
179 vec.push(1);
180 vec.push(2);
181
182 // Serialize with serde_json
183 assert_eq!(serde_json::to_string(&vec).unwrap(), "[1, 2]");
184 ```
185
186 ### `#![no_std]` Support
187
188 Bumpalo is a `no_std` crate by default. It depends only on the `alloc` and `core` crates.
189
190 ### `std` Support
191
192 You can optionally decide to enable the `std` feature in order to enable some
193 std only trait implementations for some collections:
194
195 * `std::io::Write` for `Vec<'bump, u8>`
196
197 ### Thread support
198
199 The `Bump` is `!Sync`, which makes it hard to use in certain situations around
200 threads ‒ for example in `rayon`.
201
202 The [`bumpalo-herd`](https://crates.io/crates/bumpalo-herd) crate provides a
203 pool of `Bump` allocators for use in such situations.
204
205 ### Nightly Rust `allocator_api` Support
206
207 The unstable, nightly-only Rust `allocator_api` feature defines an [`Allocator`]
208 trait and exposes custom allocators for `std` types. Bumpalo has a matching
209 `allocator_api` cargo feature to enable implementing `Allocator` and using
210 `Bump` with `std` collections. Note that, as `feature(allocator_api)` is
211 unstable and only in nightly Rust, Bumpalo's matching `allocator_api` cargo
212 feature should be considered unstable, and will not follow the semver
213 conventions that the rest of the crate does.
214
215 First, enable the `allocator_api` feature in your `Cargo.toml`:
216
217 ```toml
218 [dependencies]
219 bumpalo = { version = "3", features = ["allocator_api"] }
220 ```
221
222 Next, enable the `allocator_api` nightly Rust feature in your `src/lib.rs` or
223 `src/main.rs`:
224
225 ```rust,ignore
226 #![feature(allocator_api)]
227 ```
228
229 Finally, use `std` collections with `Bump`, so that their internal heap
230 allocations are made within the given bump arena:
231
232 ```rust,ignore
233 use bumpalo::Bump;
234
235 // Create a new bump arena.
236 let bump = Bump::new();
237
238 // Create a `Vec` whose elements are allocated within the bump arena.
239 let mut v = Vec::new_in(&bump);
240 v.push(0);
241 v.push(1);
242 v.push(2);
243 ```
244
245 [`Allocator`]: https://doc.rust-lang.org/std/alloc/trait.Allocator.html
246
247 ### Using the `Allocator` API on Stable Rust
248
249 You can enable the `allocator-api2` Cargo feature and `bumpalo` will use [the
250 `allocator-api2` crate](https://crates.io/crates/allocator-api2) to implement
251 the unstable nightly`Allocator` API on stable Rust. This means that
252 `bumpalo::Bump` will be usable with any collection that is generic over
253 `allocator_api2::Allocator`.
254
255 ### Minimum Supported Rust Version (MSRV)
256
257 This crate is guaranteed to compile on stable Rust **1.71.1** and up. It might
258 compile with older versions but that may change in any new patch release.
259
260 We reserve the right to increment the MSRV on minor releases, however we will
261 strive to only do it deliberately and for good reasons.