Tuesday, October 7, 2014

Making the HTTP server run 20% faster by using qrintf-gcc; a sprintf-optimizing preprocessor / compiler

tl;dr

This is an initial release announcement of qrintf, a preprocessor (and qrintf-gcc is the compiler frontend) that optimizes calls to sprintf and snprintf. C programs calling the functions may run faster by using the preprocessor / compiler in place of the standard compiler toolchain.

github.com/kazuho/qrintf

Background

sprintf (snprintf) is a great function for converting data to strings. The downside is that it is slow. Recently the functions have been one of the bottlenecks of H2O, which is a high performance HTTP server / library with support for HTTP/1.x and HTTP/2.

The function is slow not because stringification in general is a slow operation. It is slow mainly due to its design.

The function takes a format string (e.g. "my name is %s") and parse it at runtime, every time the function gets called. The design must have been a good choice when the C programming language was designed. Until the 1990s, every byte of memory was a precious resource. Using a memory-conscious approach (e.g. a state-machine for formatting strings) was a logical choice. But nowadays, the cost of memory is hundreds-of-thousand times cheaper than it used to be. On the other hand, the relative cost of running a state machine for building formatted strings has become large since it causes lots of branch mis-predictions that stall the CPU pipeline.

How it works

The understanding has led me to write qrintf, which works as a wrapper against the C preprocessor. It precompiles invocations of sprintf (and snprintf) with a constant format string (which is mostly the case) into optimized forms.

The example below illustrates the conversion performed by the qrintf preprocessor. As can be seen, the call to sprintf is precomplied into a series of function calls specialized for each portion of the format (and the C compiler may inline-expand the specialized calls). In the case of the example, the optimized code runs more than 10 times faster than the original form.
// source code
snprintf(
    buf,
    sizeof(buf),
    "%u.%u.%u.%u",
    sizeof(buf),
    (addr >> 24) & 0xff,
    (addr >> 16) & 0xff,
    (addr >> 8) & 0xff,
    addr & 0xff);

// after preprocessed by qrintf
_qrintf_chk_finalize(
    _qrintf_chk_u(
        _qrintf_chk_c(
            _qrintf_chk_u(
                _qrintf_chk_c(
                    _qrintf_chk_u(
                        _qrintf_chk_c(
                            _qrintf_chk_u(
                                _qrintf_chk_init(buf, sizeof(buf)),
                                (addr >> 24) & 0xff),
                            '.'),
                        (addr >> 16) & 0xff),
                    '.'),
                (addr >> 8) & 0xff),
            '.'),
        addr & 0xff));
$ gcc -Wall -O2 examples/ipv4addr.c && time ./a.out 1234567890
result: 73.150.2.210

real 0m2.475s
user 0m2.470s
sys 0m0.002s
$ qrintf-gcc -Wall -O2 examples/ipv4addr.c && time ./a.out 1234567890
result: 73.150.2.210

real 0m0.215s
user 0m0.211s
sys 0m0.002s

Performance impact on H2O

H2O uses sprintf in three parts: building the HTTP status line, building ETag, and building the access log.

By using qrintf-gcc in place of gcc, the performance of H2O jumps up by 20% in a benchmark that sends tiny files and with access logging enabled (from 82,900 reqs/sec. to 99,200 reqs/sec.).

The fact not only makes my happy (as the developer of qrintf and H2O) but also shows that optimizing sprintf at compile-time do have benefit for real world programs.

So if you are interested in using qrintf or H2O, please give it a try. Thank you for reading.

89 comments:

  1. Wow, looks great! Any plans to include your work in official GCC line?

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Credits that are offered with no pay evidence are known as no salary verification secured advances. Since these advances don't request any wage explanation to bolster the reimbursement ability of the borrower, guarantee is basic. cash advance

    ReplyDelete

  4. Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write.
    Thanks for sharing !
    tanki online 2 | 2048 game online

    ReplyDelete
  5. I am wondering, is this related to sprintf family only or also for printf family? Because I saw some pretty amazing compile-time optimization there.

    ReplyDelete
  6. yes, seems printf is optimized:

    sprintf version:

    time ./a.out 1234567890
    result: 73.150.2.210

    real 0m2.557s
    user 0m2.556s
    sys 0m0.001s

    printf version:

    time ./a.out 1234567890 > /dev/null

    real 0m0.305s
    user 0m0.302s
    sys 0m0.003s

    ReplyDelete
  7. Commonly we attain confused when we finally hear the exact terms website development service in addition to internet developer getting used interchangeably. But thoughts is broken done looking over this, all your individual confusions may perhaps disappear. Poof!
    driving directions

    ReplyDelete
  8. It's been a pleasure for me to read such an informative article. I love reading good content and It's one of them. Moreover, your blog is loading really fast. Can I know which hosting platform are you using?
    New Year GIF Video

    ReplyDelete
  9. Wmtorrents
    Get serial keys and generators to activate and crack your softwares including Keygens. By mode of softwares you can activate any softwares
    https://wmtorrents.com/

    ReplyDelete
  10. Hey Thanks for sharing this valuable information with us. I will come back to your site and keep sharing this information with us.
    Best Regards - www.office.com/setup
    www.office.com/setup
    www.office.com/setup

    ReplyDelete
  11. Nice thoughts with great helping content. I have also been seeking such thoughfull content to learn and appy in the life. We have also created such type of content on our site. You can refer out site for more ideas.
    Happy New Year 2019 Wishes Messages Sms in Bengali
    Merry Christmas 2018 SMS, Wishes, Messages, Greetings in Bengali
    Merry Christmas 2018 Wishes Messages in Marathi,funny christmas text sms

    ReplyDelete
  12. Webroot.com/safe is a protection software solution that communicates with the cloud avoiding the hassle to manage the signature updates to deploy. for office setup visit office.com/setup

    ReplyDelete
  13. Nice thoughts with great helping content. I have also been seeking such thoughfull content to learn and appy in the life.

    pip camera new version 2019

    ReplyDelete
  14. Thanks for sharing nice information with us. i like your post and all you share with us is uptodate and quite informative, i would like to bookmark the page so i can come here again to read you, as you have done a wonderful job. I found your blog using msn. This is an extremely well written article as www.jnews.in . I will be sure to bookmark it and return to read more of your useful information. Thanks for the post. I’ll certainly comeback.

    ReplyDelete
  15. I was impressed, I have to say. In fact, I rarely Encounter educational and funny blogs. You may strike a nail on your head. Your idea is wonderful. The problem is one of which is not speaking intellectually. I am pleased that 360result.inI am looking for one thing.

    ReplyDelete
  16. This article is actually a nice one it helps new internet viewers, who are wishing in favor of blogging. For more information click here latest news and I absolutely love your blog and find almost all of your post’s to be just what I’m looking for. Does one offer guest writers to write content to suit your needs? click here

    ReplyDelete
  17. Spot on with this write-up, I actually think this website needs far more attention. I’ll probably be back again to read more, thanks for the info!

    Happy Valentines Day Images 2019
    Happy Valentines Day Pictures Free Download
    Happy Valentines Day Clipart Free Download
    Happy Valentines Day Poems
    Happy Valentines Day Messages

    ReplyDelete
  18. I loved the article, keep updating interesting articles. I will be a regular reader… I am offering assignment help to students over the globe at a low price.

    https://www.studychat.in

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
  20. This is certainly additionally an exceedingly wonderful offer everyone truly qualified on the lookout with. It's always not likely everyday there is chances read a little something. extremely well written article https://apsscresult2019.in . I will be sure to and return to read more of your useful information. Thanks for the post. I’ll certainly comeback

    ReplyDelete
  21. Thanks for sharing nice information with us. i like your post and all you share with us is uptodate and quite informative, i would like to bookmark the page so i can come here again to read you, as you have done a wonderful job. I found your blog using msn. This is an extremely well written article as https://www.studyplans.in . I will be sure to bookmark it and return to read more of your useful information. Thanks for the post. I’ll certainly comeback.

    ReplyDelete
  22. Hi,
    Your article is very informative and has a lot of information. I really like your effort, keep posting.
    pip camera photo editor 2019

    ReplyDelete
  23. A very awesome blog post. We are really grateful for your blog post. You will find a lot of
    approaches after visiting your post.
    rrbgroupdresult-2019.in

    ReplyDelete
  24. Thanks for sharing nice information with us. i like your post and all you share with us is uptodate and quite informative, i would like to bookmark the page so i can come here again to read you, as you have done a wonderful job. I found your blog using msn. This is an extremely well written article as studyplans.in . I will be sure to bookmark it and return to read more of your useful information. Thanks for the post. I’ll certainly comeback

    ReplyDelete
  25. I needed to thank you for this Incredible read !! apsscresult2019.in I unquestionably adored each and every line of it.

    ReplyDelete
  26. Hi to everybody, here everyone is sharing such sarkari result
    knowledge, so it’s fastidious to see this site, jobnotifys.inand I used to visit this blog daily

    ReplyDelete
  27. Although the news is not confirmed by the UP Board Officials, academic experts who have been tracking the upmsp.edu.in 12th result 2019 over the years suggest that results will be declared on time to help students meet the timelines for college admissions.

    ReplyDelete
  28. Bihar Board 12th Result 2019 Announced for Arts, Science and Commerce Group BSEB Intermediate Result 2019 in School wise, biharboard.ac.in Student wise along District wise Merit Toppers lists from Bihar School Examination Board.

    Navodaya Result 2019 with selected list will be announced for 5th to 6th and 7th, 8th,9th, 10th and 12th grade vacant seat admissions JNVST Result 2019 in Jawahar Navodaya Vidyalaya, Download Navodaya 6th Result 2019 with JNV Selected list 2019 Pdf in District wise with waiting listed candidate details.

    The Navodaya Vidhyalaya Samithi is going to announced the JNVST 6th Class Result 2019 Navodaya result 2019 with Selected Candidate list for 5th to 6th
    class admission selection test and the NVS officials are ready to announce JNVST 9th Class Result 2019 with school wise selected candidate list for the Lateral Entry test.

    Kar 2nd PUC Result 2019 check Student wise Karnataka PUC Exam Result 2019 in college wise pue.kar.nic.in and Student wise online and SMS based service for regular and private students with school wise merit and toppers lists

    Navodaya 6th Class Admission Selection Test Result 2019 Download in School Wise with Selected Candidate list
    JNVST Result 2019 along Division wise Merit lists, Download JNVST Result 2019 or Navodaya Result 2019 for JNV Selection list for Jawahar Navodaya Vidyalaya Admission Selection test 2019-20.

    ReplyDelete
  29. I needed to thank you for this Incredible read !!
    10th Model Paper 2020

    ReplyDelete
  30. Bihar Board 12th Result 2019 Announced for Arts, Science and Commerce Group BSEB Intermediate Result 2019 in School wise, Bihar Intermediate Result 2019 Student wise along District wise Merit Toppers lists from Bihar School Examination Board.

    Navodaya Result 2019 with selected list will be announced for 5th to 6th and 7th, 8th,9th, 10th and 12th grade vacant seat admissions JNVST 6th Result 2019 in Jawahar Navodaya Vidyalaya, Download Navodaya 6th Result 2019 with JNV Selected list 2019 Pdf in District wise with waiting listed candidate details.

    The Navodaya Vidhyalaya Samithi is going to announced the JNVST 6th Class Result 2019 navodaya 6th result 2019 with Selected Candidate list for 5th to 6th class admission selection test and the NVS officials are ready to announce JNVST 9th Class Result 2019 with school wise selected candidate list for the Lateral Entry test.

    Kar 2nd PUC Result 2019 check Student wise Karnataka PUC Exam Result 2019 in college wise karresults.nic.in and Student wise online and SMS based service for regular and private students with school wise merit and toppers lists.

    Navodaya 6th Class Admission Selection Test Result 2019 Download in School Wise with Selected Candidate listJNVST Selection List 2019 along Division wise Merit lists, Download JNVST Result 2019 or Navodaya Result 2019 for JNV Selection list for Jawahar Navodaya Vidyalaya Admission Selection test 2019-20.



    ReplyDelete
  31. I really liked what you have said in this post. The best thing about this post is ,It is going to be an example for most of the people.Great job keep doing such amazing work. Regards:-
    office.com/setup | norton.com/setup | mcafee.com/activate

    ReplyDelete
  32. Kia Jam graduated from the University of California Irvine with a degree in Political Science.

    ReplyDelete
  33. Thanks for sharing such a wonderful information on this website.Model Paper

    ReplyDelete
  34. Maha 12th HSC Model Paper 2020Maha 12th HSC Model Questions Paper 2020 Maha HSC Model Papers 2020 12th Class Model Paper For HSC Maharashtra Board 2020 HSC Board Paper Pattern Science 2020 Pdf HSC Paper Pattern 2020

    ReplyDelete
  35. I love this post.

    โปรโมชั่นGclub ของทางทีมงานตอนนี้แจกฟรีโบนัส 50%
    เพียงแค่คุณสมัคร Gclub กับทางทีมงานของเราเพียงเท่านั้น
    ร่วมมาเป็นส่วนหนึ่งกับเว็บไซต์คาสิโนออนไลน์ของเราได้เลยค่ะ
    สมัครสมาชิกที่นี่ >>> Gclub online

    ReplyDelete
  36. Very cool!

    เว็บไซต์คาสิโนออนไลน์ที่ได้คุณภาพอับดับ 1 ของประเทศ
    เป็นเว็บไซต์การพนันออนไลน์ที่มีคนมา สมัคร Gclub Royal1688
    และยังมีเกมส์สล็อตออนไลน์ 1688 slot อีกมากมายให้คุณได้ลอง
    สมัครสมาชิกที่นี่ >>> Gclub Royal1688

    ReplyDelete
  37. I've got browse your journal superb info produce good information article,
    10th Model paper 2020

    ReplyDelete
  38. Fourth world Telugu Mahasabhalu Details, schedule and Topics had been uploaded and the pre-matric scholarship for minority students data of the YSR Kadapa District had been uploaded at banglarbhumi.gov.in khatian no web portal

    ReplyDelete
  39. Check out the latest board exam up result 2019 for high school and intermediate.

    ReplyDelete
  40. I needed to thank you for this Incredible read !!
    model papers

    ReplyDelete
  41. Transfer councelling clear vacancies in Hyderabad district is available on this website for the Government employees belongs to educational departments at iwu.epfindia.gov.in

    ReplyDelete
  42. To get degree from the IIT’s or for the admission they have to appear for the IIT JAM exam is must using epf balance without uan number

    ReplyDelete
  43. Kia is currently in Post Production on The Misfits, directed by Renny Harlin and starring Pierce Brosnan, Tim Roth and Nick Cannon.

    ReplyDelete
  44. Thanks nice post
    Shopping Offers, Free Recharge Tricks, Loot Offers, Coupon Codes, Amazon Offers, Recharge Offers.
    Free Paytm Cash>> Free Recharge Tricks
    Tech News, Tips and Trick, Nagaland Lottery Result, Nagaland State Lotteries Result
    Lottery Sambad Result
    Lottery Sambad, Kerala, West Bengal Lottery Result.Lottery News Daily
    Nagaland Lottery Sambad
    Nagaland Lottery Result, Nagaland State Lotteries Result
    Nagaland Lottery Result
    For Cheap web hosting click Cheapest web hosting
    For Entertainment visitVioletfeed

    ReplyDelete

  45. great information share to me very useful to all educational students.JAC Board Model Paper 2020

    ReplyDelete
  46. Thanks for sharing such a wonderful information on this website.
    SEBA HSLC Blueprint

    ReplyDelete

Note: Only a member of this blog may post a comment.