# PHP Script Compile System

- Thread: https://externals.io/message/57930
- Original archive: https://news-web.php.net/php.internals/57930
- Started by Deepak Balani on 2012-02-22T05:49:32+00:00
- Messages: 9
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #57930 — Deepak Balani — 2012-02-22T05:49:32+00:00

URL: https://externals.io/message/57930#57930

Hello all,

I am think(actually drafting) about the compilation system of PHP scripts.
I want to make a native C extension which is able to compile the scripts in
the Zend Engines opcodes and execute directly when called.

The extension may have two functions.

bool gpc_compile($source, $target): compile file to opcodes.
mixed gpc_import($target) Include file to current script.

gpc_import function accepting path to the compiled file and execute file
into the zend engine. I want to know perception of you all about this.

Thank you.
Deepak Balani

---

## #57931 — Flavius Aspra — 2012-02-22T05:55:34+00:00

URL: https://externals.io/message/57930#57931 · In reply to: #57930

> Hello all,
>
> I am think(actually drafting) about the compilation system of PHP scripts.
> I want to make a native C extension which is able to compile the scripts in
> the Zend Engines opcodes and execute directly when called.
>
> The extension may have two functions.
>
> bool gpc_compile($source, $target): compile file to opcodes.
> mixed gpc_import($target) Include file to current script.
>
> gpc_import function accepting path to the compiled file and execute file
> into the zend engine. I want to know perception of you all about this.
>
> Thank you.
> Deepak Balani
>

Hi

Do you mean something like apc_compile_file()?

I think we already have it.

Or perhaps you mean runkit?

---

## #57933 — Deepak Balani — 2012-02-22T06:07:24+00:00

URL: https://externals.io/message/57930#57933 · In reply to: #57931

No I mean persistent compilation system like

Java

HelloWorld.Java -> HelloWorld.class


HelloWorld.php -> HelloWorld.gpc

When you call

gpc_import('HelloWorld.php');

then function first look for HelloWorld.gpc if found
then include it and if not then look for HelloWorld.php or HelloWorld.inc
(whatever the setting is) if found compile it and include it else raise an
error.




On Wed, Feb 22, 2012 at 11:25 AM, Flavius Aspra <flavius.as@gmail.com>wrote:

> 
>
> > […]
> Hi
>
> Do you mean something like apc_compile_file()?
>
> I think we already have it.
>
> Or perhaps you mean runkit?

---

## #57934 — John Crenshaw — 2012-02-22T06:16:52+00:00

URL: https://externals.io/message/57930#57934 · In reply to: #57933

> -----Original Message-----
> From: Deepak Balani [mailto:wgpdeepak1989@gmail.com] 
> Sent: Wednesday, February 22, 2012 1:07 AM
> To: flavius@php.net
> Cc: internals@lists.php.net
> Subject: Re: [PHP-DEV] PHP Script Compile System
>
> No I mean persistent compilation system like
>
> Java
>
> HelloWorld.Java -> HelloWorld.class
>
>
> HelloWorld.php -> HelloWorld.gpc
>
> When you call
>
> gpc_import('HelloWorld.php');
>
> then function first look for HelloWorld.gpc if found then include it and if not then look for
> HelloWorld.php or HelloWorld.inc (whatever the setting is) if found compile it and include it else
> raise an error.

Can you explain how this is better or functionally different from the behavior of APC? APC caches bytecode this way too. Unless I've horribly misunderstood something, when you include the file APC uses the cached bytecode as long as it is available and the file was not since modified. If the file was modified APC recompiles it and caches the bytecode. Sounds like the same net result to me, except that APC is less complicated, requires no code changes, and automatically clears its own cache.

Did I miss something?

John Crenshaw
Priacta, Inc.

---

## #57935 — Rasmus Lerdorf — 2012-02-22T06:29:15+00:00

URL: https://externals.io/message/57930#57935 · In reply to: #57934

> > […]
> 
> Can you explain how this is better or functionally different from the behavior of APC? APC caches bytecode this way too. Unless I've horribly misunderstood something, when you include the file APC uses the cached bytecode as long as it is available and the file was not since modified. If the file was modified APC recompiles it and caches the bytecode. Sounds like the same net result to me, except that APC is less complicated, requires no code changes, and automatically clears its own cache.
> 
> Did I miss something?

There is also apc_bin_dump() and apc_bin_load() if you absolutely must
have something stored on disk, but assuming you are interested in the
performance aspect here, you don't want to be loading anything from disk
on a per-request basis. If the interest is along the lines of being able
to distribute obfuscated binaries or something, then this is completely
the wrong approach because it is trivial to reverse engineer unless you
add an encryption layer on top of it.

I think one thing that people miss when comparing the php compile phase
to java/c/c++ compilation is that compiling a php script to opcodes is
super fast because we don't do complicated optimization passes or any of
those things, so the performance you gain by only skipping the
compilation phase isn't actually that much unless you combine it with
also caching the op_arrays, function and class tables in memory.

Imagine needing to compile a C++ program on every request? That just
isn't feasible. Without an opcode cache, the PHP compiler runs on every
request and it is fast enough for millions of sites out there.

-Rasmus

---

## #57936 — Stas Malyshev — 2012-02-22T07:32:42+00:00

URL: https://externals.io/message/57930#57936 · In reply to: #57930

Hi!

> I am think(actually drafting) about the compilation system of PHP scripts.
> I want to make a native C extension which is able to compile the scripts in
> the Zend Engines opcodes and execute directly when called.
>
> The extension may have two functions.
>
> bool gpc_compile($source, $target): compile file to opcodes.
> mixed gpc_import($target) Include file to current script.

Just FYI: such products already exist. Besides APC that was mentioned, 
there's Zend Guard (commercial), ioncube encoder (commercial), bcompiler 
and maybe more.
Out of curiosity - why you want to do this?

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #57944 — Flavius Aspra — 2012-02-22T09:45:07+00:00

URL: https://externals.io/message/57930#57944 · In reply to: #57935

> complicated optimization passes or any of those things

Would such things be welcome/needed in the engine or as an extension?

---

## #58029 — Richard Lynch — 2012-02-24T21:33:39+00:00

URL: https://externals.io/message/57930#58029 · In reply to: #57930

> I am think(actually drafting) about the compilation system of PHP
> scripts.
> I want to make a native C extension which is able to compile the
> scripts in
> the Zend Engines opcodes and execute directly when called.
>
> The extension may have two functions.
>
> bool gpc_compile($source, $target): compile file to opcodes.
> mixed gpc_import($target) Include file to current script.
>
> gpc_import function accepting path to the compiled file and execute
> file
> into the zend engine. I want to know perception of you all about this.

It sounds intriguing to me, if it can work reliably...

But what advantage is this over an opcode cache, really?

Now, if you could compile it to binary with `make` that would be
REALLY interesting :-)

-- 
brain cancer update:
http://richardlynch.blogspot.com/search/label/brain%20tumor
Donate:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=FS9NLTNEEKWBE

---

## #58033 — Richard Lynch — 2012-02-24T21:44:51+00:00

URL: https://externals.io/message/57930#58033 · In reply to: #57944

> 
> > […]
>
> Would such things be welcome/needed in the engine or as an extension?

Note that he said "complicated" :-)

There are many trivial / easy optimizations of low-hanging fruit.

It's just the out-of-band not-in-real-time complicated ones that PHP
doesn't do, shouldn't do, and won't do.

So if you find a quick easy one, it's most welcome...

The complicated ones, not so much :-)

PS
The misinformation that opcode caches "save" compilation time
routinely rears it's ugly head.  Opcode caches save disk reads.  Using
the already-opcoded chunk is just gravy, because the cache does this
(grossly over-simplified):

if ($opcode = opcode_cache_get($filename)){ //added by opcode cache
  //do no disk I/O EXPENSIVE
  //Oh, and for gravy, don't compile it.
}
else{
  $source = file_get_contents($filename); //EXPENSIVE
  $opcode = php_compile($source);
}
php_execute($opcode);

instead of this:


if ($source = opcode_cache_get($filename)){ //added by opcode cache
  //do no disk I/O EXPENSIVE
}
else{
  $source = file_get_contents($filename); //EXPENSIVE
}
$opcode = php_compile($source); //cheap and easy small optimization
missed
php_execute($opcode);

-- 
brain cancer update:
http://richardlynch.blogspot.com/search/label/brain%20tumor
Donate:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=FS9NLTNEEKWBE
