E471 Vec allocation `#[track_caller]` should be removed · Issue #146963 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

Vec allocation #[track_caller] should be removed #146963

@Noratrieb

Description

@Noratrieb

#126557 added #[track_caller] to lots of allocating methods on Vec.

The description of all 3 PRs are very light on details, and what they don't reveal (and their added tests can't reveal since there are none):

  1. This doesn't work by default, as OOM currently isn't surfaced as a panic. The OOM messages have no location whatsoever.
  2. Even with -Zoom=panic, it doesn't work today, locations always point at the alloc crate.

I am a bit puzzled about 1). Was this never tested when the PR was opened or am I missing something? For 2) I didn't verify whether it worked originally, but even if it did, it then broke later. No one bothered to report its breakage, which I take as evidence that this feature is not very useful (probably due to 1)).

I don't think this feature is that useful because while it can be neat to see the location immediately, it's not that useful since backtraces do the same thing with only slightly more effort. If it was free I wouldn't mind, but it is not:

The feature had a 1-2% binary size impact, which is pretty significant (which was pointed out by @nnethercote on the PR). It meant that every inclined .push() had to get a location before calling the grow function, which is several instructions.

Test code:

Vec::<u32>::with_capacity(100000000000000);
struct CookedAlloc;

unsafe impl std::alloc::GlobalAlloc for CookedAlloc {
  unsafe fn alloc(&self, layout: std::alloc::Layout) -> *mut u8 {
    if layout.size() > 1_0000 {
      std::ptr::null_mut()
    } else {
      std::alloc::System.alloc(layout)
    }
  }

  unsafe fn dealloc(&self,ptr: *mut u8,layout: std::alloc::Layout) {}
}

#[global_allocator]
static GALLOC: CookedAlloc = CookedAlloc;

fn main() {
  let mut v = Vec::new();

  for i in 0.. {
    v.push([0; 1_000_000]);
  }
}

Example output (default):

memory allocation of 400000000000000 bytes failed

Example output (from push, -Zoom=panic):

thread 'main' panicked at library/std/src/alloc.rs:356:9:
memory allocation of 4000000 bytes failed
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-heavyIssue: Problems and improvements with respect to binary size 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

      0