220 5808 <b06daeb3-a585-4c55-aebf-cae643146838@isocpp.org> article
Path: news.gmane.org!not-for-mail
From: Bengt Gustafsson <bengt.gustafsson@beamways.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Remove vector<bool>?
Date: Sun, 25 Aug 2013 14:28:42 -0700 (PDT)
Lines: 226
Approved: news@gmane.org
Message-ID: <b06daeb3-a585-4c55-aebf-cae643146838@isocpp.org>
References: <CAGsORuDQdeWAO5yjA6=h0hW9Y0Sj4F1gcHB61udw5JFmtx+g4Q@mail.gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; 
	boundary="----=_Part_62_22613202.1377466122666"
X-Trace: ger.gmane.org 1377466121 16242 80.91.229.3 (25 Aug 2013 21:28:41 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Sun, 25 Aug 2013 21:28:41 +0000 (UTC)
Cc: lichray@gmail.com
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBCRIRSPDTQIRBC7O5GIAKGQECHDZLTY@isocpp.org Sun Aug 25 23:28:45 2013
Return-path: <std-proposals+bncBCRIRSPDTQIRBC7O5GIAKGQECHDZLTY@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-qe0-f69.google.com ([209.85.128.69])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBCRIRSPDTQIRBC7O5GIAKGQECHDZLTY@isocpp.org>)
	id 1VDhrY-0002mJ-NO
	for gclcip-std-proposals@m.gmane.org; Sun, 25 Aug 2013 23:28:44 +0200
Original-Received: by mail-qe0-f69.google.com with SMTP id 5sf2889130qeb.4
        for <gclcip-std-proposals@m.gmane.org>; Sun, 25 Aug 2013 14:28:43 -0700 (PDT)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=google.com; s=20120113;
        h=date:from:to:cc:message-id:in-reply-to:references:subject
         :mime-version:x-original-sender:reply-to:precedence:mailing-list
         :list-id:list-post:list-help:list-archive:list-subscribe
         :list-unsubscribe:content-type;
        bh=FyJda7nXi733kJGNOGF8KBdT34wUP/eK6NRn1gj/lS8=;
        b=EUpspuFRVtVACvnU2jPP0UIEapedHeLEqYF8hSxCwg++xEzhZw9yxmc0sQvEB1oSeO
         XEHURmUYLqo6TxRn0JTTUl9HEFVUR2ujfHdaOGoSDDQhgQWKg/8amkTpQ61YwMXd6Q+7
         6spbwWMOe+evOzdPG/7gOiEsoAGjdJ7W01YCVZiRONgRSzgx5kksiQzS516cXGWLZmqC
         C3b61dP5weQI3M2PYSJXMbQQgAaIPLo1s2fa/uIWBGiO9Og3bmyYCY5C29Z0yu9SY3gu
         XascQhsDy+t/ZFNv/ls3OlG6JZfARDAbP45SGBNeF1Hkaf7ZS2rLqPyIV7Xv4aUU+IUZ
         tJcg==
X-Received: by 10.236.68.193 with SMTP id l41mr4059687yhd.19.1377466123702;
        Sun, 25 Aug 2013 14:28:43 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.49.4.231 with SMTP id n7ls2106471qen.14.gmail; Sun, 25 Aug
 2013 14:28:43 -0700 (PDT)
X-Received: by 10.49.99.65 with SMTP id eo1mr435392qeb.3.1377466123230;
        Sun, 25 Aug 2013 14:28:43 -0700 (PDT)
In-Reply-To: <CAGsORuDQdeWAO5yjA6=h0hW9Y0Sj4F1gcHB61udw5JFmtx+g4Q@mail.gmail.com>
X-Original-Sender: bengt.gustafsson@beamways.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: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:5808
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/5808>

------=_Part_62_22613202.1377466122666
Content-Type: text/plain; charset=ISO-8859-1

I remember in pre-standard days when I started out with C++ that the STL 
implementation we used had a special bit_vector container on the side from 
vector<bool> which was not in any way specialized. Now that the standard is 
there and has been set for long it is in my opinion far to late to revert 
the specialization, although introducing it was probably not so smart in 
the first place.

Going back to the original paper we are discussing here I noticed that the 
objectives for the suggested removal of the vector<bool> specialization was 
mostly due to formalistic reasons. The exception is the well known problems 
with tryinging to  create a pointer to a bit and some performance issues. 
Here are my comments:

- The pointer creation problem is not such an important issue as it is 
immediately flagged as an error by the compiler. For example VS2012 gives 
an "illegal indirection" error, as you can't take the address of the by 
value return of &. It would of course be good if the diagnostic was easier 
to understand but I don't see that it is significantly worse than other 
error messages. 

- The performance of the iterator, and its dereference operator etc. may be 
better or worse than for instance for a vector<char>, which would be the 
logical "benchmark". With today's computer hardware the CPU itself is 
usually much faster than memory accesses so the reduced number of cache 
misses (by a factor of 8 or more) would seem likely to outweigh the cost of 
more bitwise operations, at least for larger vectors.

- Thanks to the vector<bool> iterators being special classes it should be 
possible for the caring library implementer to specialize select algortihms 
such as find. As bool itself has a very limited set of values there is a 
reasonably small subset of the standard algorithms that make sense anyway. 
To make this possible a "wordwise" access possibility must be added to 
vector<bool>::iterator, as well as an accessor for the bit offset within 
the current word. This API can be defined by each library implementer 
(possibly using friend declarations to the algorithm specializations) or it 
could be standardized so that new user defined algorithms working on 
vector<bool> can use them in a well defined and portable way.

Thus, I would suggest to:

- Run a couple of benchmarks for regular usage to verify if the concerns 
about undue slowness of the iterator operations is a big problem, a small 
problem or not a problem.

- Define that vector<bool>::iterator has an API to access more than one bit 
at a time, a quick sketch would be:

    class iterator {
        ...
        typedef X word_type;
        static const int bits_per_word = sizeof(word_type) * 8;
        
        size_t bit_now() const;        // bit within the word that the 
iterator points at now*
        word_type& word_now();   // reference would be const for a 
const_iterator
    };

- Specialize some algorithms using the above API (this would be at the 
discretion of library implementors I guess, but a reference implementation 
would hint at attainable performance gain levels). Most important 
algorithms from <algorithm> would be:

find
count
search
search_n
copy
swap_ranges
reverse
rotate

Most other algorithms, for instance replace, is meaningless for bool values 
(the only thing you can do with replace is to make all values 0 or 1!).

By the way I can't remember having used vector<bool> despite now about 20 
years of C++ so this issue is maybe not extremely important. If I were to 
use it, however, I would think that inserting/removing bits in the middle 
in a high performance way would be a very desirable feature in many 
applications. I say this because the use case when you don't is relatively 
easy to roll your own, but as soon as you start moving the bits around you 
want someone else to iron out the wrinkles for you, and that's exactly what 
we have a standard library for!

* A problem here is that many implementations (all?) use a bit mask to make 
iterator accesses faster. Converting this bit to a number is costly on most 
processors. Maybe the API would be better off if it returned the bit mask 
instead, as it is less costly to convert from bit number to bit mask 
(1<<bit_no).

Den tisdagen den 28:e maj 2013 kl. 17:50:23 UTC+2 skrev Zhihao Yuan:
>
> I saw Herb's paper: 
>
>   http://www.gotw.ca/publications/N1185.pdf 
>
> But it's not adopted. 
>
> Anyway, vector<bool> sucks, and we know it.  How about to entirely 
> remove it? 
>
> -- 
> Zhihao Yuan, ID lichray 
> The best way to predict the future is to invent it. 
> ___________________________________________________ 
> 4BSD -- http://4bsd.biz/ 
>

-- 

--- 
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_62_22613202.1377466122666
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>I remember in pre-standard days when I started out wi=
th C++ that the STL implementation we used had a special bit_vector contain=
er on the side from vector&lt;bool&gt; which was not in any way specialized=
.. Now that the standard is there and has been set for long it is in my opin=
ion far to late to revert the specialization, although introducing it was p=
robably not so smart in the first place.</div><div><br></div>Going back to =
the original paper we are discussing here I noticed that the objectives for=
 the suggested removal of the vector&lt;bool&gt; specialization was mostly =
due to formalistic reasons. The exception is the well known problems with t=
ryinging to &nbsp;create a pointer to a bit and some performance issues. He=
re are my comments:<div><br></div><div>- The pointer creation problem is no=
t such an important issue as it is immediately flagged as an error by the c=
ompiler. For example VS2012 gives an "illegal indirection" error, as you ca=
n't take the address of the by value return of &amp;. It would of course be=
 good if the diagnostic was easier to understand but I don't see that it is=
 significantly worse than other error messages.&nbsp;</div><div><br></div><=
div>- The performance of the iterator, and its dereference operator etc. ma=
y be better or worse than for instance for a vector&lt;char&gt;, which woul=
d be the logical "benchmark". With today's computer hardware the CPU itself=
 is usually much faster than memory accesses so the reduced number of cache=
 misses (by a factor of 8 or more) would seem likely to outweigh the cost o=
f more bitwise operations, at least for larger vectors.</div><div><br></div=
><div>- Thanks to the vector&lt;bool&gt; iterators being special classes it=
 should be possible for the caring library implementer to specialize select=
 algortihms such as find. As bool itself has a very limited set of values t=
here is a reasonably small subset of the standard algorithms that make sens=
e anyway. To make this possible a "wordwise" access possibility must be add=
ed to vector&lt;bool&gt;::iterator, as well as an accessor for the bit offs=
et within the current word. This API can be defined by each library impleme=
nter (possibly using friend declarations to the algorithm specializations) =
or it could be standardized so that new user defined algorithms working on =
vector&lt;bool&gt; can use them in a well defined and portable way.</div><d=
iv><br></div><div>Thus, I would suggest to:</div><div><br></div><div>- Run =
a couple of benchmarks for regular usage to verify if the concerns about un=
due slowness of the iterator operations is a big problem, a small problem o=
r not a problem.</div><div><br></div><div>- Define that vector&lt;bool&gt;:=
:iterator has an API to access more than one bit at a time, a quick sketch =
would be:</div><div><br></div><div>&nbsp; &nbsp; class iterator {</div><div=
>&nbsp; &nbsp; &nbsp; &nbsp; ...</div><div>&nbsp; &nbsp; &nbsp; &nbsp; type=
def X word_type;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; static const int bit=
s_per_word =3D sizeof(word_type) * 8;</div><div>&nbsp; &nbsp; &nbsp; &nbsp;=
&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; size_t bit_now() const; &nbsp;=
 &nbsp; &nbsp; &nbsp;// bit within the word that the iterator points at now=
*</div><div>&nbsp; &nbsp; &nbsp; &nbsp; word_type&amp; word_now(); &nbsp; /=
/ reference would be const for a const_iterator</div><div>&nbsp; &nbsp; };<=
/div><div><br></div><div>- Specialize some algorithms using the above API (=
this would be at the discretion of library implementors I guess, but a refe=
rence implementation would hint at attainable performance gain levels). Mos=
t important algorithms from &lt;algorithm&gt; would be:</div><div><br></div=
><div>find</div><div>count</div><div>search</div><div>search_n</div><div>co=
py</div><div>swap_ranges</div><div>reverse</div><div>rotate</div><div><br><=
/div><div>Most other algorithms, for instance replace, is meaningless for b=
ool values (the only thing you can do with replace is to make all values 0 =
or 1!).</div><div><br></div><div>By the way I can't remember having used ve=
ctor&lt;bool&gt; despite now about 20 years of C++ so this issue is maybe n=
ot extremely important. If I were to use it, however, I would think that in=
serting/removing bits in the middle in a high performance way would be a ve=
ry desirable feature in many applications. I say this because the use case =
when you don't is relatively easy to roll your own, but as soon as you star=
t moving the bits around you want someone else to iron out the wrinkles for=
 you, and that's exactly what we have a standard library for!<br><br>* A pr=
oblem here is that many implementations (all?) use a bit mask to make itera=
tor accesses faster. Converting this bit to a number is costly on most proc=
essors. Maybe the API would be better off if it returned the bit mask inste=
ad, as it is less costly to convert from bit number to bit mask (1&lt;&lt;b=
it_no).</div><div><br>Den tisdagen den 28:e maj 2013 kl. 17:50:23 UTC+2 skr=
ev Zhihao Yuan:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-=
left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">I saw Herb's pa=
per:
<br>
<br>&nbsp; <a href=3D"http://www.gotw.ca/publications/N1185.pdf" target=3D"=
_blank">http://www.gotw.ca/<wbr>publications/N1185.pdf</a>
<br>
<br>But it's not adopted.
<br>
<br>Anyway, vector&lt;bool&gt; sucks, and we know it. &nbsp;How about to en=
tirely
<br>remove it?
<br>
<br>--
<br>Zhihao Yuan, ID lichray
<br>The best way to predict the future is to invent it.
<br>______________________________<wbr>_____________________
<br>4BSD -- <a href=3D"http://4bsd.biz/" target=3D"_blank">http://4bsd.biz/=
</a>
<br></blockquote></div></div>

<p></p>

-- <br />
&nbsp;<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 std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<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_62_22613202.1377466122666--

.
