<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">+richard<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 2020 Dec  1, at 14:19, Chris Lattner <<a href="mailto:clattner@nondot.org" class="">clattner@nondot.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class=""><blockquote type="cite" class=""><div class=""><div dir="auto" class=""><blockquote type="cite" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;" class=""><div class=""><blockquote type="cite" class=""><div class=""><div style="overflow-wrap: break-word;" class="">On Nov 17, 2020, at 1:42 PM, David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank" class="">dblaikie@gmail.com</a>> wrote:<div class=""><blockquote type="cite" class=""><div class=""><div class=""><blockquote type="cite" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none" class="">Thoughts/suggestions:<br class="">- Adding the default seems very reasonable to me, and I think that 64 bytes is a good default.  I think you should change the behavior so that SmallVector<LargeThing> defaults to a single inline element instead of zero though.  Perhaps generate a static_assert when it is crazy large.<br class=""></blockquote><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none" class=""><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline" class="">Out of curiosity: Why a single rather than zero?</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none" class=""></div></div></blockquote></div><br class=""><div class="">My rationale for this is basically that SmallVector is typically used for the case when you want to avoid an out-of-line allocation for a small number of elements, this was the reason it was created.  While there is some performance benefits of SmallVector<T,0> over std::vector<> they are almost trivial.</div></div></div></blockquote><div class=""><br class=""></div><div class="">The performance benefits aren't trivial.</div><div class=""><br class=""></div><div class="">std::vector grow operations will refuse to use std::move for some T, a pessimization required by its exception guarantees, even if you're building with `-fno-exceptions`. We had a massive compile-time problem in 2016 related to this that I fixed with 3c406c2da52302eb5cced431373f240b9c037841 by switching to SmallVector<T,0>. You can see the history in r338071 / 0f81faed05c3c7c1fbaf6af402411c99d715cf56.</div></div></div></blockquote><div class=""><br class=""></div><div class="">That issue, at least, is fixable without switching from std::vector just by adding noexcept to the appropriate user-defined move constructors.</div></div></div>
</div></blockquote><br class=""><div class="">Sure, once we’ve added noexcept to all types in LLVM/Clang/etc. That’s a pretty long tail though; a lot of work for relatively little gain given that we don’t care about exceptions anyway and we have an optimized vector implementation in tree. </div></div></div></blockquote></div><br class=""><div class="">Can you spell this out for me?  Why do we need noexcept if we’re building with -fno-exceptions?  What is going on here?</div></div></div></blockquote><br class=""></div><div>Sure. It's a bit convoluted. Here's my understanding:</div><div><br class=""></div><div>First, here's why std::vector has this behaviour:</div><div>- std::vector grow operations need to transfer their existing elements over to the new storage.</div><div>- The grow operations are usually required to meet the "strong exception guarantee": if something throws, this function has no effect.</div><div>- If move operations throw, you can't provide this guarantee unless you copy (you can't move back the elements that have been half-moved over, in case another exception is thrown; but if it was just a copy, the original storage still has the elements safely unmodified).</div><div>- There's a caveat / carve out, that if T cannot be copy-constructed AND T's move constructor is not noexcept, then the guarantee is waived (since there's no way to implement it).</div><div>- Implementation is to call std::move_if_noexcept (<a href="https://en.cppreference.com/w/cpp/utility/move_if_noexcept" class="">https://en.cppreference.com/w/cpp/utility/move_if_noexcept</a>), which moves if it's a noexcept operation, or if T is not copy-constructible.</div><div><br class=""></div><div>Second, here's why the behaviour doesn't change when -fno-exceptions:</div><div><div>- -fno-exceptions does NOT imply `noexcept` (maybe it should?, but it doesn't).</div></div><div>- This is implemented by detecting via SFINAE whether something is `noexcept` (maybe std::vector::resize/push_back/etc should have a special case? but that's controversial).</div><div><br class=""></div><div>IMO, until all the C++ standard libraries and host compilers that we support being built with will consistently use std::move on grow operations in std::vector in -fno-exceptions mode, we should only use std::vector when we absolutely have to. It's not designed for -fno-exceptions codebases.</div><div><br class=""></div><div>(</div><div>There's some discussion in this thread:</div><div><div><a href="http://lists.llvm.org/pipermail/libcxx-dev/2019-April/000344.html" class="">http://lists.llvm.org/pipermail/libcxx-dev/2019-April/000344.html</a></div><div><div class=""></div><div class=""><br class=""></div></div><div>And Richard had a proposal that made sense to me:</div><div><a href="http://lists.llvm.org/pipermail/libcxx-dev/2019-April/000343.html" class="">http://lists.llvm.org/pipermail/libcxx-dev/2019-April/000343.html</a></div><div><div class=""></div><blockquote type="cite" class=""><div class="">I'm wondering if we should have an experimental</div><div class="">option to specify that functions are noexcept by default (overridable</div><div class="">by an explicit exception specification)</div></blockquote></div><div><br class=""></div><div>But that only fixes it in host compilers that implemented this experimental mode.</div><div>)</div></div></body></html>