Skip to content

'unsafe impl<A> Allocator for &A where A: Allocator' is problematic #98232

Closed

Description

@RalfJung

We have a general Allocator instance for &A where A: Allocator. I think that is a problem. In #98178 I fixed a problem where BTreeMap<T, Global> would actually use &Global as allocator, which leads to performance regression. Or at least, I thought I had fixed that problem -- I just realized that there are some places where it still adds an indirection and therefore adds an unnecessary reference that will be copied around:

self.range.deallocating_end(&self.alloc);

I found this by removing the unsafe impl<A> Allocator for &A where A: Allocator + ?Sized instance. I don't know of another way to avoid accidentally introducing such performance regressions.

So maybe that instance should be removed?
Cc @rust-lang/wg-allocators

Activity

  1. changed the title [-]'unsafe impl<A> Allocator for &A where A: Allocator + ?Sized' is problematic[/-] [+]'unsafe impl<A> Allocator for &A where A: Allocator' is problematic[/+] on Jun 18, 2022
  2. CAD97 commented on Jun 18, 2022

    @CAD97
    Contributor

    See also #94069, #94114 (comment); there're other reasons that this blanket implementation is problematic.

    I'd personally be fine removing the impl for the short-to-medium term at least. Long-term/stable needs a bit more consideration at least, though.

  3. added 2 commits that reference this issue on Jun 19, 2022
  4. Amanieu commented on Jun 24, 2022

    @Amanieu
    Member

    @CAD97 I don't think it's problematic if we fix the wording on the Allocator trait as I suggested in #94114 (comment).

  5. CAD97 commented on Jun 24, 2022

    @CAD97
    Contributor

    The impl can be made sound via doc clarifications (which should certainly happen even without the impl provided), yes; I was just noting the issue as a related incidental complexity of the impl.

  6. added
    I-slowIssue: Problems and improvements with respect to performance of generated code.
    A-allocatorsArea: Custom and system allocators
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    on Apr 15, 2023
  7. added
    C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing such
    on Feb 14, 2025
  8. clarfonthey commented on Oct 4, 2026

    @clarfonthey
    Contributor

    I think we're going to have to close this since we decided to stabilize this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-allocatorsArea: Custom and system allocatorsC-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-slowIssue: Problems and improvements with respect to performance of generated code.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions