Skip to content

Clarify Vec's documentation about buffer reuse #84488

@comex

Description

@comex
Contributor

The documentation for Vec states:

Do not rely on removed data to be erased for security purposes. Even if you drop a Vec, its buffer may simply be reused by another Vec.

Here, "its buffer may simply be reused" is simply meant to refer to the behavior of the underlying allocator. But I have seen people with low-level experience be misled by this into thinking that the implementation of Vec itself has its own global allocation cache. They then thought this cache was responsible for excess memory usage in their programs.

I'm not sure what the best way to clarify this would be. Part of the reason the current wording can be misleading is that "reused by another Vec" suggests a mechanism specific to Vec. On the other hand, it would also help to just explicitly mention the allocator. Here is a possible version that addresses both points, but it's more wordy than the current version:

Even if you drop a Vec, the allocator may reuse its buffer for another Vec or other allocation.

Activity

SkiFire13

SkiFire13 commented on Apr 23, 2021

@SkiFire13
Contributor

I guess this also includes in-place iteration?

the8472

the8472 commented on Apr 23, 2021

@the8472
Member

In-place iteration is a fancy case of type-conversion. Simpler examples are conversions to IntoIter, VecDeque and BinaryHeap which also keep the allocation.

jyn514

jyn514 commented on Apr 23, 2021

@jyn514
Member

Do not rely on removed data to be erased for security purposes. Even if you drop a Vec, its buffer may simply be reused by another Vec.

Maybe it would be more clear to say "may be reused by another allocation"? That doesn't imply it has to be a Vec that reuses the memory.

added
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools
on Apr 23, 2021
inquisitivecrystal

inquisitivecrystal commented on Jun 5, 2021

@inquisitivecrystal
Contributor

@rustbot label +A-collections +C-enhancement +T-libs

added
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
on Jun 5, 2021
chrismit3s

chrismit3s commented on Sep 30, 2021

@chrismit3s
Contributor

@rustbot claim

added a commit that references this issue on Oct 1, 2021
b458ecf
added a commit that references this issue on Oct 1, 2021
added a commit that references this issue on Oct 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-collectionsArea: `std::collections`A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @comex@the8472@SkiFire13@inquisitivecrystal@jyn514

    Issue actions

      Clarify Vec's documentation about buffer reuse · Issue #84488 · rust-lang/rust