Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: No small-vector optimization anymore?

45 views
Skip to first unread message

Alf P. Steinbach

unread,
Oct 13, 2016, 5:51:06 PM10/13/16
to
On 13.10.2016 23:24, Stefan Ram wrote:
> In a video someone said, there was no small-vector optimization
> anymore. Some requirement of C++11 or C++14 (inadvertently?) made it
> impossible.

Yes, that iterators and references remain valid and refer to the same
items after a swap.

Also, that a swap doesn't copy or swap or move individual items.

I.e. swap really means swapping buffers, not just buffer contents.

• • •

C++11 §23.2.1/8 (a.k.a. container.requirements.general/8):

“The expression a.swap(b), for containers a and b of a standard
container type other than array, shall exchange the values of a and b
without invoking any move, copy, or swap operations on the individual
container elements. Any Compare, Pred, or Hash objects belonging to a
and b shall be swappable and shall be exchanged by unqualified calls to
non-member swap. If
allocator_traits<allocator_type>::propagate_on_container_swap::value is
true, then the allocators of a and b shall also be exchanged using an
unqualified call to non-member swap. Otherwise, they shall not be
swapped, and the behavior is undefined unless a.get_allocator() ==
b.get_allocator(). Every iterator referring to an element in one
container before the swap shall refer to the same element in the other
container after the swap. It is unspecified whether an iterator with
value a.end() before the swap will have value b.end() after the swap.



> But he said that at the same time, people took great care to have a
> small-string optimization. So, how do you store an array of two
> double values in modern C++? I made up the following code. What do
> you think about it?
>
> #include <initializer_list> #include <ostream> #include <iostream>
> #include <string>
>
> int main() { ::std::basic_string< double > s{ 1.2, 3.6 }; ::std::cout
> << s.at( 0 )<< '\n'; }
>

Well, in order to properly specialize `std::basic_string` you should
provide a corresponding `std::char_traits`. That doesn't make sense
here. And from that you can conclude, that `std::basic_string` was not
made for this.

I'd simply use a raw array, or possibly a `std::array`.

If you want the predictable/reliable range checking of `at` you can just
code up a little helper function for indexing.


Cheers & hth.,

- Alf

Mr Flibble

unread,
Oct 13, 2016, 6:05:11 PM10/13/16
to
Boost has a small-vector optimization container; I have also created one
(neolib::vecarray).

/Flibble

red floyd

unread,
Oct 13, 2016, 7:12:53 PM10/13/16
to
On 10/13/2016 2:24 PM, Stefan Ram wrote:
> In a video someone said, there was no small-vector optimization
> anymore. Some requirement of C++11 or C++14 (inadvertently?)
> made it impossible. But he said that at the same time, people
> took great care to have a small-string optimization. So, how
> do you store an array of two double values in modern C++?
> I made up the following code. What do you think about it?
>
> #include <initializer_list>
> #include <ostream>
> #include <iostream>
> #include <string>
>
> int main()
> { ::std::basic_string< double > s{ 1.2, 3.6 };
> ::std::cout << s.at( 0 )<< '\n'; }
>

#include <array>
#include <iostream>

int main()
{
::std::array<double,2> arr{1.2, 3.6};
for (auto d : arr)
::std::cout << d << '\n';
}

woodb...@gmail.com

unread,
Oct 13, 2016, 9:58:46 PM10/13/16
to
On Thursday, October 13, 2016 at 6:12:53 PM UTC-5, red floyd wrote:
> On 10/13/2016 2:24 PM, Stefan Ram wrote:
> > In a video someone said, there was no small-vector optimization
> > anymore. Some requirement of C++11 or C++14 (inadvertently?)
> > made it impossible. But he said that at the same time, people
> > took great care to have a small-string optimization. So, how
> > do you store an array of two double values in modern C++?
> > I made up the following code. What do you think about it?

That sounds like this talk:

https://www.youtube.com/watch?v=vElZc6zSIXM&spfreload=1

He talks about it in the first ten minutes I think and
then there's a question at the end about it also.

> >
> > #include <initializer_list>
> > #include <ostream>
> > #include <iostream>
> > #include <string>
> >
> > int main()
> > { ::std::basic_string< double > s{ 1.2, 3.6 };
> > ::std::cout << s.at( 0 )<< '\n'; }
> >

That's twisted.

>
> #include <array>
> #include <iostream>
>
> int main()
> {
> ::std::array<double,2> arr{1.2, 3.6};
> for (auto d : arr)
> ::std::cout << d << '\n';
> }

That's better.


Brian
Ebenezer Enterprises
http://webEbenezer.net

Gareth Owen

unread,
Oct 14, 2016, 2:32:00 AM10/14/16
to
Mr Flibble <flibbleREM...@i42.co.uk> writes:

> Boost has a small-vector optimization container; I have also created
> one (neolib::vecarray).

The LLVM libray has one also.

Gareth Owen

unread,
Oct 14, 2016, 2:33:34 AM10/14/16
to
red floyd <no....@its.invalid> writes:

> #include <array>
> #include <iostream>
>
> int main()
> {
> ::std::array<double,2> arr{1.2, 3.6};
> for (auto d : arr)
> ::std::cout << d << '\n';
> }

Yes, but you can't dynamically resize it later.

red floyd

unread,
Oct 14, 2016, 1:22:31 PM10/14/16
to
That wasn't specified as a requirement.

Mr Flibble

unread,
Oct 14, 2016, 2:29:51 PM10/14/16
to
It should be an obvious requirement for a small-vector optimized vector.

/Flibble
0 new messages