Consider using RCPP_USE_UNWIND_PROTECT for improved performance

オープン
#244 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
cpp, r

調査の方向性

Start by inspecting the package build configuration and how Rcpp::Function calls are used in the server callback path. Compare the existing dplyr Makevars use of RCPP_USE_UNWIND_PROTECT, then run the documented wrk hello-world benchmark with and without the flag; done means a justified default build choice with no regressions in request handling.

索引モデルが issue の本文から書いたものです。

説明

I was doing some casual profiling of some httpuv servers and noticed that the call stack was deeper than I expected. You can see in the flame graph below (for the "hello, world" example) that there is a tryCatch/eval stack between later::execCallbacks() and the actual call() method (anonymous here) for the server:

httpuv-before

I eventually worked out that this tryCatch/eval setup is manually created in C++ code when evaluating an Rcpp::Function, in order to prevent longjmps. This might be well-known. However, it is also apparent from the flame graph that this particular stack seems to impose a significant amount of overhead on all requests, and I wondered if removing it could improve performance.

As it turns out, Rcpp has a not-very-well-advertised configuration option to avoid this in favour of newer C-level features of R, RCPP_USE_UNWIND_PROTECT (originally implemented by Lionel Henry, I believe). This flag is used by some existing packages, dplyr among them, so it probably has few downsides.

To illustrate the potential difference, I ran some load testing for the "hello, world" server against vanilla master:

$ wrk -d 20 -c 10 -t 2 "http://127.0.0.1:5000/"
Running 20s test @ http://127.0.0.1:5000/
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     2.55ms    1.57ms  41.95ms   96.25%
    Req/Sec     2.03k   139.92     2.39k    75.25%
  80786 requests in 20.01s, 7.70MB read
Requests/sec:   4038.18
Transfer/sec:    394.35KB

And with PKG_CPPFLAGS += -DRCPP_USE_UNWIND_PROTECT:

$ wrk -d 20 -c 10 -t 2 "http://127.0.0.1:5000/"
Running 20s test @ http://127.0.0.1:5000/
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.76ms    1.36ms  39.96ms   97.78%
    Req/Sec     2.98k   288.29     6.39k    77.56%
  118950 requests in 20.10s, 11.34MB read
Requests/sec:   5917.94
Transfer/sec:    577.92KB

As you can see, throughput is about 50% higher. The accompanying flame graph, showing the simplified stack, is below:

httpuv-after

In light of this, I believe that you should consider building httpuv with RCPP_USE_UNWIND_PROTECT by default, if possible.

主要言語
C
スター
252
フォーク
87
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

rstudio/httpuv のほかの issue

rstudio/httpuv の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。