www.php-compiler.net jan benda martin maly tomas matousek ladislav prosek pavel novak vaclav novak...

12
www.php-compiler.net Jan Benda Martin Maly Tomas Matousek Ladislav Prosek Pavel Novak Vaclav Novak Tomas Matousek Tomas Matousek Department of Software Engineering Department of Software Engineering Faculty of Mathematics and Physics Faculty of Mathematics and Physics Charles University in Prague Charles University in Prague Czech Republic Czech Republic

Upload: philip-page

Post on 01-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Www.php-compiler.net Jan Benda Martin Maly Tomas Matousek Ladislav Prosek Pavel Novak Vaclav Novak Tomas Matousek Department of Software Engineering Faculty

www.php-compiler.net

Jan Benda Martin Maly

Tomas Matousek Ladislav Prosek

Pavel NovakVaclav Novak

Tomas MatousekTomas Matousek

Department of Software EngineeringDepartment of Software EngineeringFaculty of Mathematics and PhysicsFaculty of Mathematics and Physics

Charles University in PragueCharles University in Prague

Czech RepublicCzech Republic

Page 2: Www.php-compiler.net Jan Benda Martin Maly Tomas Matousek Ladislav Prosek Pavel Novak Vaclav Novak Tomas Matousek Department of Software Engineering Faculty

www.php-compiler.netTomas Matousek, .NET Technologies, 5/30/2006

Outline

• Introduction to the Phalanger System• main features and goals

• Why PHP on .NET Framework?• Performance benchmarks

• Interesting Issues• inclusions, local variables optimization, multi-version

types/functions • accessing .NET objects, adding fields on .NET objects at run-time• using PHP native libraries

• Future Work: Phalanger 2.0• PHP/CLR – extending PHP to the first-class .NET language• Support for Mono Platform

Page 3: Www.php-compiler.net Jan Benda Martin Maly Tomas Matousek Ladislav Prosek Pavel Novak Vaclav Novak Tomas Matousek Department of Software Engineering Faculty

www.php-compiler.netTomas Matousek, .NET Technologies, 5/30/2006

Phalanger

• Extends the family of .NET languages with PHP versions 4, 5, and 6• introduces language extensions for using .NET types (PHP/CLR)

• Makes it possible to run existing PHP applications on ASP.NET web server• Significant performance improvement• Higher reliability of the web server• Better configurability• More errors revealed by the compilation• Source-free application deployment

• Brings virtually all PHP libraries to .NET Framework• migration from PHP interpreter is simple in practice without additional

costs

• Enriches both worlds• PHP applications with the functionality of the .NET platform• .NET applications with a possibility to use a popular dynamic language

• Integrates PHP with Visual Studio .NET

Page 4: Www.php-compiler.net Jan Benda Martin Maly Tomas Matousek Ladislav Prosek Pavel Novak Vaclav Novak Tomas Matousek Department of Software Engineering Faculty

www.php-compiler.netTomas Matousek, .NET Technologies, 5/30/2006

PHP Sampleinclude "a.php";

class C extends D implements I, J{ public function m($a, $b = 1, $c = array(3,2,1)) { $f = "sort"; $f($c);

$x = "b"; if ($$x) { function f() { echo "version 1"; } } else { function f() { echo "version 2"; } } f();

eval('$a++;'); } private $d;

function __get($field_name) { return $d->$field_name; } function __set($field_name, $value) { $d->$field_name = $value; }}

Page 5: Www.php-compiler.net Jan Benda Martin Maly Tomas Matousek Ladislav Prosek Pavel Novak Vaclav Novak Tomas Matousek Department of Software Engineering Faculty

www.php-compiler.netTomas Matousek, .NET Technologies, 5/30/2006

Micro-benchmarks

0,000

0,200

0,400

0,600

0,800

1,000

1,200

1,400

1,600

1,800

E mpty loop

A::$x r

ead

A::$x w

rite

A::$$x re

ad

A::$$x w

rite

$x->a re

ad

$x->a w

rite

$x->$a re

ad

$x->$a w

rite

$x->a re

ad (undec la re

d)

$x->a w

rite (u

ndec lared)

$x->$a re

ad (undecla

red)

$x->$a w

rite (u

ndec la red)

A::m()

A::$m()

$x->f()

$x->$f()

$x++

$x . $y

$x[0] read

$x[0] write

$x["str"

] read

$x["str"

] write

new A (n

o ctor)

new A (e

mpty ct

or)

time

[mic

rose

cond

s]

P HP P halanger 1.0 P halanger 2.0

• each operation performed 10M times in a loop; machine: Pentium M Dothan 1.8GHz/2MB cache

• the lesser the better

Page 6: Www.php-compiler.net Jan Benda Martin Maly Tomas Matousek Ladislav Prosek Pavel Novak Vaclav Novak Tomas Matousek Department of Software Engineering Faculty

www.php-compiler.netTomas Matousek, .NET Technologies, 5/30/2006

phpBB Benchmark Results (Apache 2.0.54)

0

2

4

6

8

10

12

14

Phalan

ger m

anag

ed M

SSQL

Phalan

ger n

ative

MSSQ

L (c

olloc

ated)

Phalan

ger n

ative

MSSQ

L (is

olat

ed)

PHP 4.3

.11 M

SSQL

Zend O

ptim

izer

PHP 5.0

.4 M

SSQL

Zend O

ptimize

r

PHP 4.3

.11 M

SSQL

PHP 5.0

.4 M

SSQL

Phalan

ger n

ative

MyS

QL (collo

cate

d)

Phalan

ger n

ative

MyS

QL (iso

lated

)

PHP 4.3

.11 M

ySQ

L Zend

Opt

imize

r

PHP 5.0

.4 M

ySQL Z

end

Opt

imize

r

PHP 4.3

.11 M

ySQ

L

PHP 5.0

.4 M

ySQL

req

ue

sts

pe

r se

con

dMSDE 2000 SP3

MySQL 4.1.11

Phalanger 1.0 PHP + Zend Optimizer PHP

the greater the better

Page 7: Www.php-compiler.net Jan Benda Martin Maly Tomas Matousek Ladislav Prosek Pavel Novak Vaclav Novak Tomas Matousek Department of Software Engineering Faculty

www.php-compiler.netTomas Matousek, .NET Technologies, 5/30/2006

Major Issues

• missing specification• “correctness” criteria: existing applications should work well without

modifications

• inclusions• standard PHP mode

inclusions similar to C language; the included file needn’t to be known at compile-time

• pure mode (since Phalanger 2.0) C# like project – unconditional declarations visible in all files comprising the project

• multi-version and compile-time unknown types/functions• classes and functions can be declared conditionally in multiple

versions• classes and functions can be generated at run-time• class can extend/implement compile-time unknown class/interface

• local variables optimization• creating a hash table for each call is expensive and unnecessary• compile-time known variables can be stored in CLR locals in majority

cases

Page 8: Www.php-compiler.net Jan Benda Martin Maly Tomas Matousek Ladislav Prosek Pavel Novak Vaclav Novak Tomas Matousek Department of Software Engineering Faculty

www.php-compiler.netTomas Matousek, .NET Technologies, 5/30/2006

Using PHP Native Extensions• a necessity for a seamless migration from PHP interpreter

• PHP• Many functions and classes implemented in PHP extensions

php_imap.dll, php_pdf.dll, php_sockets.dll, php_gtk.dll, …• PHP interpreter exposes so called Zend API to extensions (php4ts.dll)

• Phalanger• Makes the functionality in PHP extensions available for any .NET app.

managed wrappers encapsulates and shields out native implementation

Extension Manager emulates the PHP interpreter environment (Zend API)

within the application process using the extension or in a separate process, isolating untrustworthy extensions

Extension

Wrapper

C++/CLIphp4ts.dll

Mixed Code php_imap.dll

php_pdf.dll

Zend API

Application (PHP, C#,

…)

optional process boundary bridged by .NET Remoting via

Shared Memory Channel

Native Code

Managed Code

Extension

Manager

Page 9: Www.php-compiler.net Jan Benda Martin Maly Tomas Matousek Ladislav Prosek Pavel Novak Vaclav Novak Tomas Matousek Department of Software Engineering Faculty

www.php-compiler.netTomas Matousek, .NET Technologies, 5/30/2006

Managed Wrappers

• Assemblies generated automatically from native extensions and optional type information in XML format

...<function returnType="int" name="socket_write" > <param type="resource" name="socket" /> <param type="string" name="buf" /> <param optional="true" type="int" name="length" /></function>

...

php_sockets.xml

...PHP_FUNCTION(socket_write){ zend_parse_parameters(...);

ZEND_FETCH_RESOURCE(...);

// ...

RETURN_LONG(...);}...

php_sockets.dll.method public hidebysig static int32 socket_write( class [PhpNetCore]PHP.Core.PhpResource socket, string buf, int32 length) cil managed{ .custom instance void PhpNetCore]PHP.Core.ImplementsFunctionAttribute ... // ... call object [PhpNetCore]PHP.Core.Externals::InvokeFunction(...) // ... ret}

.method public hidebysig static int32 socket_write( class [PhpNetCore]PHP.Core.PhpResource socket, string buf) cil managed{ .custom instance void PhpNetCore]PHP.Core.ImplementsFunctionAttribute ... // ... call object [PhpNetCore]PHP.Core.Externals::InvokeFunction(...) // ... ret}

php_sockets.mng.dll

Page 10: Www.php-compiler.net Jan Benda Martin Maly Tomas Matousek Ladislav Prosek Pavel Novak Vaclav Novak Tomas Matousek Department of Software Engineering Faculty

www.php-compiler.netTomas Matousek, .NET Technologies, 5/30/2006

Summary & Future Work

• Phalanger 1.0• goal achieved: to make existing application working under .NET• much better performance in practice• final version available for download

• Phalanger 2.0, PHP/CLR• Make PHP a first-class language for .NET

extensions to the language enabling PHP programs to work directly with .NET types, methods, delegates, events, generics, …

extending .NET classes, overload resolution, run-time fields addition, … multi-threading support for Language Integrated Query (LINQ) minimize necessary language modifications keep the dynamic nature of the language

e.g. run-time specified generic type arguments, etc.

• ASP.NET Controls in PHP etc.

Page 11: Www.php-compiler.net Jan Benda Martin Maly Tomas Matousek Ladislav Prosek Pavel Novak Vaclav Novak Tomas Matousek Department of Software Engineering Faculty

www.php-compiler.netTomas Matousek, .NET Technologies, 5/30/2006

Related Work

• Dynamic languages on .NET• IronPython

Python is a bit more dynamic e.g. replacing methods at run-time less optimizations possible

PyStone benchmark overall improvement by factor 1.84 micro-benchmark results range from 10x faster to 10x slower

• Ruby, Perl, JScript, …

• PHP compilers• Resin Quercus

PHP to Java translation, compilation to JVM byte-code• Roadsend Compiler

PHP to C translation, compilation to native code

Page 12: Www.php-compiler.net Jan Benda Martin Maly Tomas Matousek Ladislav Prosek Pavel Novak Vaclav Novak Tomas Matousek Department of Software Engineering Faculty

www.php-compiler.netTomas Matousek, .NET Technologies, 5/30/2006

Q & A