Home > php教程 > php手册 > 使用 Rust 创建 PHP 扩展

使用 Rust 创建 PHP 扩展

WBOY
Release: 2016-06-13 09:06:33
Original
1959 people have browsed it

使用 Rust 创建 PHP 扩展

去年十月,我和 Etsy 的同事有过一个关于如何为像PHP样的解释性语言写拓展的讨论,Ruby或Python目前的状况应该会比PHP容易。我们谈到了写一个成功创建扩展的障碍是它们通常需要用C来写,但是如果你不擅长C这门语言的话很难有那个信心。

从那时起我便萌生了用Rust写一个的想法,过去的几天一直在尝试。今天上午我终于让它运行了。
C或PHP中的Rust

我的基本出发点就是写一些可以编译的Rust代码到一个库里面,并写为它一些C的头文件,在C中为被调用的PHP做一个拓展。虽然并不是很简单,但是很有趣。
Rust FFIforeign function interface)

我所做的第一件事情就是摆弄Rust与C连接的Rust的外部函数接口。我曾用简单的方法hello_from_rust)写过一个灵活的库,伴有单一的声明a pointer to a C char, otherwise known as a string),如下是输入后输出的“Hello from Rust”。

<ol class="dp-c"><li class="alt"><span><span class="comment">// hello_from_rust.rs</span><span> </span></span></li><li><span>#![crate_type = <span class="string">"staticlib"</span><span>] </span></span></li><li class="alt"><span> </span></li><li><span>#![feature(libc)] </span></li><li class="alt"><span>extern crate libc; </span></li><li><span><span class="keyword">use</span><span> std::ffi::CStr; </span></span></li><li class="alt"><span> </span></li><li><span>#[no_mangle] </span></li><li class="alt"><span>pub extern <span class="string">"C"</span><span> fn hello_from_rust(name: *</span><span class="keyword">const</span><span> libc::c_char) { </span></span></li><li><span>let buf_name = unsafe { CStr::from_ptr(name).to_bytes() }; </span></li><li class="alt"><span>let str_name = String::from_utf8(buf_name.to_vec()).unwrap(); </span></li><li><span>let c_name = format!(<span class="string">"Hello from Rust, {}"</span><span>, str_name); </span></span></li><li class="alt"><span>println!(<span class="string">"{}"</span><span>, c_name); </span></span></li><li><span>} </span></li></ol>
Copy after login

我从C或其它!)中调用的Rust库拆分它。这有一个接下来会怎样的很好的解释。

编译它会得到.a的一个文件,libhello_from_rust.a。这是一个静态的库,包含它自己所有的依赖关系,而且我们在编译一个C程序的时候链接它,这让我们能做后续的事情。注意:在我们编译后会得到如下输出:

<ol class="dp-c"><li class="alt"><span><span>note: link against the following native artifacts when linking against this </span><span class="keyword">static</span><span> library </span></span></li><li><span>note: the order <span class="keyword">and</span><span> any duplication can be significant on some platforms, </span><span class="keyword">and</span><span> so may need to be preserved </span></span></li><li class="alt"><span>note: library: Systemnote: library: pthread </span></li><li><span>note: library: c </span></li><li class="alt"><span>note: library: m </span></li></ol>
Copy after login
Copy after login

这就是Rust编译器在我们不使用这个依赖的时候所告诉我们需要链接什么。
从C中调用Rust

既然我们有了一个库,不得不做两件事来保证它从C中可调用。首先,我们需要为它创建一个C的头文件,hello_from_rust.h。然后在我们编译的时候链接到它。

下面是头文件:

<ol class="dp-c"><li class="alt"><span><span>note: link against the following native artifacts when linking against this </span><span class="keyword">static</span><span> library </span></span></li><li><span>note: the order <span class="keyword">and</span><span> any duplication can be significant on some platforms, </span><span class="keyword">and</span><span> so may need to be preserved </span></span></li><li class="alt"><span>note: library: Systemnote: library: pthread </span></li><li><span>note: library: c </span></li><li class="alt"><span>note: library: m </span></li></ol>
Copy after login
Copy after login

这是一个相当基础的头文件,仅仅为了一个简单的函数提供签名/定义。接着我们需要写一个C程序并使用它。

<ol class="dp-c"><li class="alt"><span><span class="comment">// hello.c</span><span> </span></span></li><li><span>#<span class="keyword">include</span><span> <stdio.h> </span></span></li><li class="alt"><span>#<span class="keyword">include</span><span> <stdlib.h> </span></span></li><li><span>#<span class="keyword">include</span><span> </span><span class="string">"hello_from_rust.h"</span><span> </span></span></li><li class="alt"><span> </span></li><li><span>int main(int argc, char *argv[]) { </span></li><li class="alt"><span>hello_from_rust(<span class="string">"Jared!"</span><span>); </span></span></li><li><span>} </span></li></ol>
Copy after login

我们通过运行一下代码来编译它:

gcc -Wall -o hello_c hello.c -L /Users/jmcfarland/code/rust/php-hello-rust -lhello_from_rust -lSystem -lpthread -lc -lm

注意在末尾的-lSystem -lpthread -lc -lm告诉gcc不要链接那些“本地的古董”,为了当编译我们的Rust库时Rust编译器可以提供出来。

经运行下面的代码我们可以得到一个二进制的文件:

<ol class="dp-c"><li class="alt"><span><span>$ ./hello_c </span></span></li><li><span>Hello from Rust, Jared! </span></li></ol>
Copy after login

漂亮!我们刚才从C中调用了Rust库。现在我们需要理解Rust库是如何进入一个PHP扩展的。

从 php 中调用 c

该部分花了我一些时间来弄明白,在这个世界上,该文档在 php 扩展中并不是最好的。最好的部分是来自绑定一个脚本 ext_skel 的 php 源大多数代表“扩展骨架”)即生成大多数你需要的样板代码。为了让代码运行,我十分努力地学习 php 文档,“扩展骨骼”。

你可以通过下载来开始,和未配额的 php 源,把代码写进 php 目录并且运行:

<ol class="dp-c"><li class="alt"><span><span>$ cd ext/ </span></span></li><li><span>$ ./ext_skel &ndash;extname=hello_from_rust </span></li></ol>
Copy after login

这将生成需要创建 php 扩展的基本骨架。现在,移动你处处想局部地保持你的扩展的文件夹。并且移动你的

.rust 源

.rust库

.c header

进入同一个目录。因此,现在你应该看看像这样的一个目录:

.
├── CREDITS
├── EXPERIMENTAL
├── config.m4
├── config.w32
├── hello_from_rust.c
├── hello_from_rust.h
├── hello_from_rust.php
├── hello_from_rust.rs
├── libhello_from_rust.a
├── php_hello_from_rust.h
└── tests
└── 001.phpt

一个目录,11个文件

你可以在 php docs 在上面看到关于这些文件很好的描述。建立一个扩展的文件。我们将通过编辑 config.m4 来开始吧。

不解释,下面就是我的成果:

<ol class="dp-c"><li class="alt"><span><span>PHP_ARG_WITH(hello_from_rust, </span><span class="keyword">for</span><span> hello_from_rust support, </span></span></li><li><span>[ --with-hello_from_rust Include hello_from_rust support]) </span></li><li class="alt"><span> </span></li><li><span><span class="keyword">if</span><span> test </span><span class="string">"$PHP_HELLO_FROM_RUST"</span><span> != </span><span class="string">"no"</span><span>; then </span></span></li><li class="alt"><span>PHP_SUBST(HELLO_FROM_RUST_SHARED_LIBADD) </span></li><li><span> </span></li><li class="alt"><span>PHP_ADD_LIBRARY_WITH_PATH(hello_from_rust, ., HELLO_FROM_RUST_SHARED_LIBADD) </span></li><li><span> </span></li><li class="alt"><span>PHP_NEW_EXTENSION(hello_from_rust, hello_from_rust.c, <span class="vars">$ext_shared</span><span>) </span></span></li><li><span>fi </span></li></ol>
Copy after login

正如我所理解的那样,这些是基本的宏命令。但是有关这些宏命令的文档是相当糟糕的比如:google”PHP_ADD_LIBRARY_WITH_PATH”并没有出现PHP团队所写的结果)。我偶然这个PHP_ADD_LIBRARY_PATH宏命令在有些人所谈论的在一个PHP拓展里链接一个静态库的先前的线程里。在评论中其它的推荐使用的宏命令是在我运行ext_skel后产生的。

既然我们进行了配置设置,我们需要从PHP脚本中实际地调用库。为此我们得修改自动生成的文件,hello_from_rust.c。首先我们添加hello_from_rust.h头文件到包含命令中。然后我们要修改confirm_hello_from_rust_compiled的定义方法。

<ol class="dp-c"><li class="alt"><span><span>#</span><span class="keyword">include</span><span> </span><span class="string">"hello_from_rust.h"</span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">// a bunch of comments and code removed...</span><span> </span></span></li><li><span> </span></li><li class="alt"><span>PHP_FUNCTION(confirm_hello_from_rust_compiled) </span></li><li><span>{ </span></li><li class="alt"><span>char *arg = NULL; </span></li><li><span>int arg_len, len; </span></li><li class="alt"><span>char *strg; </span></li><li><span> </span></li><li class="alt"><span><span class="keyword">if</span><span> (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, </span><span class="string">"s"</span><span>, &arg, &arg_len) == FAILURE) { </span></span></li><li><span><span class="keyword">return</span><span>; </span></span></li><li class="alt"><span>} </span></li><li><span> </span></li><li class="alt"><span>hello_from_rust(<span class="string">"Jared (from PHP!!)!"</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span>len = spprintf(&strg, 0, <span class="string">"Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP."</span><span>, </span><span class="string">"hello_from_rust"</span><span>, arg); </span></span></li><li><span>RETURN_STRINGL(strg, len, 0); </span></li><li class="alt"><span>} </span></li></ol>
Copy after login

注意:我添加了hello_from_rust(“Jared (fromPHP!!)!”);。

现在,我们可以试着建立我们的扩展:

<ol class="dp-c"><li class="alt"><span><span>$ phpize </span></span></li><li><span>$ ./configure </span></li><li class="alt"><span>$ sudo make install </span></li></ol>
Copy after login

就是它,生成我们的元配置,运行生成的配置命令,然后安装该扩展。安装时,我必须亲自使用sudo,因为我的用户并不拥有安装目录的 php 扩展。

现在,我们可以运行它啦!

<ol class="dp-c"><li class="alt"><span><span>$ php hello_from_rust.php </span></span></li><li><span>Functions available in the test extension: </span></li><li class="alt"><span>confirm_hello_from_rust_compiled </span></li><li><span> </span></li><li class="alt"><span>Hello from Rust, Jared (from PHP!!)! </span></li><li><span>Congratulations! You have successfully modified ext/hello_from_rust/config.m4. Module hello_from_rust is now compiled into PHP. </span></li><li class="alt"><span>Segmentation fault: 11 </span></li></ol>
Copy after login

还不错,php 已进入我们的 c 扩展,看到我们的应用方法列表并且调用。接着,c 扩展已进入我们的 rust 库,开始打印我们的字符串。那很有趣!但是……那段错误的结局发生了什么?

正如我所提到的,这里是使用了 Rust 相关的 println! 宏,但是我没有对它做进一步的调试。如果我们从我们的 Rust 库中删除并返回一个 char* 替代,段错误就会消失。

这里是 Rust 的代码:

<ol class="dp-c"><li class="alt"><span><span>#![crate_type = </span><span class="string">"staticlib"</span><span>] </span></span></li><li><span> </span></li><li class="alt"><span>#![feature(libc)] </span></li><li><span>extern crate libc; </span></li><li class="alt"><span><span class="keyword">use</span><span> std::ffi::{CStr, CString}; </span></span></li><li><span> </span></li><li class="alt"><span>#[no_mangle] </span></li><li><span>pub extern <span class="string">"C"</span><span> fn hello_from_rust(name: *</span><span class="keyword">const</span><span> libc::c_char) -> *</span><span class="keyword">const</span><span> libc::c_char { </span></span></li><li class="alt"><span>let buf_name = unsafe { CStr::from_ptr(name).to_bytes() }; </span></li><li><span>let str_name = String::from_utf8(buf_name.to_vec()).unwrap(); </span></li><li class="alt"><span>let c_name = format!(<span class="string">"Hello from Rust, {}"</span><span>, str_name); </span></span></li><li><span> </span></li><li class="alt"><span>CString::<span class="keyword">new</span><span>(c_name).unwrap().as_ptr() </span></span></li><li><span>} </span></li></ol>
Copy after login

并变更 C 头文件:

<ol class="dp-c"><li class="alt"><span><span>#ifndef __HELLO </span></span></li><li><span>#define __HELLO </span></li><li><span><span class="keyword">const</span><span> char * hello_from_rust(</span><span class="keyword">const</span><span> char *name); </span></span></li><li><span>#<span class="keyword">endif</span><span> </span></span></li></ol>
Copy after login

还要变更 C 扩展文件:

<ol class="dp-c"><li class="alt"><span><span>PHP_FUNCTION(confirm_hello_from_rust_compiled) </span></span></li><li><span>{ </span></li><li class="alt"><span>char *arg = NULL; </span></li><li><span>int arg_len, len; </span></li><li class="alt"><span>char *strg; </span></li><li><span> </span></li><li class="alt"><span><span class="keyword">if</span><span> (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, </span><span class="string">"s"</span><span>, &arg, &arg_len) == FAILURE) { </span></span></li><li><span><span class="keyword">return</span><span>; </span></span></li><li class="alt"><span>} </span></li><li><span> </span></li><li class="alt"><span>char *str; </span></li><li><span>str = hello_from_rust(<span class="string">"Jared (from PHP!!)!"</span><span>); </span></span></li><li class="alt"><span>printf(<span class="string">"%s/n"</span><span>, str); </span></span></li><li><span> </span></li><li class="alt"><span>len = spprintf(&strg, 0, <span class="string">"Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP."</span><span>, </span><span class="string">"hello_from_rust"</span><span>, arg); </span></span></li><li><span>RETURN_STRINGL(strg, len, 0); </span></li><li class="alt"><span>} </span></li></ol>
Copy after login

无用的微基准

那么为什么你还要这样做?我还真的没有在现实世界里使用过这个。但是我真的认为斐波那契序列算法就是一个好的例子来说明一个PHP拓展如何很基本。通常是直截了当在Ruby中):

<ol class="dp-c"><li class="alt"><span><span>def fib(at) </span><span class="keyword">do</span><span> </span></span></li><li><span><span class="keyword">if</span><span> (at == 1 || at == 0) </span></span></li><li class="alt"><span><span class="keyword">return</span><span> at </span></span></li><li><span><span class="keyword">else</span><span> </span></span></li><li class="alt"><span><span class="keyword">return</span><span> fib(at - 1) + fib(at - 2) </span></span></li><li><span><span class="func">end</span><span> </span></span></li><li class="alt"><span><span class="func">end</span><span> </span></span></li></ol>
Copy after login

而且可以通过不使用递归来改善这不好的性能:

<ol class="dp-c"><li class="alt"><span><span>def fib(at) </span><span class="keyword">do</span><span> </span></span></li><li><span><span class="keyword">if</span><span> (at == 1 || at == 0) </span></span></li><li class="alt"><span><span class="keyword">return</span><span> at </span></span></li><li><span>elsif (val = @cache[at]).present? </span></li><li class="alt"><span><span class="keyword">return</span><span> val </span></span></li><li><span><span class="func">end</span><span> </span></span></li><li class="alt"><span> </span></li><li><span>total = 1 </span></li><li class="alt"><span>parent = 1 </span></li><li><span>gp = 1 </span></li><li class="alt"><span> </span></li><li><span>(1..at).each <span class="keyword">do</span><span> |i| </span></span></li><li class="alt"><span>total = parent + gp </span></li><li><span>gp = parent </span></li><li class="alt"><span>parent = total </span></li><li><span><span class="func">end</span><span> </span></span></li><li class="alt"><span> </span></li><li><span><span class="keyword">return</span><span> total </span></span></li><li class="alt"><span><span class="func">end</span><span> </span></span></li></ol>
Copy after login

那么我们围绕它来写两个例子,一个在PHP中,一个在Rust中。看看哪个更快。下面是PHP版:

<ol class="dp-c"><li class="alt"><span><span>def fib(at) </span><span class="keyword">do</span><span> </span></span></li><li><span><span class="keyword">if</span><span> (at == 1 || at == 0) </span></span></li><li class="alt"><span><span class="keyword">return</span><span> at </span></span></li><li><span>elsif (val = @cache[at]).present? </span></li><li class="alt"><span><span class="keyword">return</span><span> val </span></span></li><li><span><span class="func">end</span><span> </span></span></li><li class="alt"><span> </span></li><li><span>total = 1 </span></li><li class="alt"><span>parent = 1 </span></li><li><span>gp = 1 </span></li><li class="alt"><span> </span></li><li><span>(1..at).each <span class="keyword">do</span><span> |i| </span></span></li><li class="alt"><span>total = parent + gp </span></li><li><span>gp = parent </span></li><li class="alt"><span>parent = total </span></li><li><span><span class="func">end</span><span> </span></span></li><li class="alt"><span> </span></li><li><span><span class="keyword">return</span><span> total </span></span></li><li class="alt"><span><span class="func">end</span><span> </span></span></li><li><span> </span></li><li class="alt"><span>这是它的运行结果: </span></li><li><span> </span></li><li class="alt"><span>$ time php php_fib.php </span></li><li><span> </span></li><li class="alt"><span>real 0m2.046s </span></li><li><span>user 0m1.823s </span></li><li class="alt"><span>sys 0m0.207s </span></li><li><span> </span></li><li class="alt"><span>现在我们来做Rust版。下面是库资源: </span></li><li><span> </span></li><li class="alt"><span>#![crate_type = <span class="string">"staticlib"</span><span>] </span></span></li><li><span> </span></li><li class="alt"><span>fn fib(at: usize) -> usize { </span></li><li><span><span class="keyword">if</span><span> at == 0 { </span></span></li><li class="alt"><span><span class="keyword">return</span><span> 0; </span></span></li><li><span>} <span class="keyword">else</span><span> </span><span class="keyword">if</span><span> at == 1 { </span></span></li><li class="alt"><span><span class="keyword">return</span><span> 1; </span></span></li><li><span>} </span></li><li class="alt"><span> </span></li><li><span>let mut total = 1; </span></li><li class="alt"><span>let mut parent = 1; </span></li><li><span>let mut gp = 0; </span></li><li class="alt"><span><span class="keyword">for</span><span> _ in 1 .. at { </span></span></li><li><span>total = parent + gp; </span></li><li class="alt"><span>gp = parent; </span></li><li><span>parent = total; </span></li><li class="alt"><span>} </span></li><li><span> </span></li><li class="alt"><span><span class="keyword">return</span><span> total; </span></span></li><li><span>} </span></li><li class="alt"><span> </span></li><li><span>#[no_mangle] </span></li><li class="alt"><span>pub extern <span class="string">"C"</span><span> fn rust_fib(at: usize) -> usize { </span></span></li><li><span>fib(at) </span></li><li class="alt"><span>} </span></li><li><span> </span></li><li class="alt"><span>注意,我编译的库rustc &ndash; O rust_lib.rs使编译器优化因为我们是这里的标准)。这里是C扩展源相关摘录): </span></li><li><span> </span></li><li class="alt"><span>PHP_FUNCTION(confirm_rust_fib_compiled) </span></li><li><span>{ </span></li><li class="alt"><span>long number; </span></li><li><span> </span></li><li class="alt"><span><span class="keyword">if</span><span> (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, </span><span class="string">"l"</span><span>, &number) == FAILURE) { </span></span></li><li><span><span class="keyword">return</span><span>; </span></span></li><li class="alt"><span>} </span></li><li><span> </span></li><li class="alt"><span>RETURN_LONG(rust_fib(number)); </span></li><li><span>} </span></li></ol>
Copy after login

运行PHP脚本:

<ol class="dp-c"><li class="alt"><span><span><?php </span></span></li><li><span><span class="vars">$br</span><span> = (php_sapi_name() == </span><span class="string">"cli"</span><span>)? </span><span class="string">""</span><span>:</span><span class="string">"<br>"</span><span>; </span></span></li><li class="alt"><span> </span></li><li><span><span class="keyword">if</span><span>(!</span><span class="func">extension_loaded</span><span>(</span><span class="string">'rust_fib'</span><span>)) { </span></span></li><li class="alt"><span>dl(<span class="string">'rust_fib.'</span><span> . PHP_SHLIB_SUFFIX); </span></span></li><li><span>} </span></li><li class="alt"><span> </span></li><li><span><span class="keyword">for</span><span> (</span><span class="vars">$i</span><span> = 0; </span><span class="vars">$i</span><span> < 100000; </span><span class="vars">$i</span><span> ++) { </span></span></li><li class="alt"><span>confirm_rust_fib_compiled(92); </span></li><li><span>} </span></li><li class="alt"><span>?> </span></li><li><span> </span></li><li class="alt"><span>这就是它的运行结果: </span></li><li><span> </span></li><li class="alt"><span>$ time php rust_fib.php </span></li><li><span> </span></li><li class="alt"><span>real 0m0.586s </span></li><li><span>user 0m0.342s </span></li><li class="alt"><span>sys 0m0.221s </span></li></ol>
Copy after login

你可以看见它比前者快了三倍!完美的Rust微基准!

总结

这里几乎没有得出什么结论。我不确定在Rust上写一个PHP的扩展是一个好的想法,但是花费一些时间去研究Rust,PHP和C,这是一个很好的方式。

如果你希望查看所有代码或者查看更改记录,可以访问GitHub Repo。



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template