views:

178

answers:

4

On a site where 90% of the pages use the same libraries, should you just load the libraries all the time or only load them when needed? The other pages would be ajax or simple pages that don't have any real functionality.

Also, should you only load the code when needed? If part way down a page you need a library, should you load it then or just load it at the top. Maybe it's possible it may never get there before of an error or wrong data. (Loading at the top makes it somewhat easier to understand, but may result in extra code not needed.)

I'm also wondering if I should make the libraries more specific so I'm not say loading the code to edit at the same time as viewing?

Basically, how much should I worry about loading code or not loading code?

+8  A: 

I would always try to give a file, class, and method a single responsibility. Because of that, separating the displaying from the editing code could be a good idea in either case.

As for loading libraries, I believe that the performance loss of including non required libraries could be quite irrelevant in a lot of cases. However, include, require, include_once, and require_once are relatively slow as they (obviously) access the file system. If the libraries you do not use on each occasion are quite big and usually include a lot of different files themselves, removing unnecessary includes could help reducing the time spent there. Nonetheless, this cost could also be reduced drastically by using an efficient caching system.

Given you are on PHP5 and your libraries are nicely split up into classes, you could leverage PHP's auto loading functionality which includes required classes as the PHP script needs them. That would pretty effectively avoid a lot of non used code to be included.

Finally, if you make any of those changes which could affect your website's performance, run some benchmarks and profile the gain or loss in performance. That way, you do not run into the risk of doing some possibly cool optimization which just costs too much time to fully implement or even degrades performance.

hangy
I am not expert on the field, but yesterday I saw precisely lot of remarks that some (big!) frameworks (like Zend one) are slowed down by their extensive use of require_once instead of autoload. Of course, you need to reach some critical mass to see this.
PhiLho
require_once was discussed here (http://stackoverflow.com/questions/186338) btw. Anyway, splitting up the classes into appropriate files and using __autoload with require should be a good and stable way to go.
hangy
+3  A: 

Bear in mind that each script that is loaded gets parsed as PHP is compiled at run-time, so there is a penalty for loading unneeded scripts. This might be minor depending on your application structure and requirements, but there are cases which this is not the case.

There are two things you can do to negate such concerns:

  1. Use __autoload to load your scripts as they are needed. This removes the need to maintain a long 'require' list of scripts and only loads what's needed for the current run.
  2. Use APC as a byte-code cache to lower the cost of loading scripts. APC caches scripts in their compiled state and will do wonders for your application performance.
Eran Galperin
+2  A: 

+1 Vote for the autoload technique.

The additional benefit of using autoload is it eliminates some of the potential for abusive code. If something fails, pop a back-trace and an "included_files" list and you get a list of places where the problem could come from.

This means you have less files to hunt through if somebody hides malicious code at the end of one of them, or designs something fruity.

I worked on a codebase once ( not mine ) where the presence of certain tokens in the URL caused unexpected behaviour, and because the code was horrible, it was a nightmare tracking the origin of the problem burried in the fact in one of the 200 included files one of them was rewriting the entire request and then calling "die"

Kent Fredric
A: 

The question was "how important".

Answer: it is NOT important at all. If you don't have a dozen servers running this app already, then this is probably early optimization, and as we all know, early optimization is the root of all evil.

In other words: don't even worry about it. There are a lot of other things to optimize speed before you should even consider this.