220 16709 <CAFdMc-0n-WL8zOoRAUQRQUNBWafbyYtSKY9bWg7UmFxoRc_2zg@mail.gmail.com> article
Path: news.gmane.org!not-for-mail
From: "dgutson ." <danielgutson@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Configurable Small Object Optimization for vector
 and string
Date: Wed, 4 Mar 2015 12:39:15 -0300
Lines: 272
Approved: news@gmane.org
Message-ID: <CAFdMc-0n-WL8zOoRAUQRQUNBWafbyYtSKY9bWg7UmFxoRc_2zg@mail.gmail.com>
References: <65b9af57-f8fb-4b11-9da1-1f5fc504dacf@isocpp.org>
	<3248556.Isfy8bpADO@tjmaciei-mobl4>
	<7d0b7959-54e3-4b59-bb69-d91949509b6e@isocpp.org>
	<CAD6_Qj8cbCy7Q=eEWyOpuncG=d2jKAMV36b3QTWc6VxK=34N+w@mail.gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary=001a11421f2efa4c1405107840bd
X-Trace: ger.gmane.org 1425483559 9454 80.91.229.3 (4 Mar 2015 15:39:19 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Wed, 4 Mar 2015 15:39:19 +0000 (UTC)
To: std-proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBDE3NBMV6UFBBJGO3STQKGQEH4UZKUY@isocpp.org Wed Mar 04 16:39:19 2015
Return-path: <std-proposals+bncBDE3NBMV6UFBBJGO3STQKGQEH4UZKUY@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-pd0-f199.google.com ([209.85.192.199])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBDE3NBMV6UFBBJGO3STQKGQEH4UZKUY@isocpp.org>)
	id 1YTBOI-0006GU-Ku
	for gclcip-std-proposals@m.gmane.org; Wed, 04 Mar 2015 16:39:19 +0100
Original-Received: by pdno5 with SMTP id o5sf67072288pdn.3
        for <gclcip-std-proposals@m.gmane.org>; Wed, 04 Mar 2015 07:39:17 -0800 (PST)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:mime-version:in-reply-to:references:date
         :message-id:subject:from:to:content-type:x-original-sender
         :x-original-authentication-results:reply-to:precedence:mailing-list
         :list-id:list-post:list-help:list-archive:list-subscribe
         :list-unsubscribe;
        bh=9l5zssh+weWLm2kCVjwsdOOSuUbQwx+ejdYB3jr1X0c=;
        b=THVczKnIkfkUU1L/u/bTf1A3MoRDQX7B6wHn7vBSe/l9x8pK3qniPu+JDUf6JGnFTj
         /U4ANzZmO1Y4Tddfkgn3+xsxZ0osxVUeM5QHG57dQ8A/mc33MC7pw6oZGoxZ/buMLDyo
         Tw8vXwdYfG6CAV1LHJPTEWVvuoHFoczuaOiEwNozzR6Lyzs9ZybVj83N3TX65FoSmNJJ
         JHF+RUwSQIObFwO4LA+ubCR98wyXnSDvjvuJ21gduZFi2V9ecxS+mz/VlCEVoxH0z+nU
         bkK6f3ptqiyq/XQWEu7EHzCC3kEdLXTDKzI16Ba1PEnsZKVoDtlycSdSJ/6bxKwg9VvN
         RPwg==
X-Gm-Message-State: ALoCoQnnUOBxl7P5wAASNKx6077tQAog1q7M9QGzdG2uXpvgWziGwm5ma380qi6gCfsSRV25tXIt
X-Received: by 10.66.144.69 with SMTP id sk5mr4505000pab.41.1425483557714;
        Wed, 04 Mar 2015 07:39:17 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.107.154.85 with SMTP id c82ls589391ioe.42.gmail; Wed, 04 Mar
 2015 07:39:16 -0800 (PST)
X-Received: by 10.50.107.7 with SMTP id gy7mr12085660igb.49.1425483555977;
        Wed, 04 Mar 2015 07:39:15 -0800 (PST)
Original-Received: from mail-ig0-x229.google.com (mail-ig0-x229.google.com. [2607:f8b0:4001:c05::229])
        by mx.google.com with ESMTPS id rt8si15424211igb.31.2015.03.04.07.39.15
        for <std-proposals@isocpp.org>
        (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Wed, 04 Mar 2015 07:39:15 -0800 (PST)
Received-SPF: pass (google.com: domain of danielgutson@gmail.com designates 2607:f8b0:4001:c05::229 as permitted sender) client-ip=2607:f8b0:4001:c05::229;
Original-Received: by igkb16 with SMTP id b16so37534960igk.1
        for <std-proposals@isocpp.org>; Wed, 04 Mar 2015 07:39:15 -0800 (PST)
X-Received: by 10.107.168.5 with SMTP id r5mr11872413ioe.87.1425483555816;
 Wed, 04 Mar 2015 07:39:15 -0800 (PST)
Original-Received: by 10.36.56.70 with HTTP; Wed, 4 Mar 2015 07:39:15 -0800 (PST)
Original-Received: by 10.36.56.70 with HTTP; Wed, 4 Mar 2015 07:39:15 -0800 (PST)
In-Reply-To: <CAD6_Qj8cbCy7Q=eEWyOpuncG=d2jKAMV36b3QTWc6VxK=34N+w@mail.gmail.com>
X-Original-Sender: danielgutson@gmail.com
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of danielgutson@gmail.com designates 2607:f8b0:4001:c05::229 as
 permitted sender) smtp.mail=danielgutson@gmail.com;       dkim=pass
 header.i=@gmail.com;       dmarc=pass (p=NONE dis=NONE) header.from=gmail.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Google-Group-Id: 399137483710
List-Post: <http://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>,
 <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:16709
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/16709>

--001a11421f2efa4c1405107840bd
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

El 04/03/2015 12:31, "David Rodr=C3=ADguez Ibeas" <dibeas@ieee.org> escribi=
=C3=B3:
>
> I think that your problem is/should be addressed in a different
direction, having an array_view/vector_view (I have heard about those in
the past, I don't know if there has been any actual proposal, but it would
be a simplified string_view).  The 'ChessPosition' class looks like it
should be an 'std::array', and if interfaces that only act on the data are
implemented in terms of a *_view, then it should work just fine.
>
> In current C++, and if you need a dynamic size (i.e. std::array does not
solve your problem), you can use an allocator to control where the memory
comes from and make it local, you would still be paying for the additional
bookkeeping inside vector, but if you need it to actually work that is
available today...

I had the same thought. Why an allocator having an initial pre-allocated
buffer of user-specified length and then use basic_string with that
allocator wouldn't be an equivalent currently conformant solution?

>
>     David
>
> On Wed, Mar 4, 2015 at 2:42 AM, Arthur O'Dwyer <arthur.j.odwyer@gmail.com=
>
wrote:
>>
>> On Tuesday, March 3, 2015 at 9:19:36 PM UTC-8, Thiago Macieira wrote:
>>>
>>> On Tuesday 03 March 2015 20:00:15 Matthew Fioravante wrote:
>>> > 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.
>>
>> [...]
>>>
>>> Since std::foo<N> is a different type from std::foo<M> where N !=3D M, =
I
don't
>>> see the point in attempting to keep compatibility with the current
>>> basic_string. Instead, be explicit that this is a different type and
define the
>>> SmallSize default value to be sizeof(std::basic_string<CharT>).
>>
>>
>> In my opinion, "small_vector" would be a great addition to the standard
library, but only if small_vector<T,N>& and vector<T>& are
inter-convertible (in constant time). I believe that's doable, although I'm
not sure how to write the implementation in purely standard-conforming C++
=E2=80=94 it might require some reinterpret_casts.
>>
>> The use-case would be things like
>>
>> struct ChessPosition {
>>     std::small_vector<ChessPiece,32> pieces;
>> };
>>
>> where ChessPosition's copy constructor and destructor get called very
often, so copying the members needs to be very fast. Inlining the data (so
no heap allocation needs to happen) is a big win.
>>
>> But then do we just want ChessPiece pieces[32]? No, because (for the
programmer's sanity) we need to track pieces.size() (which won't always be
32; usually it will be much less) separately from the fixed number 32.
>>
>> But if we know a maximum size for the vector (32 items), do we really
want a kind of bounded_vector<ChessPiece,32> which would behave basically
like a std::array plus a size member? Well, that would definitely be useful
in some cases, and I do wish there were a standard implementation of such a
thing. But in the case of ChessPosition, we actually don't want that,
because as noted above, pieces.size() will usually be much less than 32.
We'd like to be able to tune the N parameter =E2=80=94 for example, if we u=
se
small_vector<ChessPiece,16>, then early-game positions with more than 16
pieces will require a heap allocation (relatively slower), but every
position will take half the RAM (shrinking our memory footprint and the
expense of copy-constructing these things).
>>
>> Some of these use-cases might be solved other ways. For example,
bounded_vector could be described as "a vector_view onto an array."
>>
>> my $.02,
>> =E2=80=93Arthur
>>
>> --
>>
>> ---
>> 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/.
>
>
> --
>
> ---
> 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/.

--=20

---=20
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 e=
mail 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-proposa=
ls/.

--001a11421f2efa4c1405107840bd
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<p dir=3D"ltr"><br>
El 04/03/2015 12:31, &quot;David Rodr=C3=ADguez Ibeas&quot; &lt;<a href=3D"=
mailto:dibeas@ieee.org">dibeas@ieee.org</a>&gt; escribi=C3=B3:<br>
&gt;<br>
&gt; I think that your problem is/should be addressed in a different direct=
ion, having an array_view/vector_view (I have heard about those in the past=
, I don&#39;t know if there has been any actual proposal, but it would be a=
 simplified string_view).=C2=A0 The &#39;ChessPosition&#39; class looks lik=
e it should be an &#39;std::array&#39;, and if interfaces that only act on =
the data are implemented in terms of a *_view, then it should work just fin=
e.<br>
&gt;<br>
&gt; In current C++, and if you need a dynamic size (i.e. std::array does n=
ot solve your problem), you can use an allocator to control where the memor=
y comes from and make it local, you would still be paying for the additiona=
l bookkeeping inside vector, but if you need it to actually work that is av=
ailable today... </p>
<p dir=3D"ltr">I had the same thought. Why an allocator having an initial p=
re-allocated buffer of user-specified length and then use basic_string with=
 that allocator wouldn&#39;t be an equivalent currently conformant solution=
?</p>
<p dir=3D"ltr">&gt;<br>
&gt; =C2=A0 =C2=A0 David<br>
&gt;<br>
&gt; On Wed, Mar 4, 2015 at 2:42 AM, Arthur O&#39;Dwyer &lt;<a href=3D"mail=
to:arthur.j.odwyer@gmail.com">arthur.j.odwyer@gmail.com</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; On Tuesday, March 3, 2015 at 9:19:36 PM UTC-8, Thiago Macieira wro=
te:<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; On Tuesday 03 March 2015 20:00:15 Matthew Fioravante wrote: <b=
r>
&gt;&gt;&gt; &gt; Modern implementations are encouraged to implement the sm=
all string <br>
&gt;&gt;&gt; &gt; optimization (with implementation defined size) for std::=
string, reasoning <br>
&gt;&gt;&gt; &gt; that most strings tend to be small. While this may work v=
ery well in the <br>
&gt;&gt;&gt; &gt; general case, it can be either inadequate or a pessimizat=
ion in other cases. <br>
&gt;&gt;<br>
&gt;&gt; [...]=C2=A0<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Since std::foo&lt;N&gt; is a different type from std::foo&lt;M=
&gt; where N !=3D M, I don&#39;t <br>
&gt;&gt;&gt; see the point in attempting to keep compatibility with the cur=
rent <br>
&gt;&gt;&gt; basic_string. Instead, be explicit that this is a different ty=
pe and define the <br>
&gt;&gt;&gt; SmallSize default value to be sizeof(std::basic_string&lt;Char=
T&gt;). <br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; In my opinion, &quot;small_vector&quot; would be a great addition =
to the standard library, but only if small_vector&lt;T,N&gt;&amp; and vecto=
r&lt;T&gt;&amp; are inter-convertible (in constant time). I believe that&#3=
9;s doable, although I&#39;m not sure how to write the implementation in pu=
rely standard-conforming C++ =E2=80=94 it might require some reinterpret_ca=
sts.<br>
&gt;&gt;<br>
&gt;&gt; The use-case would be things like<br>
&gt;&gt;<br>
&gt;&gt; struct ChessPosition {<br>
&gt;&gt; =C2=A0 =C2=A0 std::small_vector&lt;ChessPiece,32&gt; pieces; <br>
&gt;&gt; };<br>
&gt;&gt;<br>
&gt;&gt; where ChessPosition&#39;s copy constructor and destructor get call=
ed very often, so copying the members needs to be very fast. Inlining the d=
ata (so no heap allocation needs to happen) is a big win.<br>
&gt;&gt;<br>
&gt;&gt; But then do we just want ChessPiece pieces[32]? No, because (for t=
he programmer&#39;s sanity) we need to track pieces.size()=C2=A0(which won&=
#39;t always be 32; usually it will be much less) separately from the fixed=
 number 32.<br>
&gt;&gt;<br>
&gt;&gt; But if we know a maximum size for the vector (32 items), do we rea=
lly want a kind of bounded_vector&lt;ChessPiece,32&gt; which would behave b=
asically like a std::array plus a size member? Well, that would definitely =
be useful in some cases, and I do wish there were a standard implementation=
 of such a thing. But in the case of ChessPosition, we actually don&#39;t w=
ant that, because as noted above, pieces.size() will usually be much less t=
han 32. We&#39;d like to be able to tune the N parameter =E2=80=94 for exam=
ple, if we use small_vector&lt;ChessPiece,16&gt;, then early-game positions=
 with more than 16 pieces will require a heap allocation (relatively slower=
), but every position will take half the RAM (shrinking our memory footprin=
t and the expense of copy-constructing these things).<br>
&gt;&gt;<br>
&gt;&gt; Some of these use-cases might be solved other ways. For example, b=
ounded_vector could be described as &quot;a vector_view onto an array.&quot=
;<br>
&gt;&gt;<br>
&gt;&gt; my $.02,<br>
&gt;&gt; =E2=80=93Arthur<br>
&gt;&gt;<br>
&gt;&gt; -- <br>
&gt;&gt;<br>
&gt;&gt; --- <br>
&gt;&gt; You received this message because you are subscribed to the Google=
 Groups &quot;ISO C++ Standard - Future Proposals&quot; group.<br>
&gt;&gt; To unsubscribe from this group and stop receiving emails from it, =
send an email to <a href=3D"mailto:std-proposals%2Bunsubscribe@isocpp.org">=
std-proposals+unsubscribe@isocpp.org</a>.<br>
&gt;&gt; To post to this group, send email to <a href=3D"mailto:std-proposa=
ls@isocpp.org">std-proposals@isocpp.org</a>.<br>
&gt;&gt; Visit this group at <a href=3D"http://groups.google.com/a/isocpp.o=
rg/group/std-proposals/">http://groups.google.com/a/isocpp.org/group/std-pr=
oposals/</a>.<br>
&gt;<br>
&gt;<br>
&gt; -- <br>
&gt;<br>
&gt; --- <br>
&gt; You received this message because you are subscribed to the Google Gro=
ups &quot;ISO C++ Standard - Future Proposals&quot; group.<br>
&gt; To unsubscribe from this group and stop receiving emails from it, send=
 an email to <a href=3D"mailto:std-proposals%2Bunsubscribe@isocpp.org">std-=
proposals+unsubscribe@isocpp.org</a>.<br>
&gt; To post to this group, send email to <a href=3D"mailto:std-proposals@i=
socpp.org">std-proposals@isocpp.org</a>.<br>
&gt; Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/g=
roup/std-proposals/">http://groups.google.com/a/isocpp.org/group/std-propos=
als/</a>.<br>
</p>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />

--001a11421f2efa4c1405107840bd--

.
