Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

Rustに入門したくて!

 Rustに入門したくて!

YAPC::Nagoya::Tiny 2019 の LT のスライドです。

https://yapcjapan.connpass.com/event/146727/

Masahiro Honma

November 03, 2019
Tweet

More Decks by Masahiro Honma

Other Decks in Technology

Transcript

  1. 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); }
  2. 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;
  3. 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; }
  4. 例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 } }
  5. 例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% --
  6. 例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
  7. 例2: PUBLIC SUFFIX LIST $ carton exec -- 'perl -Mblib

    benchmarks/domain_public_suffix.pl' Rate pp rs pp 78740/s -- -9% rs 86957/s 10% -- あれ、たいして速くないじゃん・・・
  8. 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% --
  9. 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; }