views:

277

answers:

7

Hi everybody.

I'm writing a Content Management System in PHP, and I want it to be the smallest one in the world. I'm planning to make it available to everyone, just like Drupal and Joomla. But to make it so ultra-tiny, I change code to smaller code.

For example, I change:

$info = parse_ini_file("info.scm"); /* to */ $i=parse_ini_file("info.scm");

just to make it smaller. But, I use some functions very often, like preg_replace();. I use it over 30 times. Should I make a function like:

function p($p,$r,$s){preg_replace($p,$r,$s);}
//and than just use:
p($my_regex, $my_replacement, $my_string);

or does this make it all work slower?

Thanks in advance.

Notice that my goal is to make it so tiny as possible.

+7  A: 

What you are attempting to do is known as 'minifying' it has no performance changes, just reduces the size of your program. Yes making that function will reduce the BYTE size of your program.

I do suggest though keeping a non minified version in order to make it more maintainable. perhaps write a program to minify it before you release a new version.

+15  A: 

It makes sense when it is code you need to push to a client (JavaScript from a server for example) but when it runs locally on the server, as is the case for php, there is little (if any) performance gain.

Jesse
right, and the second examle might even be slower i think, as the same functions are called anyways but you also call a second one.
Flo
+4  A: 

Wrapping an existing function in a own function hurts performance due the function call overhead.

If you really care about the package size, deliver your CMS as an Phar-Archive. These can be bzip2-compressed and can be directly executed. See http://www.php.net/manual/en/phar.using.intro.php to see how to use and create Phar-Archives.

sebasgo
+1  A: 

minifying has no real impact on performance on the server side. Writing less code will not improve speed, and if you plan to redistribute your code, well it will attract very few attention since the code will not be understandable.

Compiling your code will also significantly increase it's speed, but I don't think people will enjoy reading and writing bytecode in binary mode to edit you app! The only way of reducing size and increasing speed while keeping readable code is to use less memory: instead of assigning a value to a variable and then pass it to a function, pass it directly to a function.

Also keep in mind that PHP built-in functions are faster than user-defined functions.

xav0989
Smaller code means less hard disc usage, so better performance.
Time Machine
@Radeksonic: Not true, as was pointed out in another answer, your "minified" preg_replace has extra function call overhead.
Travis Beale
+5  A: 

Don’t. Please don’t. Let me emphasize that for you: Please don’t. There’s already way too much unreadable code around that is unreadable for exactly the same reasons you are giving. Hard disk space on servers is (virtually) unlimited, nobody cares about the size of your PHP files.

Bombe
I include the source, and the minified source in the package. LOLZ.
Time Machine
A: 

before APC exists this use to be a common technique:

  • remove comments
  • minify functions names
  • etc.

because each time you ran the script php shall parse it and process it.

You need consider that now, APC creates a bytecode and php doesn't need to parse each time the script runs. I would recommend you: keep your files well documented, so any other developer can see what you tried to do. use coherent functions names that can describe them.

Gabriel Sosa
A: 

If you do this, please don't release the source code in any form - keep it as a personal project, for your own interest. Trying to reduce the footprint of a body of code to the maximum degree possible is not entirely without merit (as a learning exercise), but it will be very difficult for anyone else to work with, and you won't win many friends with it :)

Bobby Jack