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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
380
本当は難しいPSL
hiratara
3
4.1k
Haskellを使おう
hiratara
3
2.9k
Lens : Smart setter for immutable data
hiratara
1
800
Other Decks in Technology
See All in Technology
扱える不確実性を増やしていく - スタートアップEMが考える「任せ方」
kadoppe
0
300
コミュニティ・勉強会を作るのは目的じゃない
ohmori_yusuke
0
210
クラウドネイティブな開発 ~ 認知負荷に立ち向かうためのコンテナ活用
literalice
0
130
M5Stack CoreS3とZephyr(RTOS)で Edge AIっぽいことしてみた
iotengineer22
0
220
Introduction to Sansan, inc / Sansan Global Development Center, Inc.
sansan33
PRO
0
3.1k
Amazon S3 Filesについて
yama3133
2
210
AI時代のガードレールとしてのAPIガバナンス
nagix
0
280
Hacobu Tech Deck
hacobu
PRO
0
110
EBS暗号化に失敗してEC2が動かなくなった話
hamaguchimmm
2
200
実践ハーネスエンジニアリング:TAKTで実現するAIエージェント制御 / Practical Harness Engineering: AI Agent Control Enabled by TAKT
nrslib
11
4.6k
目的ファーストのハーネス設計 ~ハーネスの変更容易性を高めるための優先順位~
gotalab555
8
2.2k
AI時代 に増える データ活用先
takahal
0
230
Featured
See All Featured
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.2k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
180
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
680
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.4k
Mobile First: as difficult as doing things right
swwweet
225
10k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
1k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
890
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
490
Principles of Awesome APIs and How to Build Them.
keavy
128
17k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.8k
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が常に速いとは限らない(言語速度<アルゴリズム)