Friday 17 April 2009

Blocks everywhere but not a drop to build with.

I really don't like the blocks system in Magento. That should have been obvious from the book. The .phtml files are tightly coupled to the PHP class file which is a Block, yet the two are considerably "far apart" in the file system layout. I'm always a fan of things which are tightly coupled and work in concert being "near" each other. And, by "near" I mean that you shouldn't have 5 levels of directory separation between the two. It serves no purpose except to make people think they are very different, independent items when they are not.

So, mentally, Blocks are associated with a "module", when in reality they are simply wrappers around random .phtml scripts and should be more considered to be pure template helpers (not Mage Helpers with a capital H, but I'll come back around to this). I say that blocks are mentally associated with a module because they "live" in a module directory, so people think of them as belonging to that module. In reality, most blocks are simply facades that create a thin (but expensive) layer between the PHP scripting code in the templates and the rest of the Models.

The majority of the time, a Block is instantiated when there is no reason to have an actual instance holding the script execution. Very few times will you actually see "$this" referenced inside a Block definition. When you do, it's usually just another utility function like $this->getProductId(), which could easily be handled by a simple template array or registry pattern. So, most blocks can be transliterated into static code with very few changes, and what do we know in Magento which is usually a static class...? Helpers.

I think that 99% of all blocks could be rewritten as helpers, thus reducing the brittle nature of the relationship between .phtml files and their parent blocks. I've seen some templates be destroyed by a simple function name in a Block changing from getItem to getItems. Granted, making functions static doesn't make them impervious to name changes, but it does allow the end-developer to easily make a quick fix. It is much easier to override a static utility class which has virtually no hierarchy than an instance class which is at least 3 levels sub-classed.

So, the whole point of this post is that I started a git branch with NO BLOCKS. (Well, 3 blocks actually) And, I've seen no less than a 50% speed-up in the code (which was already 25% sped up) and a reduction in memory usage around 50%.

Take this with a grain of salt because a lot of the functionality is missing, I only kept the following blocks CMS/Page Core/Template Core/Text_List and some of the HTML page ones because I couldn't untangle them. The front page shows up a little messed up, but all the functionality is there. All the login/logout links, footer, header, products, call-outs. And that page shows 50% speed improvements. Before I had sample data installed, my front page was around 85-90 milliseconds, after installing sample data it dropped to around 120 mills, with the no blocks branch it's around 60-70 mills. Magento 1.3 clocks in around 150 mills.

No comments: