Skip to content

Optimise std::vec's .sort_by for short vectors (remove/reduce allocations) #12011

Closed

Description

For a vector of length n, it currently allocates 2 * n space, but for small vectors it only uses n... and should be able get away without allocations completely.

Note: the comparison function could unwind, so this will need to be careful about what it puts on the stack etc. to be failure safe... although there is a test that should help ensure that. [].swap is probably useful.

Random notes:

  • servo saw speed-ups from this: style: Quicksort rules to avoid allocation of temporary vectors. servo/servo#1567 (apparently for vectors of length at most 16)
  • there may be some advantage to switching what implementation is used based on the size of the elements, e.g. for &mut [uint], [].swap is cheap, but for &mut [HundredKBStruct], many swaps may be more expensive than what one can do with an allocation. (Pure speculation on my part.) Of course, we could just avoid doing this, and people could go faster by boxing it and storing ~HundredKBStruct.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    I-slowIssue: Problems and improvements with respect to performance of generated code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions