Lazy-loading as an optimization possibility!!!

NOTE:For system specs see here

Use Lazy_Loader for import of modules during “startup”, so as to prevent import of module which are not necessary and explore the possibility of possible decrease in startup time.

What is lazy-loading?

What lazy-loading does is that, it loads a module only when it’s attributes are acccessed.This could be very important for applications that import a hell lot of stuff at start and thus could help lower their startup time.

Following links describe the status of imports in python presently:

  • PEP 302 first to propose import_hooks
  • But importlib much better interface to write custom modules

And following for lazy-loading:

  • Proposal of lazy import keyword here
  • Also importlib.util provides LazyLoader class for creating “lazy” instances here

Effect on startup time

Following two possibilties were to be explored:- 1) If lazy-loading could actually be done so as to decrease the “startup time” by restricting import only to modules whose attributes are actually acccessed. 2) There might be some implicit imports which do not come up, so atleast detect them.

I wrote a lazy_import_module function and here’s its comparision with a nonlazy_import_module.

Written on June 25, 2017