NewsCrawler.org

Custom allocators are now stable in Rust: what they make possible

A blog post looks at what Rust's custom allocators can do now that the feature is stable in Rust 1.100, after 10 years of waiting. Its first use case is the bump allocator[1], which reserves one large block of memory and hands out pieces of it by moving a counter forward. Individual pieces are never freed separately, so everything is released at once, which suits bursts of allocations such as a web request. The author builds a linked list of a million nodes and drops it in virtually no time, but notes that memory use may grow considerably.

The second use case is memory accounting, which tracks how much memory a part of the code uses. The author wraps an allocator in a layer that adds and subtracts the size of each allocation in an atomic counter[2]. Such numbers can drive decisions, for example when a cache should start evicting entries or a sandbox should be limited.

The author also presents thresher, a crate of their own that runs a callback when allocations reach a set threshold. It keeps a local counter in each thread and updates the global one every 64KiB, which reduces contention on the atomic counter.

Repository Rust github.com

Open source programming language focused on reliable and efficient software, with memory safety enforced at compile time.

Stars: 122,301Forks: 16,638License: Apache-2.0Updated:

Read the original cetra3.github.ioDiscussion

Sources

Published: First seen: Читать по-русски

Open as a page