Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

High Performance PHP - PHPNW

5,358 views

Published on

In reality, a fairly deep dive into compilers and how different PHP compiler implementations work.

Abstract:

PHP powers the majority of the internet. It's a fast, scalable and capable language. But what do you do when it's not fast enough? Do you switch to HHVM? What about HippyVM? What about Recki-CT? What about other options? In this talk, we'll dive into the options for speeding up a PHP site. We'll go over the options, when they are appropriate, and when they are not. We'll talk about how to make a site faster. We'll demystify and de-FUD the conversation around these alternative implementations, and get down to the real numbers involved.

Published in: Technology
  • Be the first to comment

High Performance PHP - PHPNW

  1. 1. High Performance PHP Anthony Ferrara
  2. 2. So you think you have scale problems?
  3. 3. Tip #1: You Don’t Have Scale Problems
  4. 4. Tip #1: You Don’t Have Scale Problems Your Code Just Sucks
  5. 5. Scale Free Networks
  6. 6. Scale Free Networks 80% Of Traffic Top 20% Of Sites
  7. 7. Scale Free Networks 64% Of Traffic Top 4% Of Sites 80% Of Traffic Top 20% Of Sites
  8. 8. Scale Free Networks 64% Of Traffic Top 4% Of Sites 80% Of Traffic Top 20% Of Sites Percent Traffic 20% 80% 4% 64% 0.8% 51.2% 0.016% 40.9% 0.0032% 32.78% 0.00064% 26.214% 0.0000128% 20.971%
  9. 9. Hardware Is Cheap Fleshware Is Not!
  10. 10. Relative Costs Server $0.25 Per Hour Developer $50 Per Hour
  11. 11. Relative Costs Server $0.25 Per Hour Developer $50 Per Hour 1 Developer Hour ≈≈ 400 Server Hours
  12. 12. On Average, A Developer Needs To Save One Server/Year Every 22 Hours To Justify Salary...
  13. 13. It’s A Gross Oversimplification...
  14. 14. Tip #2: Speed != Scale
  15. 15. Tip #2: Speed != Scale Speed == User Experience Scale == Server Experience
  16. 16. Adding Servers Will Not Fix Speed Issues
  17. 17. Changing Servers Will Not Fix Speed Issues
  18. 18. Changing Platforms Will Not Fix Speed Issues
  19. 19. Changing Languages Will Not Fix Speed Issues
  20. 20. Tip #3: Cache For Scale, Not Speed!!!
  21. 21. Tip #3: Cache For Scale, Not Speed!!! Caching For Speed Is A Band- Aid On A Gunshot Wound
  22. 22. Changing Code Will Fix Speed Issues
  23. 23. Fix Slow Code... Don’t Mitigate It
  24. 24. Disclaimer: The Rest Of This Talk Is About SCALE, Not Speed
  25. 25. Types Of Programming Languages - Machine Language - Device Specific - Note: NOT Assembly - Everything Else
  26. 26. What Is A Machine? - CPU Hardware? - GPU Hardware? - Virtual Hardware? - A Virtual CPU? - A Virtual Runtime?
  27. 27. What Is A Machine? Anything That Can Execute A Language Directly
  28. 28. Types Of Programming Languages - Machine Language - Device Specific - Note: NOT Assembly - Everything Else
  29. 29. Types Of Programming Languages - Machine Language - Device Specific - Note: NOT Assembly - Everything Else Executed Directly
  30. 30. Compiler (n): A computer program (or set of programs) that transforms source code written in one language (the source language) into another language (the target language).
  31. 31. Types Of Compilers - Assemblers - Basically Source Maps - Ahead Of Time (AOT) - Just In Time (JIT) - Tracing JIT - Local JIT - Region
  32. 32. function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y; }
  33. 33. Tracing JIT add(10) function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y; }
  34. 34. Tracing JIT add(10) function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y; } GUARD(arg[0], int)
  35. 35. Tracing JIT add(10) function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y; } GUARD(arg[0], int) int x = 1
  36. 36. Tracing JIT add(10) function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y; } GUARD(arg[0], int) int x = 1 int y = 2
  37. 37. Tracing JIT add(10) function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y; } GUARD(arg[0], int) int x = 1 int y = 2 int z = arg[0] + x
  38. 38. Tracing JIT add(10) function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y; } GUARD(arg[0], int) int x = 1 int y = 2 int z = arg[0] + x int tmp = z + y
  39. 39. Tracing JIT add(10) function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y; } GUARD(arg[0], int) int x = 1 int y = 2 int z = arg[0] + x int tmp = z + y return tmp
  40. 40. Tracing JIT add(10.5) function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y; } GUARD(arg[0], int) int x = 1 int y = 2 int z = arg[0] + x int tmp = z + y return tmp
  41. 41. Tracing JIT add(10.5) function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y; } GUARD(arg[0], int) int x = 1 int y = 2 int z = arg[0] + x int tmp = z + y return tmp
  42. 42. Tracing JIT add(10.5) function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y; } GUARD(arg[0], int) int x = 1 int y = 2 int z = arg[0] + x int tmp = z + y return tmp BAILOUT
  43. 43. Tracing JIT add(10.5) function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y; } GUARD(arg[0], int) int x = 1 int y = 2 int z = arg[0] + x int tmp = z + y return tmp BAILOUT
  44. 44. Tracing JIT add(10.5) function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y; } GUARD(arg[0], float) .....
  45. 45. Local JIT add(10) function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y; }
  46. 46. Local JIT / AOT add(10) function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y; } GUARD(arg[0], int) int z = arg[0] + 1 int tmp = z + 2 return tmp
  47. 47. But How Does It Do It?
  48. 48. AOT / Local JIT Compiler Code Parser Operations Target Code IR Code Generator
  49. 49. Real “Compilers” Code Compiler 1 Target Code IR 1 IR 2 Compiler 2 Compiler n
  50. 50. function blah($num) { if ($num) { return $num + 1; } return 0; }
  51. 51. function blah($num) { if ($num) { return $num + 1; } return 0; } Tokenize T_FUNCTION T_STRING T_VARIABLE T_IF T_VARIABLE T_RETURN T_VARIABLE T_LNUMBER T_RETURN T_LNUMBER
  52. 52. function blah($num) { if ($num) { return $num + 1; } return 0; } Parse function - params: [$num] - stmts: - If: - cond: - Variable: $num - stmts: - Return - Add - Variable: $num - LNumber: 1 - Return - LNumber: 0
  53. 53. function blah($num) { if ($num) { return $num + 1; } return 0; } Graph
  54. 54. function blah($num) { $x = 1; $x = $x + $num; return $x + 1; } Static Single Assignment (SSA) Assign $x = 1 Add ($x = $x + $num) Add ($tmp = $x + 1) Return $tmp
  55. 55. function blah($num) { $x = 1; $x = $x + $num; return $x + 1; } Static Single Assignment (SSA) Assign $x1 = 1 Add ($x = $x + $num) Add ($tmp = $x + 1) Return $tmp
  56. 56. function blah($num) { $x = 1; $x = $x + $num; return $x + 1; } Static Single Assignment (SSA) Assign $x1 = 1 Add ($x2 = $x1 + $num) Add ($tmp = $x + 1) Return $tmp
  57. 57. function blah($num) { $x = 1; $x = $x + $num; return $x + 1; } Static Single Assignment (SSA) Assign $x1 = 1 Add ($x2 = $x1 + $num) Add ($tmp = $x2 + 1) Return $tmp
  58. 58. function blah($num) { $x = 1; $x = $x + $num; return $x + 1; } Static Single Assignment (SSA) Assign $x1 = 1 Add ($x2 = $x1 + $num) Add ($tmp = $x2 + 1) Return $tmp
  59. 59. function blah($num) { $x = 1; if ($num) { $x = 2; } return $x; } Static Single Assignment (SSA) PHI Function Assign $x = 1 Jumpz $num end Assign $x = 2 :end Return $x
  60. 60. function blah($num) { $x = 1; if ($num) { $x = 2; } return $x; } Static Single Assignment (SSA) PHI Function Assign $x1 = 1 Jumpz $num end Assign $x = 2 :end Return $x
  61. 61. function blah($num) { $x = 1; if ($num) { $x = 2; } return $x; } Static Single Assignment (SSA) PHI Function Assign $x1 = 1 Jumpz $num end Assign $x2 = 2 :end Return $x
  62. 62. function blah($num) { $x = 1; if ($num) { $x = 2; } return $x; } Static Single Assignment (SSA) PHI Function Assign $x1 = 1 Jumpz $num end Assign $x2 = 2 :end Return $x
  63. 63. function blah($num) { $x = 1; if ($num) { $x = 2; } return $x; } Static Single Assignment (SSA) PHI Function Assign $x1 = 1 Jumpz $num end Assign $x2 = 2 :end Phi $x3 = ($x1, $x2) Return $x
  64. 64. function blah($num) { $x = 1; if ($num) { $x = 2; } return $x; } Static Single Assignment (SSA) PHI Function Assign $x1 = 1 Jumpz $num end Assign $x2 = 2 :end Phi $x3 = ($x1, $x2) Return $x3
  65. 65. function blah($num) { $x = 1; $x = $x + $num; return $x + 1; } Optimizing Static Single Assignment (SSA) Assign $x1 = 1 Add ($x2 = $x1 + $num) Add ($tmp = $x2 + 1) Return $tmp
  66. 66. function blah($num) { $x = 1; $x = $x + $num; return $x + 1; } Optimizing Static Single Assignment (SSA) Assign $x1 = 1 Add ($x2 = $x1 + $num) Add ($tmp = $x2 + 1) Return $tmp
  67. 67. function blah($num) { $x = 1; $x = $x + $num; return $x + 1; } Optimizing Static Single Assignment (SSA) Add ($x2 = 1 + $num) Add ($tmp = $x2 + 1) Return $tmp
  68. 68. Optimizations Type Efficiency Potential Complexity
  69. 69. function blah($num) { if ($num) { return $num + 1; } return 0; } Peephole
  70. 70. function blah($num) { if ($num) { return $num + 1; } return 0; } Peephole
  71. 71. function blah($num) { if ($num) { return $num + 1; } return 0; } Peephole
  72. 72. function blah($num) { if ($num) { return $num + 1; } return 0; } Peephole
  73. 73. function blah($num) { if ($num) { return $num + 1; } return 0; } Peephole
  74. 74. Optimizations Type Efficiency Potential Complexity Peephole High Low Simple
  75. 75. function blah($num) { if ($num) { return $num + 1; } return 0; } Local
  76. 76. function blah($num) { if ($num) { return $num + 1; } return 0; } Local
  77. 77. Optimizations Type Efficiency Potential Complexity Peephole High Low Simple Local Medium Moderate Simple
  78. 78. function blah($num) { if ($num) { return $num + 1; } return 0; } Global
  79. 79. Optimizations Type Efficiency Potential Complexity Peephole High Low Simple Local Medium Moderate Simple Global Low High Complex
  80. 80. function blah($num) { if ($num) { return $num + 1; } return 0; } Loop ?
  81. 81. Optimizations Type Efficiency Potential Complexity Peephole High Low Simple Local Medium Moderate Simple Global Low High Complex Loop Medium Moderate Complex
  82. 82. Putting It All Together
  83. 83. Types Of Compilers AOT Tracing JIT Local JIT Region Complexity Simple Moderate High Very High Optimizations Static None Dynamic Dynamic+Static Restart Cost None Moderate High High Potential Best Moderate Good Better Performance (Static Code) Potential Performance (Dynamic Code) Moderate Good Better Best
  84. 84. Zend PHP (5.6) PHP Zend OpCode Cache AOT Compiler
  85. 85. Zend OpCode line # * op return operands -------------------------------------------- 3 0 RECV !0 4 1 ADD ~0 !0, 1 2 RETURN ~0
  86. 86. HHVM (3.2) PHP HHVM OpCode Cache AOT Compiler Native Machine Code Cache Local JIT
  87. 87. HHVM OpCode (ByteCode) 10: Int 1 19: CGetL2 0 21: AddO 22: RetC
  88. 88. HippyVM PHP Hippy OpCode Cache AOT Compiler Native Machine Code Cache Tracing JIT
  89. 89. Recki-CT + JITFu PHP Cache AOT Compiler Recki IR JIT (kindof) Compiler Native Machine Code
  90. 90. Recki-CT Graph
  91. 91. Recki-CT IR param $1 long const $2 numeric 1 var $3 numeric + $1 $2 $3 return $3
  92. 92. Recki-CT + PECL PHP Recki IR Native AOT Compiler Machine Code PECL AOT Compiler
  93. 93. Types Of Compilers AOT Tracing JIT Local JIT Region VM? Zend X X HHVM X ? X HippyVM X X Recki-CT X ... V8 X ? SpiderMonkey / X
  94. 94. Performance Depends More On Your Code Than The Engine…
  95. 95. function add($num) { $x = 1; $y = $x . “1”; $z = $y + 2; return $z + $num; } Avoid Dynamic Types add(1) add(“1”) add([1, 2, 3]) add(“HI!”) add(new StdClass()) add(null) add(function() {}) add(fopen(“foo.txt”))
  96. 96. function blah($num) { global $amt; } Avoid Globals
  97. 97. function blah($num) { $func = “foo”; $func($num); } Avoid Dynamic Function Calls
  98. 98. if ($blah) { function blah($num) { } } Avoid Conditional Declarations
  99. 99. function blah($num) { eval(...) create_function(...) } Avoid Eval
  100. 100. The More Dynamic The Code, The Slower It Will Run
  101. 101. Anthony Ferrara joind.in/11805 @ircmaxell me@ircmaxell.com blog.ircmaxell.com github.com/ircmaxell youtube.com/ircmaxell

×