The Vec should not needlessly overallocate capacity if it is guaranteed to fail.

ce1838f
Opened by Deleted user at 2020-03-28 18:06:39

Current Vec / RawVec allocation strategy is to at least double the capacity on reallocation. This leads to unnecessary panic when the new overallocated capacity exceeds std::isize::MAX. If allocating std::isize::MAX would be sufficient it should do so instead.

For example I would expect the following to work on 32-bit platform (provided that there is sufficient amount of memory):

fn main() {
    let mut v = std::vec::from_elem(1 as u8, (std::isize::MAX / 2 + 1) as usize);
    v.push(1);
}
  1. Would’ve been fixed-ish by https://github.com/rust-lang/rust/pull/29848?

    Simonas Kazlauskas at 2016-02-06 22:15:54

  2. I'm curious: what languages succeed or fail by this metric?

    Aria Desires at 2016-02-07 06:21:16

  3. @nagisa as far as I can see that PR would have exactly the same problem. Generally, the source of problem lies in a fact that the calculation of new capacity is unaware on requirement that it should be less than std::isize::MAX, which is checked only later in alloc_guard.

    @Gankro the GNU libstdc++ works fine in this respect. It seems more like quality of implementation issue, than some strong requirement.

    Deleted user at 2016-02-07 08:11:15

  4. Triage: I don't think anyone has addressed this issue.

    Steve Klabnik at 2020-03-28 18:06:39