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

PHP Getting Faster

nunulk
October 12, 2019

PHP Getting Faster

nunulk

October 12, 2019
Tweet

More Decks by nunulk

Other Decks in Programming

Transcript

  1. ͞Βʹ଎͘ͳΔPHP
    nunulk 

    PHPΧϯϑΝϨϯεԭೄ

    View full-size slide

  2. ຊ೔͓࿩͢Δ͜ͱ
    • ࣗݾ঺հ
    • PHP 7.4 Ͱಋೖ͞ΕΔ Opcache Preloading ͱ FFI
    • ʢ͓·͚ʣNim ͷ঺հ

    View full-size slide

  3. ࣗݾ঺հ
    • ໊લ: nunulkʢ͵͵Δ͘ʣ
    • ৬ۀ: ϑϦʔϥϯεͷ΢ΣϒΤϯδχΞ
    • ਆಸ઒ݝ͔Βདྷ·ͨ͠
    • PHPͷ޷͖ͳͱ͜Ζ: Մมؔ਺ɺϚδοΫϝιου
    • Laravel Meetup Okinawa ࢀՃ཰: ໿ 45%

    View full-size slide

  4. PHP 7.4 is coming

    on Nov. 28th, 2019 !
    RC3 is now available.

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  7. Opcache Preloading
    • αʔόىಈ࣌ʹ Opcache ΛϝϞϦ্ʹϩʔυ͠
    ͓ͯ͘࢓૊Έ
    • ॳճΞΫηε࣌ʹ΋Ωϟογϡ͕ޮ͘Α͏ʹͳΔ
    • ͨͩ͠ࢦఆͰ͖Δͷ͸1ϑΝΠϧͷΈ

    View full-size slide

  8. Opcache Preloading
    // ϑϨʔϜϫʔΫͷ͢΂ͯͷϑΝΠϧΛΩϟογϡ͢Δ
    $frameworkDir = $baseDir . 'vendor/laravel/framework/
    src/Illuminate';
    $paths = load_all_files([$frameworkDir]);
    foreach ($paths as $path) {
    opcache_compile_file($path);

    }

    View full-size slide

  9. 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)

    View full-size slide

  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)


    View full-size slide

  11. Opcache Preloading
    • Opcache.enable = 1 ͢Δ͚ͩͰे෼଎͘ͳΔ
    • αʔόΛ࠶ىಈ͠ͳ͍ͱΩϟογϡ͸ߋ৽͞Εͳ͍ͷͰɺ
    ӡ༻ͷࡍ͸஫ҙ͕ඞཁ
    • Composer Ͱ΋ಋೖ͕ݕ౼͞Ε͍ͯΔͷͰɺকདྷతʹ͸
    preload͢Δର৅ͷબఆΛ composer ͔Βߦ͑ΔΑ͏ʹͳ
    Δ͔΋͠Εͳ͍

    View full-size slide

  12. FFI - Foreign Function Interface
    • PHP ͔ΒωΠςΟϒϥΠϒϥϦ
    ʢ.so, .dylib, .dllʣΛݺͿ࢓૊Έ
    • Python, Ruby, Java ͳͲͰ͢Ͱʹ࠾༻͞Ε
    ͍ͯΔ

    View full-size slide

  13. PHP ͔ΒωΠςΟϒϥΠϒϥ
    ϦΛݺͿ࢓૊Έ
    • PHP extension
    • Zend extension

    View full-size slide

  14. FFI
    $libfib = FFI::cdef('
    int fib(int a);
    ', ‘../nim/libfib.so');
    $ret = $libfib->fib(40);
    var_dump($ret);
    // int(102334155)

    View full-size slide

  15. 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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  19. FFI - Foreign Function Interface
    • ࢖͍ॴΛݟۃΊΔͷ͕೉͍͕͠ɺہॴతʹ
    εϐʔυΞοϓͤ͞Δબ୒ࢶͷͻͱͭ
    • େྔͷܭࢉΛͤ͞Δͷʹ޲͍͍ͯΔ
    • ͢ͰʹΤΫεςϯγϣϯ͕͋ΔͷͰ͋Ε͹
    ͦͪΒΛ࢖͏΄͏͕଎͍

    View full-size slide

  20. ʢ͓·͚ʣNim ͷ঺հ

    View full-size slide

  21. Nim
    • γεςϜϓϩάϥϛϯά޲͚੩తܕ෇͚ݴޠ
    • ϚϧνύϥμΠϜ
    • Python ʹࣅͨߏจΛ࣋ͭ
    • C/C++/JavaScript ʹτϥϯεύΠϧ͢Δ͜ͱ͕Ͱ͖Δ
    • 2019೥9݄ʹ 1.0 ͕ϦϦʔε͞Εͨ͹͔Γ

    View full-size slide

  22. Nim
    proc fib(a: cint): cint {.exportc, cdecl, dynlib.} =
    if a <= 2:
    result = 1
    else:
    result = fib(a - 1) + fib(a - 2)

    View full-size slide

  23. Nim
    $ nim c -d:release --noMain --header --app:lib fib.nim

    $ ls libfib.so
    libfib.so

    View full-size slide

  24. Nim
    $libfib = FFI::cdef('
    int fib(int a);
    ', ‘../nim/libfib.so');
    $ret = $libfib->fib(40);
    var_dump($ret);

    View full-size slide

  25. ڵຯͷ͋Δํ͸ Nim + FFI Ͱ

    ॏ͍ॲཧͷߴ଎Խʹ

    νϟϨϯδͯ͠Έ͍ͯͩ͘͞

    View full-size slide

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

    View full-size slide

  27. ͞Βʹ଎͘ͳΔ PHP

    ࠓޙͱ΋ΑΖ͓͘͠ئ͍͠·͢

    View full-size slide