mobasif::template introduction
DESCRIPTION
MobaSiF::Template introduction on YAPC::Asia 2009 at 2009.09.10TRANSCRIPT
![Page 1: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/1.jpg)
MobaSiF::Template高速なテンプレートエンジン
DeNA - Tadashi Matsuda - [email protected]
YAPC::Asia 2009 - 2009.09.10
![Page 2: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/2.jpg)
アジェンダ• 自己紹介
• MobaSiF::Template の概要/使い方
• ベンチマーク
• 速さのヒ・ミ・ツ
• まとめ
![Page 3: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/3.jpg)
自己紹介• 松田 唯史 (まつだ ただし)
• bobpp (ぼぶっぷ)
• http://bobpp.jp/
• (株) ディー・エヌ・エー
• モバオク(http://mbok.jp)のエンジニア
![Page 4: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/4.jpg)
初 YAPC!
• YAPC 初参加です
• いきなり発表することになり緊張しまくってますが
• よろしくお願いします
![Page 5: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/5.jpg)
•Twitter: BoBpp
•発表スライドなどリソースのせてます
Check this!
![Page 6: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/6.jpg)
MobaSiF
![Page 7: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/7.jpg)
Moba* Simple Framework
![Page 8: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/8.jpg)
Moba*
• モバオク
• 2003年から安定して稼働
• モバゲータウン
• 5億PV/day をさばく
• and more...
![Page 9: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/9.jpg)
Moba* Simple Framework
![Page 10: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/10.jpg)
Mobile Simple Framework
![Page 11: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/11.jpg)
ケータイ向けのシンプルなフレームワーク
![Page 12: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/12.jpg)
YAPC::Asia 2008 にてオープンソース化
![Page 13: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/13.jpg)
MobaSiF::Template
![Page 14: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/14.jpg)
MTemplate
• MobaSiF::Template のベース
• MobaSiF 内蔵のテンプレートエンジン
• 名前空間が攻め過ぎ。
![Page 15: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/15.jpg)
MobaSiF::Template
• 名前空間を整理 (MobaSiF::*)
• github による開発
• CPAN モジュール化
• 新機能
• テンプレートオートコンパイル
![Page 16: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/16.jpg)
基本構成• テンプレートエンジン
• MobaSiF::Template
• テンプレートコンパイラ
• MobaSiF::Template::Compiler
• プリコンパイラは鋭意開発中
![Page 17: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/17.jpg)
![Page 18: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/18.jpg)
HTMLTemplate
(*.html)
![Page 19: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/19.jpg)
HTMLTemplate
(*.html)
MobaSiF:::Template::Compilercompile
MobaSiF::Template::PreCompilerprecompile
IncludeTemplate
(*.txt/html)
![Page 20: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/20.jpg)
CompiledTemplate
(*.bin)
HTMLTemplate
(*.html)
MobaSiF:::Template::Compilercompile
MobaSiF::Template::PreCompilerprecompile
IncludeTemplate
(*.txt/html)
![Page 21: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/21.jpg)
CompiledTemplate
(*.bin)
HTMLTemplate
(*.html)
MobaSiF::Templateinsert / render
Output
(*.html)
MobaSiF:::Template::Compilercompile
MobaSiF::Template::PreCompilerprecompile
IncludeTemplate
(*.txt/html)
![Page 22: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/22.jpg)
Compile Command
CompiledTemplate
(*.bin)
HTMLTemplate
(*.html)
MobaSiF::Templateinsert / render
Output
(*.html)
MobaSiF:::Template::Compilercompile
MobaSiF::Template::PreCompilerprecompile
IncludeTemplate
(*.txt/html)
HTMLTemplate
(*.html)
HTMLTemplate
(*.html)
HTMLTemplate
(*.html)
![Page 23: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/23.jpg)
機能• 分岐
• 文字列比較
• 数値比較
• ループ
• 修飾子
• サニタイズ
• URL Encode
![Page 24: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/24.jpg)
インストール
$ cpancpan> install MobaSiF::Template
![Page 25: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/25.jpg)
使い方<テンプレート:表示>
<!-- Escape して表示 -->$=h:escape_string$
<!-- Escape せずに表示 (bypass) -->$=b:not_escape_data$
<!-- URL Encode して表示 -->$=e:url_encode_string$
![Page 26: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/26.jpg)
使い方<テンプレート:分岐>
$ if (val > 10) { $ val が 10 より大きいです$ } elsif (val > 5) { $ val が 5 より大きいです$ } else { $ val が 5 以下です$ } $
![Page 27: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/27.jpg)
使い方<テンプレート:ループ>
$ loop (list) { $ $=h:id$ / $=h:key$ / $=h:value$$ } $
![Page 28: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/28.jpg)
使い方 <コンパイル>
use MobaSiF::Template::Compiler;
MobaSiF::Template::Compiler::compile(
“/path/to/template_file.html”,
“/path/to/compiled_binary_file.bin”
);
![Page 29: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/29.jpg)
使い方 <表示>
use MobaSiF::Template;
my $param = +{ hoge => 1, fuga => [
+{ id => 1, value => ‘A’ },
+{ id => 2, value => ‘B’ },
] };
print MobaSiF::Template::insert(
“/path/to/compiled_binary_file.bin”, $param);
![Page 30: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/30.jpg)
開発時自動コンパイル
use MobaSiF::Template;
my $param = +{...};
$MobaSiF::Template::DEVELOP = 1;
print MobaSiF::Template::render(
“/path/to/template_file.html”,
“/path/to/compiled_binary_file.bin”, $param);
![Page 31: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/31.jpg)
開発時自動コンパイル
use MobaSiF::Template;
my $param = +{...};
$MobaSiF::Template::DEVELOP = 1;
print MobaSiF::Template::render(
“/path/to/template_file.html”,
“/path/to/compiled_binary_file.bin”, $param);
本番は 0 に
![Page 32: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/32.jpg)
開発時自動コンパイル
use MobaSiF::Template;
my $param = +{...};
$MobaSiF::Template::DEVELOP = 1;
print MobaSiF::Template::render(
“/path/to/template_file.html”,
“/path/to/compiled_binary_file.bin”, $param);
性能は1割ほどdown 本番は 0 に
![Page 33: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/33.jpg)
特徴
![Page 34: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/34.jpg)
非常に高速
![Page 35: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/35.jpg)
Benchmark! <Rivals>
• Template Toolkit
• Tenjin
• HTML::Template::Pro
• MobaSiF::Template
![Page 36: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/36.jpg)
Benchmark
• 分岐/ループを織り交ぜたテンプレートを用意
• 10,000 回処理させてqpsを測定
![Page 37: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/37.jpg)
Machine
• MacBook
• OS X 10.5 Leopard
• perl v.5.8.9
• 2.4GHz Core2 Duo
• 4GB Memory
![Page 38: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/38.jpg)
Result.Benchmark#1
![Page 39: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/39.jpg)
Result.0 5000.00 10000.00 15000.00 20000.00
585.8
2421.3
6451.6
17543.9
Template Toolkit
Tenjin
HTML::Template::Pro
MobaSiF::Template
Benchmark#1
![Page 40: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/40.jpg)
なぜ高速なのか
![Page 41: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/41.jpg)
binary template / XS
![Page 42: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/42.jpg)
binary template
• 分岐・ループ・表示(option)...
• これらをすべてバイナリ表現
• 後述の XS で処理しやすいように
![Page 43: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/43.jpg)
XS
• mmap によるファイル読み込み
• テンプレートに記載されたバイナリ指示をひたすら実行するだけ
![Page 44: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/44.jpg)
バイナリ指示群
文字列データ群
![Page 45: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/45.jpg)
バイナリ指示群
文字列データ群 文字データ+NULL
![Page 46: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/46.jpg)
バイナリ指示群
文字列データ群 文字データ+NULL
文字列表示:PLAIN
分岐:IF/ELSE
ループ:LOOP / etc.
変数置換:REPLACE
![Page 47: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/47.jpg)
シンプル
• 機能が少ない分高速
• 本来 View(Template) でやるべきことを違う層で処理する必要 = デメリット
![Page 48: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/48.jpg)
開発予定
• Pre-Compiler
• Mobile Pre-Compiler
• compile files command
• tests!
• 修飾子の追加等
![Page 49: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/49.jpg)
まとめ
![Page 50: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/50.jpg)
速いです!
• とにかく速いです
• この機能で事足りる場合は是非検討してください!
![Page 51: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/51.jpg)
CPAN / github
• http://search.cpan.org/~bobpp/MobaSiF-Template/
• http://github.com/bobpp/perl-MobaSiF-Template/
![Page 52: MobaSiF::Template Introduction](https://reader034.vdocuments.pub/reader034/viewer/2022052620/55795bf6d8b42ab6648b4f49/html5/thumbnails/52.jpg)
ありがとうございました