Skip to content

Reword or even allow-by-default box_vec #2404

@killercup

Description

@killercup

In #2394 (comment) (a typical "only 20%-on-topic comment" of mine), I wrote

But thinking about the box_vec lint I'm not sure I agree with its reasoning:

Vec already keeps its contents in a separate area on the heap. So if you Box it, you just add another level of indirection without any benefit whatsoever.

There is a benefit I can think of: Boxing a Vec or String reduces the size from 3 words to 1. I'm sure some this may be a good idea, and, more importantly, this is most likely done on purpose. But maybe this is just a good idea in my imagination. Or maybe there is a crate that gives you a Vec-like API but stores length an capacity as part of the heap data and we should suggest using that?

And indeed—there is a crate that does just that, and it is @gankro's thin-vec. I'll allow the fact that it is not published to crates.io and has no readme to count for "this is an obscure use case".

Update: But it may be used in Gecko in the future (source).

So, I'm totally fine if nobody cares about this (I'll most likely never use this myself), but can I suggest changing the box_vec description to this? :)

Vec already keeps its contents in a separate area on the heap. So if you Box it, you add another level of indirection. This is often not the programmer's intention.

In the case you want to reduce the size of item on the stack, you should consider using a special Vec-like data structure that stores its length and capacity on the heap.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-documentationArea: Adding or improving documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions