Allocation API

Some specific projects require the ability to change the default heap allocation algorithm. Rust uses jemalloc by default for targets that allow it. As we saw in previous chapters, one of the features of this allocator is that, in collections, it will allocate the multiple of two of the last allocations.

You can change that by using the alloc, allocator_api, alloc_jemalloc, and alloc_system features. The last two specify the global allocator of the crate, which, in cases such as kernel development, must be specified and some functions implemented so that collections work. The other two allow for more customized allocator manipulation, even giving you the option to change the allocator for each collection.

Get Rust High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.