Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP Getting Faster

73b0a25ed03f649ec63ded82ce4ac7cb?s=47 nunulk
October 12, 2019

PHP Getting Faster

73b0a25ed03f649ec63ded82ce4ac7cb?s=128

nunulk

October 12, 2019
Tweet

Transcript

  1. ͞Βʹ଎͘ͳΔPHP nunulk 
 PHPΧϯϑΝϨϯεԭೄ

  2. ຊ೔͓࿩͢Δ͜ͱ • ࣗݾ঺հ • PHP 7.4 Ͱಋೖ͞ΕΔ Opcache Preloading ͱ

    FFI • ʢ͓·͚ʣNim ͷ঺հ
  3. ࣗݾ঺հ • ໊લ: nunulkʢ͵͵Δ͘ʣ • ৬ۀ: ϑϦʔϥϯεͷ΢ΣϒΤϯδχΞ • ਆಸ઒ݝ͔Βདྷ·ͨ͠ •

    PHPͷ޷͖ͳͱ͜Ζ: Մมؔ਺ɺϚδοΫϝιου • Laravel Meetup Okinawa ࢀՃ཰: ໿ 45%
  4. I work for

  5. PHP 7.4 is coming
 on Nov. 28th, 2019 ! RC3

    is now available.
  6. New features • Typed Properties • Arrow Functions • Null

    coalescing assignment operator • Spread operator in arrays • Covariant Returns and Contravariant Parameters • Opcache Preloading • FFI
  7. New features • Typed Properties • Arrow Functions • Null

    coalescing assignment operator • Spread operator in arrays • Covariant Returns and Contravariant Parameters • Opcache Preloading • FFI
  8. Opcache Preloading • αʔόىಈ࣌ʹ Opcache ΛϝϞϦ্ʹϩʔυ͠ ͓ͯ͘࢓૊Έ • ॳճΞΫηε࣌ʹ΋Ωϟογϡ͕ޮ͘Α͏ʹͳΔ •

    ͨͩ͠ࢦఆͰ͖Δͷ͸1ϑΝΠϧͷΈ
  9. Opcache Preloading // ϑϨʔϜϫʔΫͷ͢΂ͯͷϑΝΠϧΛΩϟογϡ͢Δ $frameworkDir = $baseDir . 'vendor/laravel/framework/ src/Illuminate';

    $paths = load_all_files([$frameworkDir]); foreach ($paths as $path) { opcache_compile_file($path);
 }
  10. Opcache Preloading ab -n 100 -c 10 http://localhost Opcache ͳ͠

    Requests per second: 0.91 [#/sec] (mean) Time per request: 11010.544 [ms] (mean) Opcache ͋Γ Requests per second: 5.34 [#/sec] (mean) Time per request: 1872.084 [ms] (mean) Opcache ͋Γ w/ preload Requests per second: 6.32 [#/sec] (mean) Time per request: 1582.046 [ms] (mean)
  11. Opcache Preloading ab -n 100 -c 10 http://localhost Opcache ͳ͠

    Requests per second: 0.91 [#/sec] (mean) Time per request: 11010.544 [ms] (mean) Opcache ͋Γ Requests per second: 5.34 [#/sec] (mean) Time per request: 1872.084 [ms] (mean) Opcache ͋Γ w/ preload Requests per second: 6.32 [#/sec] (mean) Time per request: 1582.046 [ms] (mean)  
  12. Opcache Preloading • Opcache.enable = 1 ͢Δ͚ͩͰे෼଎͘ͳΔ • αʔόΛ࠶ىಈ͠ͳ͍ͱΩϟογϡ͸ߋ৽͞Εͳ͍ͷͰɺ ӡ༻ͷࡍ͸஫ҙ͕ඞཁ

    • Composer Ͱ΋ಋೖ͕ݕ౼͞Ε͍ͯΔͷͰɺকདྷతʹ͸ preload͢Δର৅ͷબఆΛ composer ͔Βߦ͑ΔΑ͏ʹͳ Δ͔΋͠Εͳ͍
  13. FFI - Foreign Function Interface • PHP ͔ΒωΠςΟϒϥΠϒϥϦ ʢ.so, .dylib,

    .dllʣΛݺͿ࢓૊Έ • Python, Ruby, Java ͳͲͰ͢Ͱʹ࠾༻͞Ε ͍ͯΔ
  14. PHP ͔ΒωΠςΟϒϥΠϒϥ ϦΛݺͿ࢓૊Έ • PHP extension • Zend extension

  15. FFI <?php $libfib = FFI::cdef(' int fib(int a); ', ‘../nim/libfib.so');

    $ret = $libfib->fib(40); var_dump($ret); // int(102334155)
  16. FFI // for/if Λ࢖ͬͯ N ·Ͱͷۮ਺ͷ࿨Λܭࢉ͢Δ
 # php index.php loop

    --php 1000000 0.056366920471191 
 # php index.php loop --ffi 1000000 0.037209987640381 
 // N ൪໨ͷϑΟϘφον਺Λ࠶ؼͰܭࢉ͢Δ # php index.php fib --php 40 4.6368939876556 # php index.php fib --ffi 40 0.28206396102905 // MySQL ʹ N ݅σʔλΛૠೖ͢Δ # php index.php mysql --php 1000 0.47904992103577 # php index.php mysql --ffi 1000 0.57981204986572
  17. FFI // for/if Λ࢖ͬͯ N ·Ͱͷۮ਺ͷ࿨Λܭࢉ͢Δ
 # php index.php loop

    --php 1000000 0.056366920471191 
 # php index.php loop --ffi 1000000 0.037209987640381 
 // N ൪໨ͷϑΟϘφον਺Λ࠶ؼͰܭࢉ͢Δ # php index.php fib --php 40 4.6368939876556 # php index.php fib --ffi 40 0.28206396102905 // MySQL ʹ N ݅σʔλΛૠೖ͢Δ # php index.php mysql --php 1000 0.47904992103577 # php index.php mysql --ffi 1000 0.57981204986572 
  18. FFI // for/if Λ࢖ͬͯ N ·Ͱͷۮ਺ͷ࿨Λܭࢉ͢Δ
 # php index.php loop

    --php 1000000 0.056366920471191 
 # php index.php loop --ffi 1000000 0.037209987640381 
 // N ൪໨ͷϑΟϘφον਺Λ࠶ؼͰܭࢉ͢Δ # php index.php fib --php 40 4.6368939876556 # php index.php fib --ffi 40 0.28206396102905 // MySQL ʹ N ݅σʔλΛૠೖ͢Δ # php index.php mysql --php 1000 0.47904992103577 # php index.php mysql --ffi 1000 0.57981204986572 
  19. FFI // for/if Λ࢖ͬͯ N ·Ͱͷۮ਺ͷ࿨Λܭࢉ͢Δ
 # php index.php loop

    --php 1000000 0.056366920471191 
 # php index.php loop --ffi 1000000 0.037209987640381 
 // N ൪໨ͷϑΟϘφον਺Λ࠶ؼͰܭࢉ͢Δ # php index.php fib --php 40 4.6368939876556 # php index.php fib --ffi 40 0.28206396102905 // MySQL ʹ N ݅σʔλΛૠೖ͢Δ # php index.php mysql --php 1000 0.47904992103577 # php index.php mysql --ffi 1000 0.57981204986572 
  20. FFI - Foreign Function Interface • ࢖͍ॴΛݟۃΊΔͷ͕೉͍͕͠ɺہॴతʹ εϐʔυΞοϓͤ͞Δબ୒ࢶͷͻͱͭ • େྔͷܭࢉΛͤ͞Δͷʹ޲͍͍ͯΔ

    • ͢ͰʹΤΫεςϯγϣϯ͕͋ΔͷͰ͋Ε͹ ͦͪΒΛ࢖͏΄͏͕଎͍
  21. ʢ͓·͚ʣNim ͷ঺հ

  22. Nim • γεςϜϓϩάϥϛϯά޲͚੩తܕ෇͚ݴޠ • ϚϧνύϥμΠϜ • Python ʹࣅͨߏจΛ࣋ͭ • C/C++/JavaScript

    ʹτϥϯεύΠϧ͢Δ͜ͱ͕Ͱ͖Δ • 2019೥9݄ʹ 1.0 ͕ϦϦʔε͞Εͨ͹͔Γ
  23. Nim proc fib(a: cint): cint {.exportc, cdecl, dynlib.} = if

    a <= 2: result = 1 else: result = fib(a - 1) + fib(a - 2)
  24. Nim $ nim c -d:release --noMain --header --app:lib fib.nim 


    $ ls libfib.so libfib.so 

  25. Nim <?php $libfib = FFI::cdef(' int fib(int a); ', ‘../nim/libfib.so');

    $ret = $libfib->fib(40); var_dump($ret);
  26. ڵຯͷ͋Δํ͸ Nim + FFI Ͱ
 ॏ͍ॲཧͷߴ଎Խʹ
 νϟϨϯδͯ͠Έ͍ͯͩ͘͞

  27. JIT in PHP 8.0 is coming on Sep. 23, 2021

    !
  28. ͞Βʹ଎͘ͳΔ PHP
 ࠓޙͱ΋ΑΖ͓͘͠ئ͍͠·͢