high performance php - phpnw

101
High Performance PHP Anthony Ferrara

Upload: anthony-ferrara

Post on 02-Dec-2014

1.978 views

Category:

Technology


1 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: High Performance PHP - PHPNW

High Performance PHPAnthony Ferrara

Page 2: High Performance PHP - PHPNW

So you think you have scale problems?

Page 3: High Performance PHP - PHPNW

You Don’t Have Scale Problems

Tip #1:

Page 4: High Performance PHP - PHPNW

You Don’t Have Scale Problems

Tip #1:

Your Code Just Sucks

Page 5: High Performance PHP - PHPNW

Scale Free Networks

Page 6: High Performance PHP - PHPNW

Scale Free Networks

Top 20% Of Sites

80% Of Traffic

Page 7: High Performance PHP - PHPNW

Scale Free Networks

Top 20% Of Sites

80% Of Traffic

Top 4% Of Sites64% Of Traffic

Page 8: High Performance PHP - PHPNW

Scale Free Networks

Top 20% Of Sites

80% Of Traffic

Top 4% Of Sites64% Of Traffic

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%

Page 9: High Performance PHP - PHPNW

Hardware Is CheapFleshware Is Not!

Page 10: High Performance PHP - PHPNW

Server$0.25 Per Hour

Relative Costs

Developer$50 Per Hour

Page 11: High Performance PHP - PHPNW

Server$0.25 Per Hour

Relative Costs

Developer$50 Per Hour

1 Developer Hour ≈≈

400 Server Hours

Page 12: High Performance PHP - PHPNW

On Average, A Developer Needs To Save One

Server/Year Every 22 Hours To Justify Salary...

Page 13: High Performance PHP - PHPNW

It’s A Gross Oversimplification...

Page 14: High Performance PHP - PHPNW

Speed != ScaleTip #2:

Page 15: High Performance PHP - PHPNW

Speed != ScaleTip #2:

Speed == User ExperienceScale == Server Experience

Page 16: High Performance PHP - PHPNW

Adding Servers Will Not Fix Speed Issues

Page 17: High Performance PHP - PHPNW

Changing Servers Will Not Fix Speed

Issues

Page 18: High Performance PHP - PHPNW

Changing Platforms Will Not Fix Speed

Issues

Page 19: High Performance PHP - PHPNW

Changing Languages Will Not Fix Speed

Issues

Page 20: High Performance PHP - PHPNW

Cache For Scale, Not Speed!!!

Tip #3:

Page 21: High Performance PHP - PHPNW

Cache For Scale, Not Speed!!!

Tip #3:

Caching For Speed Is A Band-Aid On A Gunshot Wound

Page 22: High Performance PHP - PHPNW

Changing Code Will Fix Speed Issues

Page 23: High Performance PHP - PHPNW

Fix Slow Code... Don’t Mitigate It

Page 24: High Performance PHP - PHPNW

Disclaimer:

The Rest Of This Talk Is About SCALE, Not Speed

Page 25: High Performance PHP - PHPNW

Types Of Programming Languages

- Machine Language- Device Specific- Note: NOT Assembly

- Everything Else

Page 26: High Performance PHP - PHPNW

What Is A Machine?- CPU Hardware?- GPU Hardware?- Virtual Hardware?- A Virtual CPU?- A Virtual Runtime?

Page 27: High Performance PHP - PHPNW

What Is A Machine?

Anything That Can Execute A Language

Directly

Page 28: High Performance PHP - PHPNW

Types Of Programming Languages

- Machine Language- Device Specific- Note: NOT Assembly

- Everything Else

Page 29: High Performance PHP - PHPNW

Types Of Programming Languages

- Machine Language- Device Specific- Note: NOT Assembly

- Everything ElseExecuted Directly

Page 30: High Performance PHP - PHPNW

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

Page 31: High Performance PHP - PHPNW

Types Of Compilers

- Assemblers- Basically Source Maps

- Ahead Of Time (AOT)- Just In Time (JIT)

- Tracing JIT- Local JIT- Region

Page 32: High Performance PHP - PHPNW

function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y;}

Page 33: High Performance PHP - PHPNW

function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y;}

Tracing JIT add(10)

Page 34: High Performance PHP - PHPNW

function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y;}

Tracing JIT add(10)

GUARD(arg[0], int)

Page 35: High Performance PHP - PHPNW

function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y;}

Tracing JIT add(10)

GUARD(arg[0], int)int x = 1

Page 36: High Performance PHP - PHPNW

function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y;}

Tracing JIT add(10)

GUARD(arg[0], int)int x = 1int y = 2

Page 37: High Performance PHP - PHPNW

function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y;}

Tracing JIT add(10)

GUARD(arg[0], int)int x = 1int y = 2int z = arg[0] + x

Page 38: High Performance PHP - PHPNW

function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y;}

Tracing JIT add(10)

GUARD(arg[0], int)int x = 1int y = 2int z = arg[0] + xint tmp = z + y

Page 39: High Performance PHP - PHPNW

function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y;}

Tracing JIT add(10)

GUARD(arg[0], int)int x = 1int y = 2int z = arg[0] + xint tmp = z + yreturn tmp

Page 40: High Performance PHP - PHPNW

function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y;}

Tracing JIT add(10.5)

GUARD(arg[0], int)int x = 1int y = 2int z = arg[0] + xint tmp = z + yreturn tmp

Page 41: High Performance PHP - PHPNW

function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y;}

Tracing JIT add(10.5)

GUARD(arg[0], int)int x = 1int y = 2int z = arg[0] + xint tmp = z + yreturn tmp

Page 42: High Performance PHP - PHPNW

function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y;}

Tracing JIT add(10.5)

GUARD(arg[0], int)int x = 1int y = 2int z = arg[0] + xint tmp = z + yreturn tmp

BAILOUT

Page 43: High Performance PHP - PHPNW

function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y;}

Tracing JIT add(10.5)

GUARD(arg[0], int)int x = 1int y = 2int z = arg[0] + xint tmp = z + yreturn tmp

BAILOUT

Page 44: High Performance PHP - PHPNW

function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y;}

Tracing JIT add(10.5)

GUARD(arg[0], float).....

Page 45: High Performance PHP - PHPNW

function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y;}

Local JIT add(10)

Page 46: High Performance PHP - PHPNW

function add($num) { $x = 1; $y = 2; $z = $num + $x; return $z + $y;}

Local JIT / AOT add(10)

GUARD(arg[0], int)int z = arg[0] + 1int tmp = z + 2return tmp

Page 47: High Performance PHP - PHPNW

But How Does It Do It?

Page 48: High Performance PHP - PHPNW

AOT / Local JIT Compiler

Code

ParserOperations

Target Code

IR

Code Generator

Page 49: High Performance PHP - PHPNW

Real “Compilers”

Code

Compiler 1

Target Code

IR 1

Compiler 2

IR 2

Compiler n

Page 50: High Performance PHP - PHPNW

function blah($num) { if ($num) { return $num + 1; } return 0;}

Page 51: High Performance PHP - PHPNW

function blah($num) {

if ($num) {

return $num + 1;

}

return 0;

}

TokenizeT_FUNCTIONT_STRINGT_VARIABLET_IFT_VARIABLET_RETURNT_VARIABLET_LNUMBERT_RETURNT_LNUMBER

Page 52: High Performance PHP - PHPNW

function blah($num) {

if ($num) {

return $num + 1;

}

return 0;

}

Parsefunction

- params: [$num]- stmts:

- If:- cond:

- Variable: $num- stmts:

- Return- Add

- Variable: $num- LNumber: 1

- Return- LNumber: 0

Page 53: High Performance PHP - PHPNW

function blah($num) {

if ($num) {

return $num + 1;

}

return 0;

}

Graph

Page 54: High Performance PHP - PHPNW

function blah($num) {

$x = 1;

$x = $x + $num;

return $x + 1;

}

Static Single Assignment (SSA)

Assign $x = 1Add ($x = $x + $num)Add ($tmp = $x + 1)Return $tmp

Page 55: High Performance PHP - PHPNW

function blah($num) {

$x = 1;

$x = $x + $num;

return $x + 1;

}

Static Single Assignment (SSA)

Assign $x1 = 1Add ($x = $x + $num)Add ($tmp = $x + 1)Return $tmp

Page 56: High Performance PHP - PHPNW

function blah($num) {

$x = 1;

$x = $x + $num;

return $x + 1;

}

Static Single Assignment (SSA)

Assign $x1 = 1Add ($x2 = $x1 + $num)Add ($tmp = $x + 1)Return $tmp

Page 57: High Performance PHP - PHPNW

function blah($num) {

$x = 1;

$x = $x + $num;

return $x + 1;

}

Static Single Assignment (SSA)

Assign $x1 = 1Add ($x2 = $x1 + $num)Add ($tmp = $x2 + 1)Return $tmp

Page 58: High Performance PHP - PHPNW

function blah($num) {

$x = 1;

$x = $x + $num;

return $x + 1;

}

Static Single Assignment (SSA)

Assign $x1 = 1Add ($x2 = $x1 + $num)Add ($tmp = $x2 + 1)Return $tmp

Page 59: High Performance PHP - PHPNW

function blah($num) {

$x = 1;

if ($num) {

$x = 2;

}

return $x;

}

Static Single Assignment (SSA)PHI Function

Assign $x = 1Jumpz $num endAssign $x = 2:endReturn $x

Page 60: High Performance PHP - PHPNW

function blah($num) {

$x = 1;

if ($num) {

$x = 2;

}

return $x;

}

Static Single Assignment (SSA)PHI Function

Assign $x1 = 1Jumpz $num endAssign $x = 2:endReturn $x

Page 61: High Performance PHP - PHPNW

function blah($num) {

$x = 1;

if ($num) {

$x = 2;

}

return $x;

}

Static Single Assignment (SSA)PHI Function

Assign $x1 = 1Jumpz $num endAssign $x2 = 2:endReturn $x

Page 62: High Performance PHP - PHPNW

function blah($num) {

$x = 1;

if ($num) {

$x = 2;

}

return $x;

}

Static Single Assignment (SSA)PHI Function

Assign $x1 = 1Jumpz $num endAssign $x2 = 2:endReturn $x

Page 63: High Performance PHP - PHPNW

function blah($num) {

$x = 1;

if ($num) {

$x = 2;

}

return $x;

}

Static Single Assignment (SSA)PHI Function

Assign $x1 = 1Jumpz $num endAssign $x2 = 2:endPhi $x3 = ($x1, $x2)Return $x

Page 64: High Performance PHP - PHPNW

function blah($num) {

$x = 1;

if ($num) {

$x = 2;

}

return $x;

}

Static Single Assignment (SSA)PHI Function

Assign $x1 = 1Jumpz $num endAssign $x2 = 2:endPhi $x3 = ($x1, $x2)Return $x3

Page 65: High Performance PHP - PHPNW

function blah($num) {

$x = 1;

$x = $x + $num;

return $x + 1;

}

OptimizingStatic Single Assignment (SSA)

Assign $x1 = 1Add ($x2 = $x1 + $num)Add ($tmp = $x2 + 1)Return $tmp

Page 66: High Performance PHP - PHPNW

function blah($num) {

$x = 1;

$x = $x + $num;

return $x + 1;

}

OptimizingStatic Single Assignment (SSA)

Assign $x1 = 1Add ($x2 = $x1 + $num)Add ($tmp = $x2 + 1)Return $tmp

Page 67: High Performance PHP - PHPNW

function blah($num) {

$x = 1;

$x = $x + $num;

return $x + 1;

}

OptimizingStatic Single Assignment (SSA)

Add ($x2 = 1 + $num)Add ($tmp = $x2 + 1)Return $tmp

Page 68: High Performance PHP - PHPNW

OptimizationsType Efficiency Potential Complexity

Page 69: High Performance PHP - PHPNW

function blah($num) {

if ($num) {

return $num + 1;

}

return 0;

}

Peephole

Page 70: High Performance PHP - PHPNW

function blah($num) {

if ($num) {

return $num + 1;

}

return 0;

}

Peephole

Page 71: High Performance PHP - PHPNW

function blah($num) {

if ($num) {

return $num + 1;

}

return 0;

}

Peephole

Page 72: High Performance PHP - PHPNW

function blah($num) {

if ($num) {

return $num + 1;

}

return 0;

}

Peephole

Page 73: High Performance PHP - PHPNW

function blah($num) {

if ($num) {

return $num + 1;

}

return 0;

}

Peephole

Page 74: High Performance PHP - PHPNW

OptimizationsType Efficiency Potential Complexity

Peephole High Low Simple

Page 75: High Performance PHP - PHPNW

function blah($num) {

if ($num) {

return $num + 1;

}

return 0;

}

Local

Page 76: High Performance PHP - PHPNW

function blah($num) {

if ($num) {

return $num + 1;

}

return 0;

}

Local

Page 77: High Performance PHP - PHPNW

OptimizationsType Efficiency Potential Complexity

Peephole High Low Simple

Local Medium Moderate Simple

Page 78: High Performance PHP - PHPNW

function blah($num) {

if ($num) {

return $num + 1;

}

return 0;

}

Global

Page 79: High Performance PHP - PHPNW

OptimizationsType Efficiency Potential Complexity

Peephole High Low Simple

Local Medium Moderate Simple

Global Low High Complex

Page 80: High Performance PHP - PHPNW

function blah($num) {

if ($num) {

return $num + 1;

}

return 0;

}

Loop

?

Page 81: High Performance PHP - PHPNW

OptimizationsType Efficiency Potential Complexity

Peephole High Low Simple

Local Medium Moderate Simple

Global Low High Complex

Loop Medium Moderate Complex

Page 82: High Performance PHP - PHPNW

Putting It All Together

Page 83: High Performance PHP - PHPNW

Types Of CompilersAOT Tracing JIT Local JIT Region

Complexity Simple Moderate High Very High

Optimizations Static None Dynamic Dynamic+Static

Restart Cost None Moderate High High

Potential Performance (Static Code)

Best Moderate Good Better

Potential Performance(Dynamic Code)

Moderate Good Better Best

Page 84: High Performance PHP - PHPNW

Zend PHP (5.6)

PHPZend

OpCodeCache

AOT Compiler

Page 85: High Performance PHP - PHPNW

line # * op return operands-------------------------------------------- 3 0 RECV !0 4 1 ADD ~0 !0, 1 2 RETURN ~0

Zend OpCode

Page 86: High Performance PHP - PHPNW

HHVM (3.2)

PHP

HHVM OpCode

Cache

AOTCompiler

Native Machine

Code

Cache

Local JIT

Page 87: High Performance PHP - PHPNW

10: Int 1 19: CGetL2 0 21: AddO 22: RetC

HHVM OpCode (ByteCode)

Page 88: High Performance PHP - PHPNW

HippyVM

PHP

Hippy OpCode

Cache

AOTCompiler

Native Machine

Code

Cache

Tracing JIT

Page 89: High Performance PHP - PHPNW

Recki-CT + JITFu

PHP Cache

AOT Compiler

Recki IR

JIT (kindof)

Compiler

Native Machine

Code

Page 90: High Performance PHP - PHPNW

Recki-CT Graph

Page 91: High Performance PHP - PHPNW

param $1 longconst $2 numeric 1var $3 numeric+ $1 $2 $3return $3

Recki-CT IR

Page 92: High Performance PHP - PHPNW

Recki-CT + PECL

PHP

AOT Compiler

Recki IR Native Machine

Code

PECL

AOT Compiler

Page 93: High Performance PHP - PHPNW

Types Of CompilersAOT Tracing JIT Local JIT Region VM?

Zend X X

HHVM X ? X

HippyVM X X

Recki-CT X ...

V8 X ?

SpiderMonkey \ / X

Page 94: High Performance PHP - PHPNW

Performance Depends More On

Your Code Than The Engine…

Page 95: High Performance PHP - PHPNW

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

Page 96: High Performance PHP - PHPNW

function blah($num) { global $amt;}

Avoid Globals

Page 97: High Performance PHP - PHPNW

function blah($num) { $func = “foo”; $func($num);}

Avoid Dynamic Function Calls

Page 98: High Performance PHP - PHPNW

if ($blah) { function blah($num) { }}

Avoid Conditional Declarations

Page 99: High Performance PHP - PHPNW

function blah($num) { eval(...) create_function(...)}

Avoid Eval

Page 100: High Performance PHP - PHPNW

The More Dynamic The Code, The Slower

It Will Run

Page 101: High Performance PHP - PHPNW

Anthony Ferrara

joind.in/11805@ircmaxell

[email protected]

github.com/ircmaxellyoutube.com/ircmaxell