Slide 1

Slide 1 text

Phalcon The  Fastest  PHP  Framework Rack  Lin  阿土伯   @phpcon8w2013

Slide 2

Slide 2 text

about:me !   目前為 ViViPOS  Co.,  Ltd  技術總監   !   利用 javascript  寫 〞收銀機〞(傳統產業,全新感受)   !   PHP  /  Javascript  /  Java  Programmer  (目前移情於 Scala  )   ! [email protected]   ! hNps://twiNer.com/racklin   ! hNps://www.facebook.com/racklin1002   ! hNp://racklin.blogspot.tw/      (生小孩後就變癈墟)  

Slide 3

Slide 3 text

2012  cphalcon  contributors

Slide 4

Slide 4 text

2013  cphalcon  contributors

Slide 5

Slide 5 text

  學如逆⽔水⾏行⾈舟、不進則退     在 Open  Source  更是如此  

Slide 6

Slide 6 text

Phalcon  Framework   Phalcon  Kernel   Zephir  

Slide 7

Slide 7 text

Phalcon  Dev  Box !   Virtual  Machine  for  PhalconPHP  Development   ! hNps://github.com/racklin/phalcon-­‐dev-­‐box   !   Vagrant  /  VirtualBox  

Slide 8

Slide 8 text

Phalcon  Benefits   !  The  Fastest   !  Few  Memory  Usage   !  Cross-­‐PlaZorm   !  Highly  Decoupled   !   Full-­‐stack  Framework   C 實作

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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  

Slide 11

Slide 11 text

所以結論…   為了快!專案要用 c  /  c++  來寫  web    

Slide 12

Slide 12 text

Raphters !   A  web  framework  for  C

Slide 13

Slide 13 text

除了慣C哥     大部份人打完招呼   Hello  World!   不知道要做什麼…..

Slide 14

Slide 14 text

Fibonacci  as  php  extension

Slide 15

Slide 15 text

Fibonacci  as  php  extension 0m0.79s        馬上變成第二名

Slide 16

Slide 16 text

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  

Slide 17

Slide 17 text

速度只要出問題,  C  都能搞定

Slide 18

Slide 18 text

  平衡   A  PHP  web  framework     delivered  as  a  C  extension  

Slide 19

Slide 19 text

Frameworks  benchmark

Slide 20

Slide 20 text

Frameworks  benchmark

Slide 21

Slide 21 text

 速度不是問題?

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

Raspberry  Pi

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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  

Slide 28

Slide 28 text

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  

Slide 29

Slide 29 text

Highly  Decoupled !   Ex.  Validamon

Slide 30

Slide 30 text

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  

Slide 31

Slide 31 text

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)  

Slide 32

Slide 32 text

Phalcon  Development  Tools ! WebDevTools  

Slide 33

Slide 33 text

Phalcon  Development  Tools !   Command  Line  Tools  

Slide 34

Slide 34 text

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.    

Slide 35

Slide 35 text

CLI  Launcher

Slide 36

Slide 36 text

CLI  MainTask Php  console.php    //  MainTask::mainAcmon   Php  console.php  main  test  //  MainTask::testAcmon  

Slide 37

Slide 37 text

Phalcon  Kernel

Slide 38

Slide 38 text

Phalcon  Kernel(2012~) !   2012  PHPConfTW     ! Phalcon  0.5.x  Source  Code  分離出來的專案。   ! hNps://github.com/racklin/phalcon_kernel   !   Ant  的 phpWowExtension  專案。   ! hNps://github.com/y8zeng/phpWowExtension  

Slide 39

Slide 39 text

Phalcon  Kernel(~2012) !   2012  PHPConfTW     ! Phalcon  0.5.x  Source  Code  分離出來的專案。   ! hNps://github.com/racklin/phalcon_kernel   !   Ant  的 phpWoWExtension  專案。   ! hNps://github.com/y8zeng/phpWowExtension  

Slide 40

Slide 40 text

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.  

Slide 41

Slide 41 text

Phalcon  Kernel(2012~) !   基於 ZEND  API   !   提供大量 Helper  funcmons  /  macros   ! php  style    的 c  開發風格  

Slide 42

Slide 42 text

Zend  API

Slide 43

Slide 43 text

眼皮忍耐一下!    

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

zval

Slide 46

Slide 46 text

PHP>        $greemng

Slide 47

Slide 47 text

Data  Types Long Boolean Double String  value ZVAL_LONG() ZVAL_BOOL() ZVAL_DOUBLE() ZVAL_STRING() NULL ZVAL_NULL() Create  macro

Slide 48

Slide 48 text

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()

Slide 49

Slide 49 text

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)

Slide 50

Slide 50 text

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  的記 憶體

Slide 51

Slide 51 text

MM  Sample

Slide 52

Slide 52 text

MM  Sample

Slide 53

Slide 53 text

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);  

Slide 54

Slide 54 text

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)  \

Slide 55

Slide 55 text

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  

Slide 56

Slide 56 text

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 個參數  

Slide 57

Slide 57 text

Call  Funcmons PHP  Code

Slide 58

Slide 58 text

Call  Funcmons

Slide 59

Slide 59 text

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  個參數  

Slide 60

Slide 60 text

Call  Methods

Slide 61

Slide 61 text

OK!  Wake  UP!

Slide 62

Slide 62 text

c9s  ValidamonKit hNps://github.com/c9s/ValidamonKit     c  extension  DEMO     速度快 3  倍    

Slide 63

Slide 63 text

寫 C  就像談戀愛     即期待   又怕受傷害  

Slide 64

Slide 64 text

大部份人對於 C

Slide 65

Slide 65 text

Zephir

Slide 66

Slide 66 text

大海賊 DSL  時代來臨 ! Javascript   ! CoffeeScript  ,  LiveScript  ,    TypeScript.   !   Java   ! Scala  ,  Groovy,  Clojure   !   GNOME  GObject  C   ! Vala   ! Zend  Engine  C   ! Zephir    

Slide 67

Slide 67 text

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++

Slide 68

Slide 68 text

CompilaLon  Scheme

Slide 69

Slide 69 text

Hello  World!

Slide 70

Slide 70 text

Hello  World! 開頭少了

Slide 71

Slide 71 text

Hello  World  Generated  C

Slide 72

Slide 72 text

Zephir  -­‐  PHP  Syntax  Like

Slide 73

Slide 73 text

Generated  C

Slide 74

Slide 74 text

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)

Slide 75

Slide 75 text

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)  

Slide 76

Slide 76 text

Variable  DeclaraLons !   Declaramons

Slide 77

Slide 77 text

Variable  Assignment !   Assignment  /  Value  Change   !   MUST  Using    let  keyword.

Slide 78

Slide 78 text

Dynamic  Types

Slide 79

Slide 79 text

Stamc  Types

Slide 80

Slide 80 text

Arrays

Slide 81

Slide 81 text

Hashes NOT  PHP        k  =>  v   NOT  JSON  

Slide 82

Slide 82 text

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:    

Slide 83

Slide 83 text

Calling  FuncLons !   To  call  a  PHP  funcmon,  you  can  just  refer  its  name  in  the  Zephir  

Slide 84

Slide 84 text

Create  Zephir  Project ! zephir  init  –  Inimal  zephir  project  and  create  config.json   ! zephir  [compile]  –  Compile  and  Install  compiled  .so  .

Slide 85

Slide 85 text

DEMO   Hello  World!     Encoder  /  Decoder  Extension    (自己動手,別花錢買 ionCube  啦)

Slide 86

Slide 86 text

b2p  go  back  to  PHP

Slide 87

Slide 87 text

Phalcon  2.0 ! Phalcon  2.0  is  currently  being  rewriNen  in  Zephir.

Slide 88

Slide 88 text

Phalcon  2.0 ! Phalcon  2.0  is  currently  being  rewriNen  in  Zephir.

Slide 89

Slide 89 text

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.    

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

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  

Slide 92

Slide 92 text

Thank  You