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
760
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
3.1k
偶然見つけたEncode.pmのバグ
hiratara
1
420
本当は難しいPSL
hiratara
3
4.2k
Haskellを使おう
hiratara
3
2.9k
Lens : Smart setter for immutable data
hiratara
1
830
Other Decks in Technology
See All in Technology
【CEDEC2026】Creative Approaches to Localizing the Dialects and Unique Speech of Umamusume: Pretty Derby Characters in English
cygames
PRO
3
25k
Goでデータパイプラインを作ろう
sansantech
PRO
1
500
【GCC2026】大規模言語モデルを活用した内製検索サービスの社内展開や業務活用
bandainamcostudios
PRO
0
240
QAエンジニア起点で進める、SmartHRにおける信頼性向上について
kaomi_wombat
1
150
トヨタ⽣産⽅式(TPS)⼊⾨
recruitengineers
PRO
3
990
Software Supply Chain Attackからクラウド環境を守るためにできること
lhazy
2
280
生成 AI の基礎 〜 サンプル実装で学ぶ基本原理
enakai00
7
4.4k
ブラウザ研修 2026
recruitengineers
PRO
7
1.2k
SmartHR Engineering Team Deck
smarthr
1
2.6k
DatadogのBits Chatが開発組織にもたらしたもの / What Bits Chat Has Brought Us
sms_tech
1
280
ホームラボ紹介
y_sera15
0
200
SO-101×VLAによる3色キューブのピック&プレース
abeja
0
220
Featured
See All Featured
Git: the NoSQL Database
bkeepers
PRO
432
67k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
620
Game over? The fight for quality and originality in the time of robots
wayneb77
1
240
GraphQLとの向き合い方2022年版
quramy
50
15k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.4k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2k
Faster Mobile Websites
deanohume
310
32k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
800
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
580
Marketing to machines
jonoalderson
1
5.7k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
1
400
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
2.4k
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が常に速いとは限らない(言語速度<アルゴリズム)