Thursday, October 2, 2014

sprintf を最大10倍以上高速化するプリプロセッサ「qrintf」を作った

最近H2OというHTTPサーバを書いているのですが、プロファイルを取ってみるとsprintfが結構な時間を食っていて不満に感じていました。実際、sprintfは数値や文字列をフォーマットするのに十徳ナイフ的に便利なので、HTTPサーバに限らず良く使われる(そしてCPU時間を消費しがちな)関数です。

では、sprintfを最適化すれば、様々なプログラムが より高速に動作するようになるのではないでしょうか。ということで作ったのが、qrintfです。

qrintfは、Cプリプロセッサのラッパーとしてソースコードに含まれるsprintfの呼出フォーマットを解析し、フォーマットにあわせたコードに書き換えることで、sprintfを高速化します。

たとえば、以下のようなIPv4アドレスを文字列化するコード片を
sprintf(
    buf,
    "%d.%d.%d.%d",
    (addr >> 24) & 0xff,
    (addr >> 16) & 0xff,
    (addr >> 8) & 0xff,
    addr & 0xff);
以下のようにコンパイル時に自動的に書き換え、実行時にフォーマット文字列の解析する負荷を省くことで大幅な高速化を実現しています。
((int)(_qrintf_d(
    _qrintf_c(
        _qrintf_d(
            _qrintf_c(
                _qrintf_d(
                    _qrintf_c(
                        _qrintf_d(
                            _qrintf_init(buf),
                            (addr >> 24) & 0xff),
                        '.'),
                    (addr >> 16) & 0xff),
                '.'),
            (addr >> 8) & 0xff),
        '.'),
    addr & 0xff)
).off);

たとえば上記のコードの場合、以下のように、qrintfを用いることで10倍以上文字列化処理が高速化することを確認しています。
$ gcc -O2 examples/ipv4addr.c
$ time ./a.out 1234567890
result: 73.150.2.210

real    0m2.602s
user    0m2.598s
sys 0m0.003s
$ ./qrintf-gcc -O2 examples/ipv4addr.c
$ time ./a.out 1234567890
result: 73.150.2.210

real    0m0.196s
user    0m0.192s
sys 0m0.003s

sprintfを呼んでいるコードを書き換えずに高速化できて、ハッピーですね!

49 comments:

  1. I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful
    Its my great pleasure to visit your blog and to enjoy your great posts here. I like it a lot. I can feel that you paid much attention for those articles, as all of them make sense and are very useful
    شركة مكافحة الحشرات بالقطيف
    شركة مكافحة النمل الابيض بالقطيف
    شركة مكافحة الحشرات بالدمام
    شركة مكافحة الحشرات بالخبر
    شركة مكافحة البق بالقطيف
    شركة مكافحة البق بالدمام
    شركة مكافحة النمل الابيض بالدمام
    شركة مكافحة النمل الابيض بالخبر
    هتم شركة مكافحة الحشرات بالدمام باعمال المكافحة للصراصير بالدمام فالصراصير من اكثر انواع الحشرات التى تسبب الضيق والالم وتسبب الشعور بالاشمئزاز والقرف فى المكان فاذا كنت تعانى من وجود الصراصير وتعانى من وجودها وانتشارها فى اى مكان فى منزلك وبالاخص فى المطابخ والصراصير الصغيره المتواجدة فى المكان

    ReplyDelete
  2. I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post
    شركة تنظيف بالخبر
    شركة تنظيف بالجبيل
    شركة تنظيف بالدمام
    شركة تنظيف بالقطيف

    ReplyDelete
  3. Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
    شركة نقل اثاث بالقطيف
    نقل اثاث بالدمام رخيص
    ارخص شركة نقل اثاث بالدمام
    شركة نقل عفش بالدمام
    شركة نقل اثاث بالخبر
    شركة نقل اثاث بالدمام

    ReplyDelete
  4. Nice to read this article will be very helpful in the future, share more info with us. Good job!
    agarwal packers and movers jaipur
    go kart frames

    ReplyDelete
  5. Add as many shows as possible, so you will never be missing your favorite TV shows. download driver It involves looking at specific actions and places those actions in context of the character's motives and intent.

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

    ReplyDelete
  7. A Nice written beautiful article, it shows how deep knowledge you have about the topic. Thanks a lot for sharing with us.
    hot tv actress

    ReplyDelete
  8. Free to play until your character reaches level 15. This is what you want As the case progresses you'll be given more and more bizarre choices, including some pretty dumb ways to die.

    ReplyDelete
  9. Toggle Protection Restrict access of Wi-Fi, Bluetooth and Mobile Data of your phone. download files A wolf got on to them and watched their move shortly after they arrived in the forest.

    ReplyDelete
  10. Use the built-in capabilities of your HTML5 web browser for such things as spell checking, print, print preview, and find functions. Go to website. From Pixineers: Your dream figure, at your fingertipsThe prospect of plastic surgery is both exciting and daunting.

    ReplyDelete
  11. Now you can earn free power ups throughout the game. downlodable torrent Requirements: Any iPhone, IPod or iPad with firmware, ISO 4.

    ReplyDelete
  12. A2: You may receive the confirmation notification right away or it can be delayed. downlodable software Our system provides specific entry prices, specific profit targets and specific exit points to the penny.

    ReplyDelete
  13. Trollope had some very miserable experiences at these two public schools. http://downloadbestsoftwaresite.ru The user who provides offending content may be ejected.

    ReplyDelete
  14. Give the free version a try, and if you like it, you can upgrade from inside the app. descargar script mata virus amvo Babies like to focus on things that have high contrast because they are easier for them to see.

    ReplyDelete
  15. Supported File Format Extension XVID/DIVX AVI, MP4, VOB, ASF, 3GP, FLV, WMV, RMVB, MPEG, H264, MKV, TS, M2TS. This is what you want There are different missions and requirements in the story stages, such as destroying the enemies, protecting the VIP, decapitation strike and so on.

    ReplyDelete
  16. It will accelerate your discovery of new chords in leaps and bounds and raise your level of playing to new heights. http://gooddownloadtorrentdatabase.us Reserve a taxi at a fixed price throughout Sweden with one app.

    ReplyDelete
  17. The Ultimate Sikhism Library - (A Unique Collection of 3 sacred books of the Sikhs) for iPhone. downloadornot.top "Stunt Driver is a great concept, and the best part is that the graphics and controls bring that concept to life.

    ReplyDelete
  18. AFTER EACH MOVE, EVERY SHARED LETTER ON THE TRAY WILL INCREMENT BY ONE POINT. http://downloadallthingsfast.me Simple, easy to use navigation for quick reference.

    ReplyDelete
  19. From Pilvy: CloudPlay is a lightweight music player that lets you search and make playlists out of free music on the web. Welcome to my site. Next, choose the length of time you&'d like for it to be and choose the music you&'d like to use.

    ReplyDelete
  20. You can access the tests at anytime and anywhere with your device. downloadfreethingshere.gdn Translations are limited to 10/day, unlimited translations upgrade available via in-app purchase for only 0.

    ReplyDelete
  21. So if your willing to risk losing your money too Have fun. downloadfilesfromaaliyah.gdn I had planned on using it to monitor my spending from different accounts and cash.

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

    ReplyDelete
  23. Nervousness and excitement are the two emotions apart from the others that will dominate your mind on the day of BSEB 10th Result 2019.

    ReplyDelete
  24. This is a Really Good Article. Always I read your blog. Thanks for sharing good information.
    Check more:
    SSC Marksheet Download 2019.
    SSC Dakhil Result 2019

    ReplyDelete
  25. Very Helpful Post. Keep it up. I l also share similar post here- education-board-result.com/ssc-result-2019/

    ReplyDelete
  26. Honours 2nd year result 2019 is the hottest topic in Bangladesh right now. People from here and there are searching for the result. If you are one of them, then cool down! You come to the right place. Because from here, you can easily collect your Honours 2nd year result 2019. Read this article carefully to learn how to download your result. There are many ways for collecting Honours 4th year result 2019. Degree third year examination result is out now. NU Degree 1st Year Result 2018 will be announced by National University. One can easily download Degree 1st year result 2019. We have gathered some of very popular methods of downloading result.

    ReplyDelete

  27. walton olvio mm17 Walton Company Provide Expected Price of this Phone. The Expected Price of Olvio MM17 Price in Bangladesh is 999 TK. Buy this smartphone Lokal Market and this phone available May 2019.

    ReplyDelete
  28. HSC Exam Result 2019
    Today we will discuss about hsc result 2019. Do you know how to check hsc result 2019 easily? If you don’t know then read this comment. hsc exam result 2019 is going to publish on the next month. hsc result 2019 marksheet for Bangladeshi Students are going to publish. Bangladesh Education Board especially Dhaka Education Board will be announced and published first the Secondary School Certificate examination result or equivalent exam result for the year 2019. Education Board already announced and published hsc result 2019 dhaka board. This post helps you downloading xi class admission result 2019 . We also provide you degre 1st year exam result 2019. There is a great opportunity for you to get honours 2nd year result 2019.
    There we also provide bangle eid sms and bangla sad sms

    ReplyDelete
  29. HSC Result 2019. educationboradresults.gov.bd. So, HSC All Education

    Board Result 2019. HSC Markshit 2019. eboardresults.com. Alim Markshit

    2019, HSC Marksit 2019,HSC Result Markshit. Alim Marksit 2019. HSC

    Result Marksit. Alim Result Markshit, Alim Result 2019

    Marksit.https://jagobahe.com/hsc-result-2019

    HSC Result 2019

    ReplyDelete
  30. https://bdjobstotal.com/hsc-result-2019

    ReplyDelete
  31. HSC Result 2019 Published Date in Bangladesh
    HSC Result 2019 has been published by Education Board Bangladesh. There were numerous concepts and comments around about the HSC Exam Result. Many thought that the result would be published on July 22, 2019. Learn HSC Result 2019 published date. This year HSC Exam starts on April 01, 2019 and exam ends on May 14, 2019. In this article, we are going to discuss about how to download/check/get HSC Exam Result 2019.
    HSC Result 2019 will be published on July 21, 2019. This date is confirmed by several high authentic source from the education ministry of Bangladesh. After the publication of the result, you can get HSC Result 2019 Marksheet from this link.

    ReplyDelete
  32. I have read your blog it is very helpful for me.
    hsc result 2019

    ReplyDelete

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