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
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
偶然見つけたEncode.pmのバグ
hiratara
1
250
本当は難しいPSL
hiratara
3
4k
Haskellを使おう
hiratara
3
2.8k
Lens : Smart setter for immutable data
hiratara
1
760
Other Decks in Technology
See All in Technology
【新卒研修資料】LLM・生成AI研修 / Large Language Model・Generative AI
brainpadpr
25
17k
extension 現場で使えるXcodeショートカット一覧
ktombow
0
220
How to achieve interoperable digital identity across Asian countries
fujie
0
120
Shirankedo NOCで見えてきたeduroam/OpenRoaming運用ノウハウと課題 - BAKUCHIKU BANBAN #2
marokiki
0
160
SwiftUIのGeometryReaderとScrollViewを基礎から応用まで学び直す:設計と活用事例
fumiyasac0921
0
150
PLaMoの事後学習を支える技術 / PFN LLMセミナー
pfn
PRO
9
3.9k
AI ReadyなData PlatformとしてのAutonomous Databaseアップデート
oracle4engineer
PRO
0
220
AWS Top Engineer、浮いてませんか? / As an AWS Top Engineer, Are You Out of Place?
yuj1osm
2
150
GopherCon Tour 概略
logica0419
2
190
AIAgentの限界を超え、 現場を動かすWorkflowAgentの設計と実践
miyatakoji
0
150
Azure Well-Architected Framework入門
tomokusaba
1
330
関係性が駆動するアジャイル──GPTに人格を与えたら、対話を通してふりかえりを習慣化できた話
mhlyc
0
130
Featured
See All Featured
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
114
20k
GraphQLの誤解/rethinking-graphql
sonatard
73
11k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.1k
Rebuilding a faster, lazier Slack
samanthasiow
84
9.2k
A Modern Web Designer's Workflow
chriscoyier
697
190k
Building Adaptive Systems
keathley
43
2.8k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Fireside Chat
paigeccino
40
3.7k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.2k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
285
14k
A Tale of Four Properties
chriscoyier
160
23k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
9
850
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が常に速いとは限らない(言語速度<アルゴリズム)