[buddypress-dev] JIT Inclusions

Andy Peatling andypeatling at automattic.com
Tue May 5 10:27:19 GMT 2009


Something I've been looking into over the last few weeks is something  
I like to call "JIT inclusions" or "Just In Time" inclusions. I  
thought it would be a nice topic to re-open up this mailing list with.

Here's the challenge BuddyPress faces with the way it works right now.

Each component needs to set up its own environment. It needs its own  
template tags, filters, DB access classes and so on. As it stands in  
BuddyPress, each component loads these in as soon as the component is  
initialized. When you multiply this by 8 components, you're loading a  
lot of code into memory on each page load. This means BuddyPress has a  
fairly large memory footprint, even if you're doing nothing.

What I think needs to happen is this JIT idea, where a file that  
contains code that is needed for a function is only included right  
before it is *actually* needed.

Some of this could be handled by adding require_once() calls within  
functions, so they are only loaded when that function is called. This  
won't make a huge difference overall though, and I don't think  
littering the code with require_once() calls is a solid approach.

What really needs to happen is some sort of system where when a  
function is called, a check is done to see if this function exists, if  
not, the correct file is included so the function can be loaded.

Think about this scenario:

Template tags for components can be used anywhere, on any page, under  
any slug. How do we make those template tags available for use without  
loading all component template tag files on every page?

With the system I envision, when you call the bp_has_site_members()  
loop, an action would fire that would automatically include the 'bp- 
core/bp-core-templatetags.php' file that this template loop resides  
in. That way we are only including the files we need just in time for  
the functions within them to run.

Is any of this possible? Possibly, but possibly not. The system would  
have to maintain a list of all functions, and in which files they  
reside in. This in itself may be too inefficient. It would also have  
to handle some sort of function_exists() call, so that PHP doesn't  
trigger a fatal error before we even have a chance to fetch the  
correct file.

I know that some PHP frameworks use naming conventions to handle at  
least some of this. In PHP5 there is also autoload for classes (http://www.php.net/manual/en/language.oop5.autoload.php 
) which would be great for DB access, but we need to stick to  
supporting PHP4 for now. Perhaps there is a way we can mimic this  
functionality though?

My question is, does anyone have any experience, or smart ideas  
surrounding this?

Andy

---------
Andy Peatling | Social Engineer | Automattic
http://apeatling.wordpress.com


More information about the buddypress-dev mailing list