Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Rustに入門したくて!
Search
Masahiro Honma
November 03, 2019
Technology
740
0
Share
Rustに入門したくて!
YAPC::Nagoya::Tiny 2019 の LT のスライドです。
https://yapcjapan.connpass.com/event/146727/
Masahiro Honma
November 03, 2019
More Decks by Masahiro Honma
See All by Masahiro Honma
ARA に捧げる鎮魂歌
hiratara
0
2.8k
偶然見つけたEncode.pmのバグ
hiratara
1
370
本当は難しいPSL
hiratara
3
4.1k
Haskellを使おう
hiratara
3
2.9k
Lens : Smart setter for immutable data
hiratara
1
790
Other Decks in Technology
See All in Technology
Microsoft Fabricで考える非構造データのAI活用
ryomaru0825
0
540
VSCode中心だった自分がターミナル沼に入門した話
sanogemaru
0
860
GitHub Actions侵害 — 相次ぐ事例を振り返り、次なる脅威に備える
flatt_security
11
6.9k
やさしいとこから始めるGitHubリポジトリのセキュリティ
tsubakimoto_s
3
2.1k
【Oracle Cloud ウェビナー】データ主権はクラウドで守れるのか?NTTデータ様のOracle Alloyで実現するソブリン対応クラウドの最適解
oracle4engineer
PRO
3
120
AIエージェント時代に必要な オペレーションマネージャーのロールとは
kentarofujii
0
230
Oracle Cloud Infrastructure:2026年3月度サービス・アップデート
oracle4engineer
PRO
0
220
Zephyr(RTOS)でOpenPLCを実装してみた
iotengineer22
0
160
開発チームとQAエンジニアの新しい協業モデル -年末調整開発チームで実践する【QAリード施策】-
qa
0
530
AWS DevOps Agent or Kiro の使いどころを考える_20260402
masakiokuda
0
100
OpenClawでPM業務を自動化
knishioka
2
350
Kiro Meetup #7 Kiro アップデート (2025/12/15〜2026/3/20)
katzueno
2
270
Featured
See All Featured
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.2k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
93
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.6k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
260
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3.4k
Accessibility Awareness
sabderemane
0
88
Exploring anti-patterns in Rails
aemeredith
2
300
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.3k
Visualization
eitanlees
150
17k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
140
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が常に速いとは限らない(言語速度<アルゴリズム)