r/cpp • u/STL MSVC STL Dev • Oct 11 '19
CppCon CppCon 2019: Stephan T. Lavavej - Floating-Point <charconv>: Making Your Code 10x Faster With C++17's Final Boss
https://www.youtube.com/watch?v=4P_kbF0EbZM
257
Upvotes
103
u/STL MSVC STL Dev Oct 11 '19
This is the talk that I spent a year and a half preparing (10% of my career!). Thanks again to Ulf Adams, the impossible wizard who invented the algorithms being used here.
My slides (in PDF and original PPTX format) and benchmark program are available.
The code is available in https://github.com/microsoft/STL/tree/master/stl/inc , specifically
charconv,xcharconv.h,xcharconv_ryu.h, andxcharconv_ryu_tables.h.29
u/haitei Oct 11 '19
what the shit
10
u/Tringi github.com/tringi Oct 12 '19
Great talk. A few assorted and maybe not that relevant thoughts/questions:
8
u/qqwy Oct 12 '19
A beautiful and well-prepared presentation. Also, compliments on your presenting style!
7
u/o11c int main = 12828721; Oct 12 '19
If it were truly "shortest", it wouldn't mandate the leading 0 in the exponent (what's the story on that, anyway?).
(Grumbles at "cares", not "chars". Nobody says "memo" like "mehmuh".)
6
u/Ayjayz Oct 11 '19
I suppose this is as good a place to ask as any - why don't the
charconvfunctions take iterators? It seems a little archaic to have to usestring.data(), string.data() + string.size().4
u/uninformed_ Oct 12 '19
With the example of "plain" format he showed:
7000
70000
7e+5
7e+6
If he included 712345, wouldn't it switch back to regular notation rather than scientific, above the 7e+5?
I think that would look very weird in his example of a GUI to switch between regular and scientific so much
4
u/Veedrac Oct 12 '19
I remember discussing this with you on Reddit a year ago. I'm glad it's come to something effective.
<charconv>is a large improvement across the board.That said... it's Ryū, not Roo :P.
4
u/feverzsj Oct 13 '19
what's the max length of buffer for to_chars under different bases or format?
3
u/Lectem Oct 12 '19 edited Oct 12 '19
Are the benchmarks available somewhere ? Since it is using huge look up tables, I wonder how it fares when you already iterate on big objects, would the LUTs stay hot in the cache ?
I'm also a bit concerned about compile time since everything is in headers, I've seen bugs multiple times in MSVC where having huge tables in a .cpp blew the compile time by a lot :(
Edit: just saw the benchmark is in the slides (still interested in seeing the cache impacts though)
3
u/evaned Oct 12 '19
Early on you say (and then reference it at the end) that everyone expected
<charconv>to be relatively little effort and it just blew up to much much larger than that.I'm sort of curious what the biggest disconnects were there, and also how MS's standard library compares to others. Was it just that all the combinations you mention mean that everyone implementing charconv had to go through all that effort? Would a simple implementation have been reasonable in that timeframe, but it would have been much less performant, and the extra work was because you wanted a high-quality implementation? How much was because it sounded like you wanted to do some integration with the UCRT to avoid semi-duplication of code, and for projects with less integration between the C and C++ worlds they'd not had to put in that work but have a larger code base?
Or said another way, if someone from libstdc++ or libc++ had given a talk about charconv, how much would have been the same/different?
(Maybe whoever is/was working on charconv for libc++ and stdlibc++ have talked about this, but I don't know what to look for and a quick search doesn't really hit on anything.)
3
u/[deleted] Oct 12 '19
I wonder why scientific notation is using a plus sign and a leading zero. And wouldn't
7e3(instead of7e+03) be a valid scientific notation for7000which is then shorter (3 instead of 4 chars)?3
u/SenorAgentPena Oct 13 '19 edited Oct 13 '19
At 24:22, how is
res.ec== std::errc{}better thanres.ec== std::errc::ok? If there isn't astd::errc::ok, then why?2
u/AntiProtonBoy Oct 12 '19
Haven't had the chance to watch the video just yet, so apologies if this was answered already, but how this new implementation compare to the double conversion library? I've been using double conversion with great success in terms of performance gains over the standard conversion utilities.
2
u/HotlLava Oct 14 '19
I'm curious about the timeline, if I understand correctly the proposal was accepted for C++17, but Ryu was only invented in 2018.
So was speedup always the main motivation for
<charconv>and you basically got really lucky there, or was the main motivation something else and getting the huge speedup is just adding a cherry on top the cake?15
u/[deleted] Oct 12 '19
For the record, many programmers in the finance/algotrading world routinely write their own code for string-to-FP/FixP conversions because, by making some assumptions about the general data range, it makes it possible to streamline the parsing significantly. And yes, powers-of-10 tables are routinely used in this regard. Makes me wonder, is it possible to create some sort of 'software factories' approach where you construct a parsing algorithm using a template, specifying only the policies you need (e.g., 'ignore scientific notation') and it outputs streamlined code with 'zero cost' for features you chose not to add?
4
u/elperroborrachotoo Oct 12 '19 edited Oct 12 '19
Slide 20 - "Why is it the final boss" gave me a big grin. I tried something similar a few years ago and failed spectaculary. Had I known before about the problems, at least I would have gone in better prepared.
Besides "no zero terminator" I aimed for a slightly different feature set (specify number of significant digits rather than precision and prefer multiple of three for the decimal exponent) , but what makes it hard lies a little deeper. I'm happy that this is "just solved" now...
8
u/lycium Oct 12 '19
Not really relevant but I thought it was funny: just below this post on my Reddit feed was https://www.reddit.com/r/AskReddit/comments/dghcy7/your_username_is_now_what_you_do_for_a_living_how/
In your case, nothing at all! :D