Topic: Configurable Small Object Optimization for vector and string
Author: Matthew Fioravante <fmatthew5876@gmail.com>
Date: Tue, 3 Mar 2015 20:00:15 -0800 (PST)
Raw View
------=_Part_5552_679899402.1425441615713
Content-Type: multipart/alternative;
boundary="----=_Part_5553_308763727.1425441615713"
------=_Part_5553_308763727.1425441615713
Content-Type: text/plain; charset=UTF-8
Modern implementations are encouraged to implement the small string
optimization (with implementation defined size) for std::string, reasoning
that most strings tend to be small. While this may work very well in the
general case, it can be either inadequate or a pessimization in other cases.
It would be nice if string got an additional size_t template argument:
template <typename CharT, typename Traits, typename Allocator, size_t
SmallSize = /* implementation defiend*/> class basic_string;
Implementations can make the default SmallSize whatever they like. Setting
SmallSize to 0 would disable SSO, falling back to dynamic allocation for
all strings.
Configuring the SOO size std::string has many uses cases:
- If you want to convert a large numeric value into a string and that
numeric value has a compile time bounded range of values then a string with
an SSO buffer large enough to store the largest possible representation
without allocating memory is the most efficient string to return from
parsing routines.
- A local variable string used to store an operating system path could
benefit from using a much larger buffer size of a few kilobytes or more.
- Identifiers such a stock tickers which have a fixed upper bound on length.
Disabling SOO (by setting the size to 0), also has important use cases:
- When storing a std::string as a data member of the class and you know the
string will either always be large -OR- the string data is not hot data and
you want to keep it off the same cacheline as *this*. In this case SOO is a
major pessimization as it wastes several bytes of precious cache line space.
As a further extension to solving the above issue. An implementation could
optimize basic_string<T,Traits,Alloc,0> for size by only storing a single
pointer in the basic_string object directly and storing the size, capacity,
and the character data within the allocated buffer.
The small object optimization is also very useful for vector. A lot of C++
libraries have implemented a small_vector type which works exactly like
std::vector except that it implements the SOO. Instead of implementing
another clone of vector, we can add a size_t template argument to vector to
also optionally enable SOO.
template <typename T, typename Allocator, size_t SmallSize = 0> class vector
;
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_5553_308763727.1425441615713
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Modern implementations are encouraged to implement the sma=
ll string optimization (with implementation defined size) for std::string, =
reasoning that most strings tend to be small. While this may work very well=
in the general case, it can be either inadequate or a pessimization in oth=
er cases.<div><br></div><div>It would be nice if string got an additional s=
ize_t template argument:</div><div><br></div><div><div class=3D"prettyprint=
" style=3D"border: 1px solid rgb(187, 187, 187); word-wrap: break-word; bac=
kground-color: rgb(250, 250, 250);"><code class=3D"prettyprint"><div class=
=3D"subprettyprint"><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"><br></span><font color=3D"#660066"><span style=3D"color: #008;" class=3D=
"styled-by-prettify">template</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-=
prettify"><</span><span style=3D"color: #008;" class=3D"styled-by-pretti=
fy">typename</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> </span><span style=3D"color: #606;" class=3D"styled-by-prettify">CharT</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">,</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">typename</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #606;" class=3D"styled-by-prettify">Traits</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"styl=
ed-by-prettify">typename</span><span style=3D"color: #000;" class=3D"styled=
-by-prettify"> </span><span style=3D"color: #606;" class=3D"styled-by-prett=
ify">Allocator</span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> size=
_t </span><span style=3D"color: #606;" class=3D"styled-by-prettify">SmallSi=
ze</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"c=
olor: #800;" class=3D"styled-by-prettify">/* implementation defiend*/</span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">></span><span=
style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D=
"color: #008;" class=3D"styled-by-prettify">class</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify"> basic_string</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">;</span></font></div></code></div=
><br>Implementations can make the default SmallSize whatever they like. Set=
ting SmallSize to 0 would disable SSO, falling back to dynamic allocation f=
or all strings.<br><div><br></div><div>Configuring the SOO size std::string=
has many uses cases:</div><div><br></div><div>- If you want to convert a l=
arge numeric value into a string and that numeric value has a compile time =
bounded range of values then a string with an SSO buffer large enough to st=
ore the largest possible representation without allocating memory is the mo=
st efficient string to return from parsing routines.</div><div>- A local va=
riable string used to store an operating system path could benefit from usi=
ng a much larger buffer size of a few kilobytes or more.</div><div>- Identi=
fiers such a stock tickers which have a fixed upper bound on length.</div><=
div><br></div><div>Disabling SOO (by setting the size to 0), also has impor=
tant use cases:</div><div><br></div><div>- When storing a std::string as a =
data member of the class and you know the string will either always be larg=
e -OR- the string data is not hot data and you want to keep it off the same=
cacheline as <i>this</i>. In this case SOO is a major pessimization as it =
wastes several bytes of precious cache line space.</div></div><div><br></di=
v><div>As a further extension to solving the above issue. An implementation=
could optimize basic_string<T,Traits,Alloc,0> for size by only stori=
ng a single pointer in the basic_string object directly and storing the siz=
e, capacity, and the character data within the allocated buffer.</div><div>=
<br></div><div>The small object optimization is also very useful for vector=
.. A lot of C++ libraries have implemented a small_vector type which works e=
xactly like std::vector except that it implements the SOO. Instead of imple=
menting another clone of vector, we can add a size_t template argument to v=
ector to also optionally enable SOO.</div><div><br></div><div><div class=3D=
"prettyprint" style=3D"border: 1px solid rgb(187, 187, 187); word-wrap: bre=
ak-word; background-color: rgb(250, 250, 250);"><code class=3D"prettyprint"=
><div class=3D"subprettyprint"><font color=3D"#660066"><span style=3D"color=
: #008;" class=3D"styled-by-prettify">template</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify"><</span><span style=3D"color: #008;" class=3D=
"styled-by-prettify">typename</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> T</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">typenam=
e</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><=
span style=3D"color: #606;" class=3D"styled-by-prettify">Allocator</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> size_t </span><span style=
=3D"color: #606;" class=3D"styled-by-prettify">SmallSize</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"> </span><span style=3D"color: #066;" class=
=3D"styled-by-prettify">0</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">></span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">=
class</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> vect=
or</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span>=
</font></div></code></div></div><div><br></div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_5553_308763727.1425441615713--
------=_Part_5552_679899402.1425441615713--
.