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を呼んでいるコードを書き換えずに高速化できて、ハッピーですね!

35 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 got what you mean , a debt of gratitude is in order for posting .Woh I am cheerful to discover this site through google
    I really enjoyed reading this post, big fan. Keep up the good work andplease tell me when can you publish more articles or where can I read more on the subject?
    شركة تنظيف بالجبيل
    شركة تنظيف بالدمام
    شركة تنظيف بالخبر
    شركة تنظيف بالقطيف

    ReplyDelete
  3. 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
  4. 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
  5. 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
  6. 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.
    IPL 2017 Live Streaming
    UP 10th Result
    UP Board 10th Result 2017
    UP High School Result 2017
    NEET UG Exam Result
    CBSE Class 10th 2017 Result
    AP SSC 10th Exam results 2017
    TS EAMCET Result Date

    ReplyDelete
  7. 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
  8. This comment has been removed by the author.

    ReplyDelete
  9. 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
  10. 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
  11. 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
  12. 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
  13. Now you can earn free power ups throughout the game. downlodable torrent Requirements: Any iPhone, IPod or iPad with firmware, ISO 4.

    ReplyDelete
  14. 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
  15. Trollope had some very miserable experiences at these two public schools. http://downloadbestsoftwaresite.ru The user who provides offending content may be ejected.

    ReplyDelete
  16. Thanks for sharing this awasome news for us. Get your outdoor storage needs organized with a backyard Garden shed in wide variety of metal, plastic and wood material with any home style and garden view..if you live abroad and want to get garden shed in Newzealnd you will be also see here Garden shed Nz. For more visit my page

    ReplyDelete
  17. It is the good article and very inspires blog I hope this is very good and mostly desirable article because of your blog has
    the most information and good knowledge for essays like that.
    tailoring solutions

    ReplyDelete
  18. 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
  19. 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
  20. 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