[lexical cast] Performance improvements

Working with Boost Lexical Cast library found that some casts are working really slow. For example, cast from short to int is done via cast from short to char[], and cast from char[] to int. Cast from char* to string is done via string() constructor call and string`s append function call (instead of string constructor call with char array as a parameter). So I wrote patch to Boost Lexical Cast library, that returns Target directly constructed from Source in cases, when: 1) Source and Target are arithmetics (and not char or wchar_t), and all values of Source are in range of Target 2) Source and Target are both char or both wchar_t 3) Source is array of CharT and Target is std::basic_string<CharT, ...> 4) Source and Target are std::basic_string with same template parameters It gives performance boost more than 10 times in case 1) , about 7 times in case 2), and about 1.5 times in cases 3) and 4). Existing tests for Boost Lexical Cast library cover considered cases. Patch was successfully tested on Intel 11.1.072, g++-4.4.5, VC++9 compilers. P.S.: I`m a newbie here, so where shall I send this patch? Shall I test it on other compilers?

On 19 Mar 2011, at 20:44, Antony Polukhin wrote:
Working with Boost Lexical Cast library found that some casts are working really slow. For example, cast from short to int is done via cast from short to char[], and cast from char[] to int. Cast from char* to string is done via string() constructor call and string`s append function call (instead of string constructor call with char array as a parameter).
So I wrote patch to Boost Lexical Cast library, that returns Target directly constructed from Source in cases, when: 1) Source and Target are arithmetics (and not char or wchar_t), and all values of Source are in range of Target 2) Source and Target are both char or both wchar_t 3) Source is array of CharT and Target is std::basic_string<CharT, ...> 4) Source and Target are std::basic_string with same template parameters
Just one quick question, does your code have the same behaviour as the existing Lexical Cast when casting out of bounds (int -> unsigned int, int -> short, unsigned int -> int)? Chris

Antony Polukhin wrote:
Working with Boost Lexical Cast library found that some casts are working really slow.
[snip]
So I wrote patch to Boost Lexical Cast library, that returns [snip] It gives performance boost more than 10 times in case 1) , about 7 times in case 2), and about 1.5 times in cases 3) and 4).
Nice.
Patch was successfully tested on Intel 11.1.072, g++-4.4.5, VC++9 compilers.
P.S.: I`m a newbie here, so where shall I send this patch? Shall I test it on other compilers?
That set of compilers should be sufficient. Refer to <http://www.boost.org/support/bugs.html> for information on submitting patches. _____ Rob Stewart robert.stewart@sig.com Software Engineer using std::disclaimer; Dev Tools & Components Susquehanna International Group, LLP http://www.sig.com IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.

Message du 19/03/11 21:57 De : "Antony Polukhin" A : boost@lists.boost.org Copie à : Objet : [boost] [lexical cast] Performance improvements
Working with Boost Lexical Cast library found that some casts are working really slow. For example, cast from short to int is done via cast from short to char[], and cast from char[] to int. Cast from char* to string is done via string() constructor call and string`s append function call (instead of string constructor call with char array as a parameter).
So I wrote patch to Boost Lexical Cast library, that returns Target directly constructed from Source in cases, when: 1) Source and Target are arithmetics (and not char or wchar_t), and all values of Source are in range of Target 2) Source and Target are both char or both wchar_t 3) Source is array of CharT and Target is std::basic_string 4) Source and Target are std::basic_string with same template parameters
It gives performance boost more than 10 times in case 1) , about 7 times in case 2), and about 1.5 times in cases 3) and 4).
Hi, I don't know if you are aware of Boost.Conversion on the review schedule. This library provide a framework to specialize conversions from unrelated types much on the line you are proposing. The library provides a default conversion that uses the explicit constructor or conversion operator. In addition the library provides already a certain number of conversions between unrelated standard or boost types. I have in my TODO list the creation of specializations for types convertible using numeric_cast and lexical_cast, but they are not yet there. I need to see how to specialize the generic behavior when these operators are applicable, the main issue is that I want to make preference to the current conversion when available Target(source), but there is no way to check for this trait without compiler support. The main advantage of the library is that all the conversions will use the same prototype. convert_to(Source const&); 1) Source and Target are arithmetics (and not char or wchar_t), and all values of Source are in range of Target The default current behavior will not manage with bound checking, but the user or the library could add specialization for the builtin that uses numeric_cast. 2) Source and Target are both char or both wchar_t This is the identity, isn't it? The default behavior works then in this case. 3) Source is array of CharT and Target is std::basic_string This could be added easily in an extrinsic way (either by the user or as a specialization by the library). 4) Source and Target are std::basic_string with same template parameters This is the identity, isn't it? I think that it is better to improve my library that is there to manage with any conversion, and let lexical cast do what it did originally. Let me know what do you think. Best, Vicente

From Vicente BOTET, on Mon 4/4/2011 5:59 PM:
I think that it is better to improve my library that is there to manage with any conversion, and let lexical cast do what it did originally.
Let me know what do you think.
It didn't sound like he change the behavior of lexical_cast<>(), at all. I am very much in favor of these optimizations going in, since I have a large amount of code which uses lexical_cast<>(). I really don't see what this has to do with your library. IMO, if its existence hinges on some fairly straight-forward optimizations going into lexical_cast<>(), that should raise some serious questions. Matt

Message du 05/04/11 04:13 De : "Gruenke, Matt" A : boost@lists.boost.org Copie à : Objet : Re: [boost] [lexical cast] Performance improvements
From Vicente BOTET, on Mon 4/4/2011 5:59 PM:
I think that it is better to improve my library that is there to manage with any conversion, and let lexical cast do what it did originally.
Let me know what do you think.
It didn't sound like he change the behavior of lexical_cast<>(), at all. I am very much in favor of these optimizations going in, since I have a large amount of code which uses lexical_cast<>().
I really don't see what this has to do with your library. IMO, if its existence hinges on some fairly straight-forward optimizations going into lexical_cast<>(), that should raise some serious questions.
You are right. There is no reason to don't make these optimizations in lexical_cast if the behavior is not changed. I guess there are enough test to cover the functionality. Best, Vicente
participants (5)
-
Antony Polukhin
-
Christopher Jefferson
-
Gruenke, Matt
-
Stewart, Robert
-
Vicente BOTET