Boost C++ Libraries: Ticket #5564: lexical_cast performance boost for signed and unsigned char types https://svn.boost.org/trac10/ticket/5564 <p> We were looking through the sources of boost::lexical_cast library, and found a possible way of optimization.<br /> </p> <p> According to the [27.6.2.5.4 Character inserter function templates] of Programming languages - C++ standard, basic_ostream::operator&lt;&lt; reinterpret_casts unsigned char* and signed char* to char* and calls operator&lt;&lt;. So we can convert from unsigned char* and signed char* types to char* type and use optimized boost::lexical_cast coversion methods without construction of basic_streambuf&lt;CharT&gt; objects. Same approach can be used with signed char and unsigned char (we can just static_cast them to char type).<br /> </p> <p> According to the [27.6.1.2.3 basic_istream::operator&gt;&gt;] of Programming languages - C++ standard, basic_istream::operator&gt;&gt; for unsigned chars and signed chars behaves the same way, as for char type. boost::lexical_cast library treats remained in stream data as an error, so when the sizeof(CharT) is bigger than sizeof(char), the there will remain some data in stream and such situation can be interpreted as error. We can detect such cases at compile time, using BOOST_STATIC_ASSERT( sizeof(CharT) == sizeof(signed char) );<br /> </p> <p> Using those optimizations, we won't construct basic_streambuf&lt;CharT&gt; object, so there will be no dynamic memory allocation and we will use the lexical_cast algorithms optimized for char and char* types.<br /> </p> <p> Patch contains the required modifications and was tested on gcc4.5 </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5564 Trac 1.4.3 Bolotov Ilya, Mishakov Daniil <daniil987@…> Wed, 25 May 2011 09:50:48 GMT attachment set https://svn.boost.org/trac10/ticket/5564 https://svn.boost.org/trac10/ticket/5564 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">lexical_cast.diff</span> </li> </ul> <p> Patch for the trunk version </p> Ticket Antony Polukhin Thu, 26 May 2011 15:07:37 GMT status changed https://svn.boost.org/trac10/ticket/5564#comment:1 https://svn.boost.org/trac10/ticket/5564#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> Ticket Antony Polukhin Fri, 27 May 2011 04:22:27 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/5564#comment:2 https://svn.boost.org/trac10/ticket/5564#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/72203" title="Fixes #5564. Adds tests on #5564 and more tests on #5557">[72203]</a>) Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/5564" title="#5564: Patches: lexical_cast performance boost for signed and unsigned char types (closed: fixed)">#5564</a>. Adds tests on <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/5564" title="#5564: Patches: lexical_cast performance boost for signed and unsigned char types (closed: fixed)">#5564</a> and more tests on <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/5557" title="#5557: Bugs: lexical_cast&lt;int&gt;(&#34;+2&#34;) failure (closed: fixed)">#5557</a> </p> Ticket