Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does PHP use opcode caches while Java compiles to bytecode files?

Tags:

php

apc

From my point of view, both PHP and Java have a similar structure. At first you write some high-level code, which then must be translated in a simpler code format to be executed by a VM. One difference is, that PHP works directly from the source code files, while Java stores the bytecode in .class files, from where the VM can load them.

Nowadays the requirements for speedy PHP execution grow, which leads people to believe that it would be better to directly work with the opcodes and not go through the compiling step each time a user hits a file.

The solution seem to be a load of so called Accelerators, which basically store the compiled results in cache and then use the cached opcodes instead of compiling again.

Another approach, done by Facebook, is to completely compile the PHP code to a different language.

So my question is, why is nobody in the PHP world doing what Java does? Are there some dynamic elements that really need to be recompiled each time or something like that? Otherwise it would be really smarter to compile everything when the code goes into production and then just work with that.

like image 546
erikbstack Avatar asked May 23 '12 08:05

erikbstack


People also ask

What is OPCode caching?

OPcache is a type of caching system that saves precompiled script bytecode in a server's memory called a cache, so each time a user visits a web page, it loads faster. Here's more detail on OPcache and how to install it for your WordPress site to speed it up.

What are PHP opcodes?

An OPCode is the numeric identifier of a single operation that can be performed by the Zend Virtual Machine (Zend VM). After PHP has scanned the human-readable source code and chunked it into tokens, these tokens are grouped together in the parsing phase.


2 Answers

The most important difference is that the JVM has an explicit specification that covers the bytecode completely. That makes bytecode files portable and useful for more than just execution by a specific JVM implementation.

PHP doesn't even have a language specification. PHP opcodes are an implementation detail of a specific PHP engine, so you can't really do anything interesting with them and there's little point in making them more visible.

like image 82
Michael Borgwardt Avatar answered Oct 23 '22 15:10

Michael Borgwardt


PHP opcodes are not the same as Java classfiles. Java classfiles are well specified, and are portable between machines. PHP opcodes are not portable in any way. They have memory addresses baked into them, for example. They are strictly an implementation detail of the PHP interpreter, and shouldn't be considered anything like Java bytecode.

Does it have to be this way? No, probably not. But the PHP source code is a mess, and there is neither the desire, nor the political will in the PHP internals community to make this happen. I think there was talk of baking an opcode cache into PHP 6, but PHP 6 died, and I don't know the status of that idea.

Reference: I wrote phc so I was pretty knee deep in PHP implementation/compilation for a few years.

like image 45
Paul Biggar Avatar answered Oct 23 '22 15:10

Paul Biggar