惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed
Security Latest
Security Latest
P
Privacy International News Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
AI
AI
Cisco Talos Blog
Cisco Talos Blog
K
Kaspersky official blog
S
Secure Thoughts
PCI Perspectives
PCI Perspectives
Simon Willison's Weblog
Simon Willison's Weblog
D
DataBreaches.Net
GbyAI
GbyAI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
罗磊的独立博客
V
Visual Studio Blog
aimingoo的专栏
aimingoo的专栏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
V
V2EX
Last Week in AI
Last Week in AI
有赞技术团队
有赞技术团队
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tenable Blog
T
Threat Research - Cisco Blogs
T
Troy Hunt's Blog
V2EX - 技术
V2EX - 技术
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
L
Lohrmann on Cybersecurity
F
Full Disclosure
H
Help Net Security
博客园 - Franky
Stack Overflow Blog
Stack Overflow Blog
N
Netflix TechBlog - Medium
Engineering at Meta
Engineering at Meta
A
Arctic Wolf
O
OpenAI News
S
Securelist

Ruby | 酷 壳 - CoolShell

数据即代码:元驱动编程 | 酷 壳 - CoolShell API设计:用流畅接口构造内部DSL | 酷 壳 - CoolShell 对象的消息模型 | 酷 壳 - CoolShell StackOverflow的404错误页 | 酷 壳 - CoolShell 程序员眼中的编程语言 | 酷 壳 - CoolShell 编程语言汽车 | 酷 壳 - CoolShell 到处都是Unix的胎记 | 酷 壳 - CoolShell 免费电子书:Ruby Complete | 酷 壳 - CoolShell 2009年脚本语言排名 | 酷 壳 - CoolShell
让Ruby增加30%的性能改进 | 酷 壳 - CoolShell
陈皓 · 2009-05-05 · via Ruby | 酷 壳 - CoolShell

一切都和 --enable-pthread 有关

问一下 Ruby 黑客怎么简单地增加一个线程的Ruby应用程序的性能。也许,这些黑客会告诉你,“小伙,每个人都知道在编译Ruby的时候你需要使用configure 的 --disable-pthread参数”。没错,在configure --disable-pthread 可以让你得到大约 30% 性能提高。但是,这是为什么呢?

所有的这一些我们需要使用 strace 工具,这个工具可以打出所有的真实的操作系统的调用。

下面,是一段我们测试的例程:

def make_thread
  Thread.new {
    a = []
    10_000_000.times {
      a << "a"
      a.pop
    }
  }
end

t = make_thread
t1 = make_thread

t.join
t1.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,180sigprocmask系统调用。但是,如果你是在--disable-pthread 的Ruby版本下运行,你会发现根本没有那么多的sigprocmask 系统调用(只有 3 次,简直就是天壤之别

查看一下源代码

我们知道 configure 是一个脚本,其主要用来创建一个 config.h 文件,其中有一大堆宏定义 defines ,这些宏定义决定了使用什么样的函数。所以,让我们来比较一下版本 ./configure --enable-pthread 和版本./configure --disable-pthread的不同之处吧。

$ diff config.h config.h.pthread
> #define _REENTRANT 1
> #define _THREAD_SAFE 1
> #define HAVE_LIBPTHREAD 1
> #define HAVE_NANOSLEEP 1
> #define HAVE_GETCONTEXT 1
> #define HAVE_SETCONTEXT 1

好的,现在我们再 grep 一下Ruby的源代码,我们可以看到只要HAVE_[S/G]ETCONTEXT 被设置了,Ruby 就会调用setcontext()getcontext() 这两个系统调用来存取context 的状态,以便异常处理时的切换(通过EXEC_TAG)。

而如果 HAVE_[S/G]ETCONTEXT 没有被定义 的情况下,Ruby 会使用 _setjmp/_longjmp这两个系统调用。

我们来看看 _setjmp/_longjmp 的man page:

… The _longjmp() and _setjmp() functions shall be equivalent to longjmp() and setjmp(), respectively, with the additional restriction that _longjmp() and _setjmp() shall not manipulate the signal mask…

还有setcontext /getcontext的 man page:

… uc_sigmask is the set of signals blocked in this context (see sigprocmask(2)) …

我们可以看到 getcontext 调用每次都要调用sigprocmask 但是_setjmp 不会。

补丁

请点击 这里获取补丁

这个补丁增加了一个configure 的参数 --disable-ucontext 其可以让你关闭使用 setcontext或getcontext,你只需要像如下方式使用就好了。

./configure --disable-ucontext --enable-pthread

如果你以这种方式编译Ruby,那么,你的程序的性能在同等条件下可能会有30%左右的提升。

文章:来源

Loading...