rust-lang / rust-lang/rust

Allocators in `BTreeMap` and `BTreeSet` need more work

Open
#161,678 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-allocators A-collections C-discussion requires-nightly T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

In https://github.com/rust-lang/rust/pull/157428, we banned allocators from unwinding out of their drop to fix #159334 and its related issues. We also introduced AllocatorClone to fix #156920. In #161684, we replaced the Allocator + Clone bounds with AllocatorClone as a short-term solution.

However, the allocators are still cloned at every recursive call site, which was originally done to fix regressions with using references for this, since ZST allocators (like the most important allocator, Global), had measureable overhead. However, for non-Copy allocators, this is completely unreasonable. Even the destructor of BTreeMap clones around the allocator recursively, so there is no way to use BTreeMap without AllocatorClone at all currently.

We should probably come up with another solution that allows ZSTs to be passed by-value, while expensive-to-clone allocators can be passed by reference, perhaps through some kind of unsafe trait AllocatorShare { type Share<'a>: Allocator; fn share_alloc(&self) -> Self::Share<'_>; }. This would also allow something like Box<Arc<&Arc<A>>> to be passed around as A::Share, since it can defer recursively. However it has its own design problems.

Discussion on zulip

@rustbot label A-allocators requires-nightly -I-unsound A-collections -C-bug C-discussion

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue concerns allocator handling in BTreeMap and BTreeSet, with context in PRs #157428 and #161684, the related issues, and the linked Zulip discussion. Start by tracing the recursive allocator clone sites and the destructor path. Done requires a settled design and implementation that addresses expensive allocator cloning while preserving ZST behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.