phalcon / zephir introduction at phpconftw2013
DESCRIPTION
TRANSCRIPT
Phalcon The Fastest PHP Framework
Rack Lin 阿土伯 @phpcon8w2013
about:me
! 目前為 ViViPOS Co., Ltd 技術總監 ! 利用 javascript 寫 〞收銀機〞(傳統產業,全新感受)
! PHP / Javascript / Java Programmer (目前移情於 Scala )
! hNps://twiNer.com/racklin
! hNps://www.facebook.com/racklin1002
! hNp://racklin.blogspot.tw/ (生小孩後就變癈墟)
2012 cphalcon contributors
2013 cphalcon contributors
學如逆⽔水⾏行⾈舟、不進則退
在 Open Source 更是如此
Phalcon Framework Phalcon Kernel
Zephir
Phalcon Dev Box
! Virtual Machine for PhalconPHP Development
! hNps://github.com/racklin/phalcon-‐dev-‐box
! Vagrant / VirtualBox
Phalcon Benefits
! The Fastest
! Few Memory Usage
! Cross-‐PlaZorm
! Highly Decoupled ! Full-‐stack Framework
C 實作
C 有多快
! Fibonacci 40 (費式數列) hNp://bit.ly/Pk8HkJ
Language Times
C with –O2 0m0.202s
java 0m1.305s
scala 0m1.808s
nodejs 0m2.872s
Ruby (1.9.x) 0m37.404s
Php (5.4) 1m17.728s
Python (2.7.2) 1m17.979s
Ruby (1.8.x) 3m35.135s
Php (5.3) 6m17.192s
Fibonacci 40
0
100
200
300
400
500
c with -‐O2
java scala nodejs ruby 1.9.2
php 5.4 ruby 1.8.x
php 5.3
所以結論…
為了快!專案要用 c / c++ 來寫 web
Raphters ! A web framework for C
除了慣C哥
大部份人打完招呼 Hello World!
不知道要做什麼…..
Fibonacci as php extension
Fibonacci as php extension
0m0.79s 馬上變成第二名
Fibonacci 圖表
0 50
100 150 200 250 300 350 400 450
c with -‐O2 java scala nodejs ruby 1.9.2 php 5.4 ruby 1.8.x php 5.3 php ext
速度只要出問題, C 都能搞定
平衡
A PHP web framework
delivered as a C extension
Frameworks benchmark
Frameworks benchmark
速度不是問題?
Raspberry Pi
Raspberry Pi
! OS: Raspbian (Debian 7)
! Web Server: Nginx 1.2.1
! PHP 5.4.4 (FPM)
! CPU: ARMv6 700 MHz
! RAM: 512MB (480MB)
! Disk: 4GB Class 6 SDHC
hNps://gist.github.com/hdogan/5466569
Frameworks benchmark Pi
2.89 3.96 5.4
67.38
0
10
20
30
40
50
60
70
80
Laravel Yii CodeIgniter Phalcon
Requests per Second
Frameworks benchmark Pi
345.58
252.61
185.24
14.84 0
50
100
150
200
250
300
350
400
Laravel Yii CodeIgniter Phalcon
Memory Usage
Highly Decoupled
! Ex. Validamon
EVERYTHING YOU NEED
! Single or Mulm Module MVC Applicamons
! Sinatra / Slim / expressjs like Micro Applicamons.
! Dependency Injecmon/Container
! Universal Auto-‐Loader
! Events Management
! ORM – MySQL / PostgreSQL (#schema)/ SQLite
! ODM for Mongo
Template Engine Volt
! Volt is an ultra-‐fast and designer friendly templamng language wriNen in C for PHP. (TWIG Extension only one method)
! Volt is inspired by Jinja (Python)
Phalcon Development Tools
! WebDevTools
Phalcon Development Tools
! Command Line Tools
Console Applicamon
! Single / Mulm Module Console Applicamons
! cronjobs
! Daemon Applicamon ! Long-‐life applicamon , using supervisor and kill yourself.
! Job Workers ! Using supervisor and kill yourself.
CLI Launcher
CLI MainTask
Php console.php // MainTask::mainAcmon
Php console.php main test // MainTask::testAcmon
Phalcon Kernel
Phalcon Kernel(2012~)
! 2012 PHPConfTW ! Phalcon 0.5.x Source Code 分離出來的專案。
! hNps://github.com/racklin/phalcon_kernel
! Ant 的 phpWowExtension 專案。 ! hNps://github.com/y8zeng/phpWowExtension
Phalcon Kernel(~2012)
! 2012 PHPConfTW ! Phalcon 0.5.x Source Code 分離出來的專案。
! hNps://github.com/racklin/phalcon_kernel
! Ant 的 phpWoWExtension 專案。 ! hNps://github.com/y8zeng/phpWowExtension
Phalcon Kernel(2012~)
! hNps://github.com/phalcon/cphalcon/blob/master/ext/kernel/README.md
Phalcon Kernel is a meta-‐framework on top of the Zend API helping to create PHP extensions in an easier way for a PHP developer. In order to build a fast and stable framework, we have created the Phalcon Kernel. The use of this API helps us to write C code in a PHP style. We have developed a number of funcmons to help the programmer to write code more interoperable with PHP in a easier way. Phalcon Kernel API is based on the Zend API, but we have added more features to facilitate us the work. Phalcon is a very large project, frameworks need to be developed and improved every day, Phalcon Kernel API helps us to write C code that is more stable and familiar to PHP developers.
Phalcon Kernel(2012~)
! 基於 ZEND API
! 提供大量 Helper funcmons / macros
! php style 的 c 開發風格
Zend API
眼皮忍耐一下!
zval
PHP> $greemng
Data Types
Long Boolean Double String value
ZVAL_LONG() ZVAL_BOOL() ZVAL_DOUBLE() ZVAL_STRING()
NULL
ZVAL_NULL()
Create macro
Data Types
Long Boolean Double String value String Length
Z_LVAL() Z_BVAL() Z_DVAL() Z_STRVAL() Z_STRLEN()
Z_LVAL_P() Z_BVAL_P() Z_DVAL_P() Z_STRVAL_P() Z_STRLEN_P()
HashTable Object
Z_ARRVAL() Z_OBJVAL()
Z_ARRVAL_P() Z_OBJVAL_P()
GeNer and Idenmfy macro
IdenLfy BOOL Long Double String NULL
Z_TYPE() IS_BOOL IS_LONG IS_DOUBLE IS_STRING IS_NULL
Z_TYPE_P()
Return Types
Macro Type
RETURN_FALSE / RETURN_TRUE Boolean
RETURN_NULL() NULL
RETURN_LONG() Long
RETURN_DOUBLE() Double
RETURN_STRING() String
RETURN_CTOR() RETURN_CCTOR() RETURN_NCTOR()
ZVAL (Phalcon)
Memory Management
! Phalcon Memory Management 簡化了 Zend Memory Managemant.
! PHALCON_MM_GROW() 建立一個 phalcon memory entry 結構
! PHALCON_INIT_VAR 建立並配置記憶體給 zval 並將其設為 null, 且將其指標的指標存至 phalcon memory entry.
! PHALCON_MM_RESTORE 釋放所有經由 PHALCON_INIT_VAR 配置且 refcount =1 的記憶體
MM Sample
MM Sample
String
! ZVAL_STRING() – Zend API
! concat_funcmon – Zend API
! Concat macro
PHALCON_INIT_VAR(month); ZVAL_STRING(month, "2012", 1); PHALCON_INIT_VAR(today_is); ZVAL_STRING(today_is, "Today is", 1);
PHALCON_INIT_VAR(first_part); concat_funcmon(first_part, today_is, month);
PHALCON_INIT_VAR(today); PHALCON_CONCAT_SVSV(today, "Today is", month, " ", day);
Concat Macros #define PHALCON_CONCAT_SV(result, op1, op2) \ #define PHALCON_CONCAT_SVS(result, op1, op2, op3) \ #define PHALCON_CONCAT_SVSV(result, op1, op2, op3, op4) \ #define PHALCON_CONCAT_SVSVS(result, op1, op2, op3, op4, op5) \ #define PHALCON_CONCAT_SVSVSV(result, op1, op2, op3, op4, op5, op6) \ #define PHALCON_CONCAT_SVSVSVS(result, op1, op2, op3, op4, op5, op6, op7) \ #define PHALCON_CONCAT_SVSVV(result, op1, op2, op3, op4, op5) \ #define PHALCON_CONCAT_SVV(result, op1, op2, op3) \ #define PHALCON_CONCAT_VS(result, op1, op2) \ #define PHALCON_CONCAT_VSV(result, op1, op2, op3) \ #define PHALCON_CONCAT_VSVS(result, op1, op2, op3, op4) \ #define PHALCON_CONCAT_VSVSVS(result, op1, op2, op3, op4, op5, op6) \ #define PHALCON_CONCAT_VSVSVSV(result, op1, op2, op3, op4, op5, op6, op7) \ #define PHALCON_CONCAT_VV(result, op1, op2) \ #define PHALCON_CONCAT_VVSV(result, op1, op2, op3, op4) \ #define PHALCON_CONCAT_VVV(result, op1, op2, op3) \ #define PHALCON_CONCAT_VVVVV(result, op1, op2, op3, op4, op5) \
Arrays
! array_init(zval*) – inimal as array
! Add to array ! add_next_index_long / add_next_index_double ! add_next_index_bool / add_next_index_stringl
! Get (index type) (*string length) ! phalcon_array_fetch_long / phalcon_array_fetch_string
! Update (index type) (*string length) ! phalcon_array_update_long / phalcon_array_update_string
! Isset / unset (*string size) ! phalcon_array_isset_long / phalcon_array_isset_string ! phalcon_array_unset / phalcon_array_unset_string
Call Funcmons
! PHALCON_CALL_FUNC 呼叫 PHP funcmon ,有回傳值
! PHALCON_CALL_FUNC_NORETURN 呼叫 PHP funcmon ,沒有回傳值
! PHALCON_CALL_FUNC_PARAMS_1 呼叫 PHP funcmon 並傳一個參數,有回傳值
! PHALCON_CALL_FUNC_PARAMS_1_NORETURN 呼叫 PHP funcmon 並傳一個參數,沒有回傳值
! 目前巨集定義到傳 3 個參數
Call Funcmons
PHP Code
Call Funcmons
Call Methods
! PHALCON_CALL_METHOD 呼叫 PHP funcmon ,有回傳值
! PHALCON_CALL_METHOD_NORETURN 呼叫 PHP funcmon ,沒有回傳值
! PHALCON_CALL_METHOD_PARAMS_1 呼叫 PHP funcmon 並傳一個參數,有回傳值
! PHALCON_CALL_METHOD_PARAMS_1_NORETURN 呼叫 PHP funcmon 並傳一個參數,沒有回傳值
! 目前巨集定義到傳 5 個參數
Call Methods
OK! Wake UP!
c9s ValidamonKit hNps://github.com/c9s/ValidamonKit
c extension DEMO
速度快 3 倍
寫 C 就像談戀愛
即期待 又怕受傷害
大部份人對於 C
Zephir
大海賊 DSL 時代來臨
! Javascript ! CoffeeScript , LiveScript , TypeScript.
! Java ! Scala , Groovy, Clojure
! GNOME GObject C ! Vala
! Zend Engine C ! Zephir
Zephir
! Zephir -‐ Ze(nd Engine) Ph(p) I(nte)r(mediate)
! A high level language that eases the creamon and maintainability of extensions for PHP.
! Zephir extensions are exported to C code that can be compiled and opmmized by major C compilers such as gcc/clang/vc++
CompilaLon Scheme
Hello World!
Hello World!
開頭少了 <?php 語法幾乎相同。
Hello World Generated C
Zephir -‐ PHP Syntax Like
Generated C
Zephir features
! Stamc/Dynamic typing
! Automamc memory management
! Produce object-‐oriented libraries that can be used in PHP
! Produce faster and beNer code that can be compiled by most important C-‐compilers: (gcc, vc++, clang)
! The syntax of Zephir is inspired by C, PHP, Rust and Javascript.
! Make the code more debugable ?????? (Without source map)
Basic
! Organizing Code in Files and Namespaces ! Every file must contain a class (and just one class). ! Every class must have a namespace ! The directory structure must match the names of classes and
namespaces used. ( NOT Follow PSR-‐0)
Variable DeclaraLons
! Declaramons
Variable Assignment
! Assignment / Value Change
! MUST Using let keyword.
Dynamic Types
Stamc Types
Arrays
Hashes
NOT PHP k => v NOT JSON
For Statement
! A ‘for’ is a control structure that allows to traverse arrays or strings
!
! Keys in hashes can be obtained in the following way:
Calling FuncLons
! To call a PHP funcmon, you can just refer its name in the Zephir
Create Zephir Project
! zephir init – Inimal zephir project and create config.json
! zephir [compile] – Compile and Install compiled .so .
DEMO
Hello World!
Encoder / Decoder Extension (自己動手,別花錢買 ionCube 啦)
b2p
go back to PHP
Phalcon 2.0
! Phalcon 2.0 is currently being rewriNen in Zephir.
Phalcon 2.0
! Phalcon 2.0 is currently being rewriNen in Zephir.
Is Zephir produc0on-‐ready
! No, it’s in a very alpha stage.
! Phalcon Kernel is producmon-‐ready.
! If you review generated c sources, it’s producmon-‐ready.
! Keep Zephir in mind.
References
! Phalcon-‐dev-‐box: ! hNps://github.com/racklin/phalcon-‐dev-‐box
! PHPConfTW2013 sample Codes: ! hNps://github.com/racklin/phpcon8w2013
! Slides: ! hNps://speakerdeck.com/racklin/zephir-‐jian-‐jie
Thank You