Skip to content

Size-based representation for std::vector #6421

Description

Instructions

libc++ has a new optional ABI where std::vector stores len and cap instead of pointers which yields significantly better results when hardening is enabled, and "positive or neutral impact on run-time" without hardening

Adding a size-based vector to libc++’s unstable ABI

tl;dr We can significantly improve the runtime performance of std::vector by changing its representation from three pointers to one pointer and two integers. This document explains the details of this change, along with the justifications for making it.

There was #2574 but the benchmarks there were flawed and not cover all cases like different element sizes. In real code there are a lot of cases where std::vector<some_struct> is used and the struct size is unlikely to be a power of 2. In this case size() would need a multiplication, usually by a small number that fit into the immediate field of the instructions, or can be split into LEA or SHIFT/ADD series. In the traditional pointer-based it's a division by some number that will be converted into multiplication by a large "random" number that may need multiple instructions to construct

It would be nice if this alternative layout can be added

Activity

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

    performanceMust go fastervNextBreaks binary compatibility

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions