> On April 11, 2018, 2:06 a.m., Benjamin Mahler wrote: > > 3rdparty/stout/include/stout/strings.hpp > > Lines 390-392 (patched) > > <https://reviews.apache.org/r/63367/diff/4/?file=1991745#file1991745line390> > > > > This is definitely simple but is this how C++20 std libraries implement > > this? > > > > It seems non-optimal to always have to scan the entire prefix array for > > the null character? E.g. startsWith("foo", "bar ... <1MB>"). Ideally this > > could trip to false after checking the first character only?
In `libc++` the implementation is the same: ``` template<class _CharT, class _Traits = char_traits<_CharT> > class basic_string_view { /* ... */ _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY basic_string_view(const _CharT* __s) : __data(__s), __size(_Traits::length(__s)) {} /* ... */ _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY bool starts_with(basic_string_view __s) const _NOEXCEPT { return size() >= __s.size() && compare(0, __s.size(), __s) == 0; } /* ... */ _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY bool starts_with(const value_type* __s) const _NOEXCEPT { return starts_with(basic_string_view(__s)); } /* ... */ } ``` As far as I can see, it's not yet implemented in `libstdc++`, and I don't have access to the Windows standard library. I think it's a bit of a trade-off, with this version you do an additional scan but gain vectorizability (if that is a word). If you don't know the length in advance, you can't really vectorize because you might accidentally read off the end of the string. There does seem to be another free optimization here though: We don't need the full length of the prefix, as soon as we're at length `s.size()+1` we can abort. - Benno ----------------------------------------------------------- This is an automatically generated e-mail. To reply, visit: https://reviews.apache.org/r/63367/#review200870 ----------------------------------------------------------- On April 3, 2018, 4:27 p.m., Benno Evers wrote: > > ----------------------------------------------------------- > This is an automatically generated e-mail. To reply, visit: > https://reviews.apache.org/r/63367/ > ----------------------------------------------------------- > > (Updated April 3, 2018, 4:27 p.m.) > > > Review request for mesos, Alexander Rukletsov and Benjamin Mahler. > > > Repository: mesos > > > Description > ------- > > This saves an unnecessary memory allocation when > testing if a string starts or ends with a string literal, > which accounts for almost all usages of these functions > in Mesos and in libprocess. > > > Diffs > ----- > > 3rdparty/stout/include/stout/strings.hpp > 067a7923c02342bccd9be1136a981fd6b0e0e9b4 > 3rdparty/stout/tests/strings_tests.cpp > 395540aad88c76a66a43a54edfe9ca1a2d46d3b4 > > > Diff: https://reviews.apache.org/r/63367/diff/5/ > > > Testing > ------- > > > Thanks, > > Benno Evers > >