Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Rustに入門したくて!
Search
Masahiro Honma
November 03, 2019
Technology
0
720
Rustに入門したくて!
YAPC::Nagoya::Tiny 2019 の LT のスライドです。
https://yapcjapan.connpass.com/event/146727/
Masahiro Honma
November 03, 2019
Tweet
Share
More Decks by Masahiro Honma
See All by Masahiro Honma
ARA に捧げる鎮魂歌
hiratara
0
1.2k
偶然見つけたEncode.pmのバグ
hiratara
1
340
本当は難しいPSL
hiratara
3
4.1k
Haskellを使おう
hiratara
3
2.8k
Lens : Smart setter for immutable data
hiratara
1
770
Other Decks in Technology
See All in Technology
ページの可視領域を算出する方法について整理する
yamatai1212
0
160
バグハンター視点によるサプライチェーンの脆弱性
scgajge12
2
490
HIG学習用スライド
yuukiw00w
0
110
Noを伝える技術2025: 爆速合意形成のためのNICOフレームワーク速習 #pmconf2025
aki_iinuma
2
1.1k
Sansan Engineering Unit 紹介資料
sansan33
PRO
1
3.3k
Design System Documentation Tooling 2025
takanorip
1
930
Bill One 開発エンジニア 紹介資料
sansan33
PRO
4
16k
eBPFとwaruiBPF
sat
PRO
4
1.5k
著者と読み解くAIエージェント現場導入の勘所 Lancers TechBook#2
smiyawaki0820
9
3.1k
翻訳・対話・越境で強いチームワークを作ろう! / Building Strong Teamwork through Interpretation, Dialogue, and Border-Crossing
ar_tama
4
1.6k
私のRails開発環境
yahonda
0
180
一億総業務改善を支える社内AIエージェント基盤の要諦
yukukotani
8
2.8k
Featured
See All Featured
Build your cross-platform service in a week with App Engine
jlugia
234
18k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3.2k
Designing Experiences People Love
moore
142
24k
Why You Should Never Use an ORM
jnunemaker
PRO
60
9.6k
Producing Creativity
orderedlist
PRO
348
40k
The Art of Programming - Codeland 2020
erikaheidi
56
14k
Learning to Love Humans: Emotional Interface Design
aarron
274
41k
Mobile First: as difficult as doing things right
swwweet
225
10k
Docker and Python
trallard
46
3.7k
A Tale of Four Properties
chriscoyier
162
23k
Imperfection Machines: The Place of Print at Facebook
scottboms
269
13k
The Cult of Friendly URLs
andyhume
79
6.7k
Transcript
Rustに入門したくて! perl-xsでXSを書き始めた話 hiratara
Rustに入門したい理由 おっさんプログラマの(以下略)
XSは難しい • XS言語がむずかしい • C言語がむずかしい • Perl APIがむずかしい
XSは難しい MODULE = Sum PACKAGE = Sum void sum(...) PPCODE:
{ if (items != 2) { croak("Invalid argument count: %d", items); } SV *a = ST(0); SV *b = ST(1); IV ret = SvIV(a) + SvIV(b); XPUSHs(sv_2mortal(newSViv(ret))); XSRETURN(1); }
perl-xs • rustのクレート • PerlXS API のラッパー • rustだけでPerl拡張が書ける
perl-xs use perl_xs::IV; xs! { package Sum::RS; sub sum(ctx, a:
IV, b: IV) { a + b } }
用意するもの • cpanfile • Makefile.PL • lib/Foo/Bar.pm • src/lib.rs
cpanfile • Module::Install::Rust ◦ Makefile.PL の記述に必要 • Ouroboros ◦ perl-xs
クレートが必要
Makefile.PL use inc::Module::Install; name "Sum-RS"; version "0.01"; abstract "Tests for
perl-xs"; configure_requires "Module::Install::Rust" => 0; requires "XSLoader" => 0; rust_use_perl_xs { git => "https://github.com/vickenty/perl-xs" }; rust_requires "perl-sys" => { git => "https://github.com/vickenty/perl-sys" }; rust_requires "perlxs_derive" => { git => "https://github.com/vickenty/perl-xs" }; rust_write; WriteAll;
lib/Foo/Bar.pm package Sum::RS; use strict; use warnings; require XSLoader; XSLoader::load();
src/lib.rs #[macro_use] extern crate perl_xs; #[macro_use] extern crate perl_sys; mod
fib { use perl_xs::IV; xs! { package Sum::RS; sub sum(ctx, a: IV, b: IV) { a + b } } } xs! { bootstrap boot_Sum__RS; use fib; }
例1: フィボナッチ数列 (perl) sub fib { return 1 if $_[0]
<= 2; fib($_[0] - 1) + fib($_[0] - 2); }
例1: フィボナッチ数列 (perl-xs) use perl_xs::IV; fn fib_(n: i32) -> i32
{ if n <= 2 { 1 } else { fib_(n - 1) + fib_(n - 2) } } xs! { package Fib::RS; sub fib(ctx, n: IV) { fib_(n as i32) as IV } }
例1: フィボナッチ数列 $ carton exec -- 'perl -Mblib benchmarks/fib.pl' (warning:
too few iterations for a reliable count) Rate pp rs pp 3.74/s -- -99% rs 500/s 13260% --
例2: PUBLIC SUFFIX LIST • .com, .co.jp, .jp, .*.compute.amazonaws.com など
◦ クッキーを設定できないドメイン • Domain::PublicSuffix ◦ https://metacpan.org/pod/Domain::PublicSuffix • publicsuffix ◦ https://github.com/rushmorem/publicsuffix
例2: PUBLIC SUFFIX LIST $ carton exec -- 'perl -Mblib
benchmarks/domain_public_suffix.pl' Rate pp rs pp 78740/s -- -9% rs 86957/s 10% -- あれ、たいして速くないじゃん・・・
遅い理由 • rust の publicsuffix の実装が遅い • perl-xs が遅い
publicsuffixが遅い • 線形検索してる ◦ 修正済: https://github.com/rushmorem/publicsuffix/pull/20 • 余分な処理がある ◦ ドメインのフォーマットのバリデーション
◦ ユニコード周りの処理 $ carton exec -- 'perl -Mblib benchmarks/domain_public_suffix.pl' (warning: too few iterations for a reliable count) Rate pp rs pp 77519/s -- -71% rs 270270/s 249% --
perl-xsが遅い • Ouroboros のオーバヘッド(マクロの関数化) • 例外処理のオーバヘッド IV ouroboros_sv_iv(pTHX_ SV* sv)
{ return SvIV(sv); } int perl_sys_hv_iternext(HE** RETVAL, HV * hv) { int rc = 0; dJMPENV; JMPENV_PUSH(rc); if (rc == 0) { *RETVAL = hv_iternext(hv); } JMPENV_POP; return rc; }
perl-xs のまとめ • rustだけで書けて楽 • PerlとRustが切り替わる箇所はそれなりに遅い • PerlよりRustが常に速いとは限らない(言語速度<アルゴリズム)