我們都知道在編譯Ruby的時候你需要使用configure的 --disable-pthread參數。沒錯,在configure --disable-pthread 可以讓你得到大約 30% 效能提高。但是,這是為什麼呢?
所有的這一些我們需要使用 strace 工具,這個工具可以打出所有的真實的作業系統的調用。
下面,是一段我們測試的常式:
def make_thread Thread.new { a = [] 10_000_000.times { a << "a" a.pop } }endt = make_threadt1 = make_threadt.joint1.join
如果我們使用 strace 工具去測試 configure --enable-pthread 版本的Ruby引擎,那麼我們可以得 到下面這樣的結果:
22:46:16.706136 rt_sigprocmask(SIG_BLOCK, NULL, [], 8 ) = 0 <0.000004>22:46:16.706177 rt_sigprocmask(SIG_BLOCK, NULL, [], 8 ) = 0 <0.000004>22:46:16.706218 rt_sigprocmask(SIG_BLOCK, NULL, [], 8 ) = 0 <0.000004>22:46:16.706259 rt_sigprocmask(SIG_BLOCK, NULL, [], 8 ) = 0 <0.000005>22:46:16.706301 rt_sigprocmask(SIG_BLOCK, NULL, [], 8 ) = 0 <0.000004>22:46:16.706342 rt_sigprocmask(SIG_BLOCK, NULL, [], 8 ) = 0 <0.000004>22:46:16.706383 rt_sigprocmask(SIG_BLOCK, NULL, [], 8 ) = 0 <0.000004>
你會發現上面的sigprocmask 系統調用一頁一頁又一頁地沒完沒了的。如果你用 strace -c,你會發 現一共大約20,054,180 個sigprocmask系統調用。但是,如果你是在--disable-pthread 的Ruby版本下運 行,你會發現根本沒有那麼多的sigprocmask 系統調用(只有 3 次,簡直就是天壤之別)