Archive

Posts Tagged ‘optimization’

Introducing LayerCache

August 26th, 2009

I’ve recently started a new project, called LayerCache. It’s an easy-to-use caching framework for PHP5, which allows you to cache items in multiple layers. When a requested item isn’t present in one layer, the framework reads from the next layer in the stack. If the item isn’t present in any layer, it’s retrieved from the source and stored in all caches in the stack.

An example of usage would be caching user profiles in Memcache (bigger, slower, global to all webservers) and APC (smaller, faster, local to each webserver). It also offers a mechanism called prefetch, which aims to reduce the database hit when an item is removed from the cache because of age (ttl).

Currently, the framework offers interfaces to Memcache, APC, file caching and local caching (PHP array with LRU, caching in a request scope). I’ll probably add more caching interfaces support as the project evolves. There is no documentation, apart from examples (see link bellow) and unit tests (yes, that counts as documentation). The current code is fully tested with PHPUnit, but no stable package is currently available.

For more, visit the following links:

Avoid micro-optimizations

March 10th, 2009

I’ve read this post about micro-optimization today, and I think many of the listed hints are wrong. Not wrong in way of being false, but in a way that you shouldn’t use them. Sebastian already wrote something about it, but more can be said.

The difference between static and object method call isn’t about speed at all, it’s about what you’re dealing with and what you’re trying to do. If you have an object, you call an object method. If you have a class with a static method, you call the static method. You shouldn’t choose between the two just for the sake of speed. You shouldn’t even have the option of choosing. They’re not interchangeable. And, in most cases you should be using objects, not classes, for testability’s sake.

A similar case can be made for “accessing a global variable is faster then an object property”. If I start developing with this hint in mind, and I start putting object properties in the global scope, what I am going to get? A mess, that’s what. You should never think about putting an object property in the global scope, for any reason, optimization included.

Another example would be “an array is a faster alternative to a class with several fields”. (Should be object, not class.) Again, this makes little sense. An array is a hash-like storage of data, an object is a black box that receives and sends messages, it’s encapsulated data with behavior attached. These two are two different things, and if I change the code to use arrays instead of objects, the change ripples throughout the rest of the code. Again, this leads to messy code. Not to mention that in the spirit of OOP you should be aiming to use objects, not arrays. Sure, there are cases where this hint would be suitable, but optimization like this should be the last thing you think about.

There are more hints like this, but I’m not going to list them all, because I’ve made my point: most of the hints are invalid, because they compare non-interchangeable things. If I push it a little, it’s almost like saying: not using echo is faster than using it. Yes, it’s completely true, but these two options don’t do the same thing.

I can’t help myself, so I’ll say a little something about the famous PHP quotes optimization: single quotes are faster than double quotes. First, these two aren’t the same, so changing one to another ripples out. Second, with quotes it’s only about readability and taste. Third, the speed gain is literally negligible. This is another hint you should forget as soon as possible. A second spent on quotes optimization is a second lost. I just lost a few minutes writing this, but some readers will hopefully gain them by not bothering about optimizing quotes.

A lot can be said about optimization, but most of these hints sure aren’t worth remembering. Even more; forget them as soon as possible. If you have a speed problem, find out where it is, and fix it. In the vast majority of the time it’s IO-related resources: the database and files, maybe network shares, or whatnot. I’ve never seen a real-life problem where for statement was causing problems and foreach solved them. This just doesn’t happen, except maybe if you’re computing Pi in php-cli.

The path to optimization should be:

  1. Write working code, no matter how slow it is. It’s a million times better than fast code with bugs.
  2. If and only if you undoubtedly have performance issues, profile your code, locate and measure slow code.
  3. Optimize the slowest thing. Only the slowest thing.
  4. Loop.

Also keep in your mind that by optimizing your code, you reduce readability, and consequently maintainability. This means that you’ll lose more time the next time you come back to update or fix the code.

There are also some good points that Alex makes, so I’ll repeat them here, because these are worth remembering:

  • use prepared database statements,
  • avoid @ (error control operator),
  • avoid notices and warnings in your scripts.
Author: Categories: Thoughts Tags: ,