<div dir="ltr">We've pretty happy now with a patch that adds two wrappers around SmallVector that make it 1) more convenient to use and 2) will tend to mitigate misuse of SmallVector. We think it's ready for wider discussion: <a href="https://reviews.llvm.org/D90884">https://reviews.llvm.org/D90884</a><div><br></div><div>SVec<T> is a convenience alias for SmallVector<T, N> with N chosen automatically to keep its size under 64 Bytes (that heuristic is easy to change though). The recommendation in the patch is to use this "on the stack, where a "small" number of elements are expected".<br><br>Vec<T> is a convenience alias for SmallVector<T, 0>. It lets us get the (little-known?) benefits of SmallVector even when it has no inline elements (see <a href="https://llvm.org/docs/ProgrammersManual.html#llvm-adt-smallvector-h">https://llvm.org/docs/ProgrammersManual.html#llvm-adt-smallvector-h</a>). The recommendation in the patch is to use this when the SmallVector is on the heap.</div><div><br></div><div>A lot of this is boiled out from the discussion in <a href="https://groups.google.com/g/llvm-dev/c/q1OyHZy8KVc/m/1l_AasOLBAAJ?pli=1">https://groups.google.com/g/llvm-dev/c/q1OyHZy8KVc/m/1l_AasOLBAAJ?pli=1</a></div><div><br></div><div>The goals here are twofold:</div><div><br></div><div>1. convenience: not having to read/write "N", or do an extra edit/recompile cycle if you forgot it</div><div><br></div><div>2. avoiding pathological cases: The choice of N is usually semi-arbitrary in our experience, and if one isn't careful, can result in sizeof(SmallVector) becoming huge, especially in the case of nested SmallVectors. This patch avoids pathological cases in two ways:</div><div>  A. SVec<T>'s heuristic keeps sizeof(SVec<T>) bounded, which prevents pathological size amplifications like in `SmallVector<struct {SmallVector<T, 4> a, b; }, 4>`, where the small sizes effectively multiply together. Of course, users can always write SmallVector<T, N> explicitly to bypass this, but the need for that seems rare.</div><div>  B. SmallVector<T, 0> feels "weird to write" for most folks, even though it is frequently the right choice. Vec<T> mitigates that by "looking natural".</div><div><br></div><div><div>I'm surfacing this as an RFC to get feedback on a couple higher-level points:<br></div><div>- does everybody agree that SVec<T> and Vec<T> are useful to have?</div><div>- get wider consensus around suggesting these as "defaults" (see my updates to ProgrammersManual.rst in the patch)</div><div>- how much we want to bulk migrate code vs let it organically grow. Replacing SmallVector<T, 0> with Vec<T> should be completely mechanical. Replacing SmallVector<T, N> for general N would be a lot more work.<br></div></div><div>- of course: naming. SVector/Vector were floated in the patch as well and seem ok. SmallVec was rejected as it was a prefix of SmallVector (messes up autocomplete).</div><div><br></div><div>Looking forward to a world with fewer guessed SmallVector sizes,</div><div><br></div><div>-- Sean Silva</div></div>