Magento Code change for gain better performance

After lot of work on the performance optimization work I have found we can some amount of performance by some code changes in magento core. magento to performance majorly depends on hardware selection but there are some modification in magento code which is really help me to improve performance in larger traffic website.

1. Disable unused modules  from /etc/modules/ .

2. Magento Visitor Log Changes.

Magento Visitor Logging Mage_Log and Mage_Report modules perform a write operation on every page view to own tables. After few monthes it becomes very slow, since size of the tables increasing dramatically.Remove observers of Mage_Log and Mage_Reports module by config.xml  but Drawback You will not be able to use native Magento reports for visitors activity.

3. Non optimized layout handles usage Magento adds on every category and product page a layout handle with its entity id, so you have layout cache unique for each product or category.Solution is add an observer to controller_action_layout_load_before, check loaded handles and remove items that match starts with CATEGORY_ and PRODUCT_, except PRODUCT_TYPE.

Solution Example

Of Observer

public function optimizeLayout($observer) 
{ 
    $update = $observer->getLayout()->getUpdate(); 
    foreach ($update->getHandles() as $handle) {
         if (strpos($handle, 'CATEGORY_') === 0 || (strpos($handle, 'PRODUCT_') === 0 && strpos($handle, 'PRODUCT_TYPE_') === false)) { 
             $update->removeHandle($handle); 
         } 
    } 
}

Drawback Widgets based on Category Id and Product Id handles will not work. Hovewer you always have for them “Custom Layout Update XML” attribute on entity level.

4. Remove all blocks which you actually don’t use/need from layout xml

Example how to remove the poll from the right column.

<layout version="0.1.0">
  <default>
    <reference name="right">
      <remove name="right.poll"/>
    </reference>
  </default>
</layout>

There are lot more blocks like compare product, mini cart , wishlist etc which we are really not use but it load every time when page load you can check it using developer toolbar and remove it.

5. No cache for CMS Blocks Magento doesn’t cache them by default. On average projects you have up to 10-20 CMS blocks that can be edited by customer on every page. Ivan Solution is Add an observer to core_block_abstract_to_html_before, and specify required cache information for the block, like cache key (that is a block identifier) and cache tags (Mage_Cms_Model_Block::CACHE_TAG).
Solution Example of Observer

public function optimizeCmsBlocksCache($observer) {
$block = $observer->getBlock();
if ($block instanceof Mage_Cms_Block_Widget_Block || $block instanceof Mage_Cms_Block_Block) {
    $cacheKeyData = array( 
         Mage_Cms_Model_Block::CACHE_TAG, 
         $block->getBlockId(), 
         Mage::app()->getStore()->getId()
    );
    $block->setCacheKey(implode('_', $cacheKeyData));
    $block->setCacheTags(
        array(Mage_Cms_Model_Block::CACHE_TAG)
    );
    $block->setCacheLifetime(false); 
}}

2 thoughts on “Magento Code change for gain better performance

  1. Pingback: Magento Code change for gain better performance | Magento Practice

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.