111

I know you can minify PHP, but I'm wondering if there is any point. PHP is an interpreted language so will run a little slower than a compiled language. My question is: would clients see a visible speed improvement in page loads and such if I were to minify my PHP?

Also, is there a way to compile PHP or something similar?

Bojangles
  • 99,427
  • 50
  • 170
  • 208

8 Answers8

169

PHP is compiled into bytecode, which is then interpreted on top of something resembling a VM. Many other scripting languages follow the same general process, including Perl and Ruby. It's not really a traditional interpreted language like, say, BASIC.

There would be no effective speed increase if you attempted to "minify" the source. You would get a major increase by using a bytecode cache like APC.

Facebook introduced a compiler named HipHop that transforms PHP source into C++ code. Rasmus Lerdorf, one of the big PHP guys did a presentation for Digg earlier this year that covers the performance improvements given by HipHop. In short, it's not too much faster than optimizing code and using a bytecode cache. HipHop is overkill for the majority of users.

Facebook also recently unveiled HHVM, a new virtual machine based on their work making HipHop. It's still rather new and it's not clear if it will provide a major performance boost to the general public.

Just to make sure it's stated expressly, please read that presentation in full. It points out numerous ways to benchmark and profile code and identify bottlenecks using tools like xdebug and xhprof, also from Facebook.


2021 Update

HHVM diverged away from vanilla PHP a couple versions ago. PHP 7 and 8 bring a whole bunch of amazing performance improvements that have pretty much closed the gap. You now no longer need to do weird things to get better performance out of PHP!

Minifying PHP source code continues to be useless for performance reasons.

Charles
  • 50,943
  • 13
  • 104
  • 142
  • 2
    Note that the whole bytecode/vm thing doesn't actually buy you anything without an external(!) bytecode cache. I don't get why PHP keeps throwing the bytecode away by default... –  Nov 02 '10 at 17:25
  • I imagine that it's a shared hosting issue. APC is included in PHP by default as of the old 6.0-based trunk. I'm not sure if we'll see it by default in 5.4 or whatever the new trunk ends up being called... – Charles Nov 02 '10 at 17:27
  • Thanks very much for the info. Unfortunately I don't have deep control of my server (it's rented). Is it likely web hosts use APC? – Bojangles Nov 02 '10 at 17:29
  • 3
    Most shared hosting providers do not use APC. You should look into a Virtual Private Server so that you can control the configuration. VPSes are often more expensive than normal shared hosting, but far less expensive than a real dedicated server. Popular VPS providers include Slicehost and Linode. Don't forget that you should be benchmarking and profiling your code *first*! – Charles Nov 02 '10 at 17:35
  • Is there any benefit to making the code more compact? Will it take less time for say AJAX to send a request to the file and get a response? – electrikmilk Nov 18 '20 at 19:44
16

Forgo the idea of minifying PHP in favor of using an opcode cache, like PHP Accelerator, or APC.

Or something else like memcached

Stephen
  • 18,827
  • 9
  • 60
  • 98
  • 3
    it is not clear from that you mean something other than a opcode cache. – Treffynnon Nov 02 '10 at 16:58
  • 5
    memcached is a perfectly cromulent way to improve performance... after performing benchmarking and profiling and establishing that caching itself will be the best possible performance gain. – Charles Nov 02 '10 at 16:58
  • 1
    if you're using PHP 5.5, you don't need to install any opcode cache because it comes now with it by default. – firewall Jan 21 '15 at 12:58
  • 1
    I actually looked up cromulent :(. I agree with the others, using memcached in this context is misleading. Caching data as part of the compilation process is not the same as the resulting op code. – Mike Purcell Oct 01 '15 at 19:07
6

Yes there is one (non-technical) point.

Your hoster can spy your code on his server. If you minify and uglify it, it is for spys more difficult to steal your ideas.

One reason for minifying and uglifying php may be spy-protection. I think uglyfing code should one step in an automatic deployment.

Dieter Porth
  • 61
  • 1
  • 1
3

This is less an answer than an advertisement. I'm been working on a PHP extension that translates Zend opcodes to run on a VM with static typing. It doesn't accelerate arbitrary PHP code. It does allow you to write code that run way faster than what regular PHP allows. The key here is static typing. On a modern CPU, a dynamic language eats branch misprediction penalty left and right. Fact that PHP arrays are hash tables also imposes high cost: lot of branch mispredictions, inefficient use of cache, poor memory prefetching, and no SIMD optimization whatsoever. Branch misprediction and cache misses in particular are achilles' heel for today's processors. My little VM sidesteps those problem by using static types and C array instead of hash table. The result ends up running roughly ten times faster. This is using bytecode interpretation. The extension can optionally compile a function through gcc. In that case, you get two to five times more speed.

Here's the link for anyone interested:

https://github.com/chung-leong/qb/wiki

Again, the extension is not a general PHP accelerator. You have to write code specific for it.

cleong
  • 7,242
  • 4
  • 31
  • 40
3

With some rewriting (shorter variable names) you could save a few bytes of memory, but that's also seldomly significant.

However I do design some of my applications in a way that allows to concatenate include scripts together. With php -w it can be compacted significantly, adding a little speed gain for script startup. On an opcode-enabled server this however only saves a few file mtime checks.

mario
  • 144,265
  • 20
  • 237
  • 291
1

You don't need to minify PHP. In order to get a better performance, install an Opcode cache; but the ideal solution would be to upgrade your PHP to the 5.5 version or above because the newer versions have an opcode cache by default called Zend Optimiser that is performing better than the other ones http://massivescale.blogspot.com/2013/06/php-55-zend-optimiser-opcache-vs-xcache.html.

firewall
  • 638
  • 1
  • 7
  • 23
1

There are PHP compilers... see this previous question for a list; but (unless you're the size of Facebook or are targetting your application to run client-side) they're generally a lot more trouble than they're worth

Simple opcode caching will give you more benefit for the effort involved. Or profile your code to identify the bottlenecks, and then optimise it.

Community
  • 1
  • 1
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
1

The "point" is to make the file smaller, because smaller files load faster than bigger files. Also, removing whitespace will make parsing a tiny bit faster since those characters don't need to be parsed out.

Will it be noticeable? Almost never, unless the file is huge and there's a big difference in size.

Danial
  • 1,496
  • 14
  • 15