52
MobaSiF::Template 高速なテンプレートエンジン DeNA - Tadashi Matsuda - bobpp [email protected] YAPC::Asia 2009 - 2009.09.10

MobaSiF::Template Introduction

Embed Size (px)

DESCRIPTION

MobaSiF::Template introduction on YAPC::Asia 2009 at 2009.09.10

Citation preview

Page 1: MobaSiF::Template Introduction

MobaSiF::Template高速なテンプレートエンジン

DeNA - Tadashi Matsuda - [email protected]

YAPC::Asia 2009 - 2009.09.10

Page 2: MobaSiF::Template Introduction

アジェンダ• 自己紹介

• MobaSiF::Template の概要/使い方

• ベンチマーク

• 速さのヒ・ミ・ツ

• まとめ

Page 3: MobaSiF::Template Introduction

自己紹介• 松田 唯史 (まつだ ただし)

• bobpp (ぼぶっぷ)

• http://bobpp.jp/

• (株) ディー・エヌ・エー

• モバオク(http://mbok.jp)のエンジニア

Page 4: MobaSiF::Template Introduction

初 YAPC!

• YAPC 初参加です

• いきなり発表することになり緊張しまくってますが

• よろしくお願いします

Page 5: MobaSiF::Template Introduction

•Twitter: BoBpp

•発表スライドなどリソースのせてます

Check this!

Page 6: MobaSiF::Template Introduction

MobaSiF

Page 7: MobaSiF::Template Introduction

Moba* Simple Framework

Page 8: MobaSiF::Template Introduction

Moba*

• モバオク

• 2003年から安定して稼働

• モバゲータウン

• 5億PV/day をさばく

• and more...

Page 9: MobaSiF::Template Introduction

Moba* Simple Framework

Page 10: MobaSiF::Template Introduction

Mobile Simple Framework

Page 11: MobaSiF::Template Introduction

ケータイ向けのシンプルなフレームワーク

Page 12: MobaSiF::Template Introduction

YAPC::Asia 2008 にてオープンソース化

Page 13: MobaSiF::Template Introduction

MobaSiF::Template

Page 14: MobaSiF::Template Introduction

MTemplate

• MobaSiF::Template のベース

• MobaSiF 内蔵のテンプレートエンジン

• 名前空間が攻め過ぎ。

Page 15: MobaSiF::Template Introduction

MobaSiF::Template

• 名前空間を整理 (MobaSiF::*)

• github による開発

• CPAN モジュール化

• 新機能

• テンプレートオートコンパイル

Page 16: MobaSiF::Template Introduction

基本構成• テンプレートエンジン

• MobaSiF::Template

• テンプレートコンパイラ

• MobaSiF::Template::Compiler

• プリコンパイラは鋭意開発中

Page 17: MobaSiF::Template Introduction
Page 18: MobaSiF::Template Introduction

HTMLTemplate

(*.html)

Page 19: MobaSiF::Template Introduction

HTMLTemplate

(*.html)

MobaSiF:::Template::Compilercompile

MobaSiF::Template::PreCompilerprecompile

IncludeTemplate

(*.txt/html)

Page 20: MobaSiF::Template Introduction

CompiledTemplate

(*.bin)

HTMLTemplate

(*.html)

MobaSiF:::Template::Compilercompile

MobaSiF::Template::PreCompilerprecompile

IncludeTemplate

(*.txt/html)

Page 21: MobaSiF::Template Introduction

CompiledTemplate

(*.bin)

HTMLTemplate

(*.html)

MobaSiF::Templateinsert / render

Output

(*.html)

MobaSiF:::Template::Compilercompile

MobaSiF::Template::PreCompilerprecompile

IncludeTemplate

(*.txt/html)

Page 22: MobaSiF::Template Introduction

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

機能• 分岐

• 文字列比較

• 数値比較

• ループ

• 修飾子

• サニタイズ

• URL Encode

Page 24: MobaSiF::Template Introduction

インストール

$ cpancpan> install MobaSiF::Template

Page 25: MobaSiF::Template Introduction

使い方<テンプレート:表示>

<!-- Escape して表示 -->$=h:escape_string$

<!-- Escape せずに表示 (bypass) -->$=b:not_escape_data$

<!-- URL Encode して表示 -->$=e:url_encode_string$

Page 26: MobaSiF::Template Introduction

使い方<テンプレート:分岐>

$ if (val > 10) { $ val が 10 より大きいです$ } elsif (val > 5) { $ val が 5 より大きいです$ } else { $ val が 5 以下です$ } $

Page 27: MobaSiF::Template Introduction

使い方<テンプレート:ループ>

$ loop (list) { $ $=h:id$ / $=h:key$ / $=h:value$$ } $

Page 28: MobaSiF::Template Introduction

使い方 <コンパイル>

use MobaSiF::Template::Compiler;

MobaSiF::Template::Compiler::compile(

“/path/to/template_file.html”,

“/path/to/compiled_binary_file.bin”

);

Page 29: MobaSiF::Template Introduction

使い方 <表示>

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

開発時自動コンパイル

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

開発時自動コンパイル

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

開発時自動コンパイル

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

特徴

Page 34: MobaSiF::Template Introduction

非常に高速

Page 35: MobaSiF::Template Introduction

Benchmark! <Rivals>

• Template Toolkit

• Tenjin

• HTML::Template::Pro

• MobaSiF::Template

Page 36: MobaSiF::Template Introduction

Benchmark

• 分岐/ループを織り交ぜたテンプレートを用意

• 10,000 回処理させてqpsを測定

Page 37: MobaSiF::Template Introduction

Machine

• MacBook

• OS X 10.5 Leopard

• perl v.5.8.9

• 2.4GHz Core2 Duo

• 4GB Memory

Page 38: MobaSiF::Template Introduction

Result.Benchmark#1

Page 39: MobaSiF::Template Introduction

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

なぜ高速なのか

Page 41: MobaSiF::Template Introduction

binary template / XS

Page 42: MobaSiF::Template Introduction

binary template

• 分岐・ループ・表示(option)...

• これらをすべてバイナリ表現

• 後述の XS で処理しやすいように

Page 43: MobaSiF::Template Introduction

XS

• mmap によるファイル読み込み

• テンプレートに記載されたバイナリ指示をひたすら実行するだけ

Page 44: MobaSiF::Template Introduction

バイナリ指示群

文字列データ群

Page 45: MobaSiF::Template Introduction

バイナリ指示群

文字列データ群 文字データ+NULL

Page 46: MobaSiF::Template Introduction

バイナリ指示群

文字列データ群 文字データ+NULL

文字列表示:PLAIN

分岐:IF/ELSE

ループ:LOOP / etc.

変数置換:REPLACE

Page 47: MobaSiF::Template Introduction

シンプル

• 機能が少ない分高速

• 本来 View(Template) でやるべきことを違う層で処理する必要 = デメリット

Page 48: MobaSiF::Template Introduction

開発予定

• Pre-Compiler

• Mobile Pre-Compiler

• compile files command

• tests!

• 修飾子の追加等

Page 49: MobaSiF::Template Introduction

まとめ

Page 50: MobaSiF::Template Introduction

速いです!

• とにかく速いです

• この機能で事足りる場合は是非検討してください!

Page 52: MobaSiF::Template Introduction

ありがとうございました