Tags:
development1Add my vote for this tag create new tag
, view all tags

Lazy Loading

Lazy Loading is a technique for performance optimisation. The idea is not to load code modules into memory until you are sure they are going to be required.

For example, consider a plugin module called FrobnozzPlugin that requires the Perl modules TWiki::Contrib::Frob and TWiki::Contrib::Nozz to process %FROBNOZZ% tags. These two modules are not required by the TWiki core, so they are only loaded by the plugin.

Normally you would put the lines:

use TWiki::Contrib::Frobb;
use TWiki::Contrib::Nozz;
at the top of FrobnozzPlugin.pm. However, this makes the Perl compiler load these two modules each time the plugin is "discovered" by the TWiki core, whether the text to be rendered contains any %FROBNOZZ% tags or not.

To avoid this inefficiency it is better to use these modules only if they are required, by lazy loading them when the tag is seen.

use vars qw ( $initialised );

BEGIN {
    $initialised = 0;
};

sub commonTagsHandler {
    $_[0] =~ s/%FROBNOZZ%/&_handleFrobnozz()/ge;
}

sub _handleFrobnozz {
    unless ( $initialised ) {
        eval 'use TWiki::Contrib::Frobb';
        if ( $@ ) {
            TWiki::Func::writeWarning("Failed to use Frobb: $@");
            return;
        }
        eval 'use TWiki::Contrib::Nozz';
        if ( $@ ) {
            TWiki::Func::writeWarning("Failed to use Nozz: $@");
            return;
        }
        $initialised = 1;
    }
    ...
}

Lazy loading can give significant performance improvements, especially where the referenced modules are large.

-- CrawfordCurrie - 13 Aug 2004

-- WillNorris - 13 Aug 2004

on the flip side, without a good code coverage set of tests (or people that don't run those tests before commiting) lazyloading produces hidden bugs frown

-- SvenDowideit - 23 Jul 2006

Edit | Attach | Watch | Print version | History: r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r4 - 2006-07-23 - SvenDowideit
 
  • Learn about TWiki  
  • Download TWiki
This site is powered by the TWiki collaboration platform Powered by Perl Hosted by OICcam.com Ideas, requests, problems regarding TWiki? Send feedback. Ask community in the support forum.
Copyright © 1999-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.