Lazy initialization

Last updated

In computer programming, lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed. It is a kind of lazy evaluation that refers specifically to the instantiation of objects or other resources.

Contents

This is typically accomplished by augmenting an accessor method (or property getter) to check whether a private member, acting as a cache, has already been initialized. If it has, it is returned straight away. If not, a new instance is created, placed into the member variable, and returned to the caller just-in-time for its first use.

If objects have properties that are rarely used, this can improve startup speed. Mean average program performance may be slightly worse in terms of memory (for the condition variables) and execution cycles (to check them), but the impact of object instantiation is spread in time ("amortized") rather than concentrated in the startup phase of a system, and thus median response times can be greatly improved.

In multithreaded code, access to lazy-initialized objects/state must be synchronized to guard against race conditions.

The "lazy factory"

In a software design pattern view, lazy initialization is often used together with a factory method pattern. This combines three ideas:

Examples

ActionScript 3

The following is an example of a class with lazy initialization implemented in ActionScript:

packageexamples.lazyinstantiation{publicclassFruit{privatevar_typeName:String;privatestaticvarinstancesByTypeName:Dictionary=newDictionary();publicfunctionFruit(typeName:String):void{this._typeName=typeName;}publicfunctiongettypeName():String{return_typeName;}publicstaticfunctiongetFruitByTypeName(typeName:String):Fruit{returninstancesByTypeName[typeName]||=newFruit(typeName);}publicstaticfunctionprintCurrentTypes():void{foreach(varfruit:FruitininstancesByTypeName){// iterates through each valuetrace(fruit.typeName);}}}}

Basic use:

package{importexamples.lazyinstantiation;publicclassMain{publicfunctionMain():void{Fruit.getFruitByTypeName("Banana");Fruit.printCurrentTypes();Fruit.getFruitByTypeName("Apple");Fruit.printCurrentTypes();Fruit.getFruitByTypeName("Banana");Fruit.printCurrentTypes();}}}

C

In C, lazy evaluation would normally be implemented inside one function, or one source file, using static variables.

In a function:

#include<stddef.h>#include<stdlib.h>#include<stdio.h>#include<string.h>typedefstructFruit{char*name;structFruit*next;intnumber;/* Other members */}Fruit;Fruit*getFruit(char*name){staticFruit*fruitList;staticintseq;Fruit*f;for(f=fruitList;f;f=f->next){if(!strcmp(name,f->name)){returnf;}}if(!(f=malloc(sizeof(Fruit)))){returnNULL;}if(!(f->name=strdup(name))){free(f);returnNULL;}f->number=++seq;f->next=fruitList;fruitList=f;returnf;}/* Example code */intmain(intargc,char*argv[]){Fruit*f;if(argc<2){fprintf(stderr,"Usage: fruits fruit-name [...]\n");return1;}for(inti=1;i<argc;i++){if((f=getFruit(argv[i]))){printf("Fruit %s: number %d\n",argv[i],f->number);}}return0;}

Using one source file instead allows the state to be shared between multiple functions, while still hiding it from non-related functions.

Fruit.h:

#pragma oncetypedefstructFruit{char*name;structFruit*next;intnumber;/* Other members */}Fruit;Fruit*getFruit(char*name);voidprintFruitList(FILE*file);

Fruit.c:

#include<stddef.h>#include<stdlib.h>#include<stdio.h>#include<string.h>#include"Fruit.h"staticFruit*fruitList;staticintseq;structFruit*getFruit(char*name){Fruit*f;for(f=fruitList;f;f=f->next){if(!strcmp(name,f->name)){returnf;}}if(!(f=malloc(sizeof(Fruit)))){returnNULL;}if(!(f->name=strdup(name))){free(f);returnNULL;}f->number=++seq;f->next=fruitList;fruitList=f;returnf;}voidprintFruitList(FILE*file){for(Fruit*f=fruitList;f;f=f->next){fprintf(file,"%4d  %s\n",f->number,f->name);}}

Main.c:

#include<stdlib.h>#include<stdio.h>#include"Fruit.h"intmain(intargc,char*argv[]){Fruit*f;if(argc<2){fprintf(stderr,"Usage: fruits fruit-name [...]\n");return1;}for(inti=1;i<argc;i++){if((f=getFruit(argv[i]))){printf("Fruit %s: number %d\n",argv[i],f->number);}}printf("The following fruits have been generated:\n");printFruitList(stdout);return0;}

C#

In .NET Framework 4.0 Microsoft has included a Lazy class that can be used to do lazy loading. Below is some dummy code that does lazy loading of Class Fruit

varlazyFruit=newLazy<Fruit>();Fruitfruit=lazyFruit.Value;

Here is a dummy example in C#.

The Fruit class itself doesn't do anything here, The class variable _typesDictionary is a Dictionary/Map used to store Fruit instances by typeName.

usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;publicclassFruit{privatestring_typeName;privatestaticIDictionary<string,Fruit>_typesDictionary=newDictionary<string,Fruit>();privateFruit(stringtypeName){this._typeName=typeName;}publicstaticFruitGetFruitByTypeName(stringtype){Fruitfruit;if(!_typesDictionary.TryGetValue(type,outfruit)){// Lazy initializationfruit=newFruit(type);_typesDictionary.Add(type,fruit);}returnfruit;}publicstaticvoidShowAll(){if(_typesDictionary.Count>0){Console.WriteLine("Number of instances made = {0}",_typesDictionary.Count);foreach(KeyValuePair<string,Fruit>kvpin_typesDictionary){Console.WriteLine(kvp.Key);}Console.WriteLine();}}publicFruit(){// required so the sample compiles}}classProgram{staticvoidMain(string[]args){Fruit.GetFruitByTypeName("Banana");Fruit.ShowAll();Fruit.GetFruitByTypeName("Apple");Fruit.ShowAll();// returns pre-existing instance from first // time Fruit with "Banana" was createdFruit.GetFruitByTypeName("Banana");Fruit.ShowAll();Console.ReadLine();}}

A fairly straightforward 'fill-in-the-blanks' example of a Lazy Initialization design pattern, except that this uses an enumeration for the type

namespaceDesignPatterns.LazyInitialization;publicclassLazyFactoryObject{// internal collection of items// IDictionary makes sure they are uniqueprivateIDictionary<LazyObjectSize,LazyObject>_LazyObjectList=newDictionary<LazyObjectSize,LazyObject>();// enum for passing name of size required// avoids passing strings and is part of LazyObject aheadpublicenumLazyObjectSize{None,Small,Big,Bigger,Huge}// standard type of object that will be constructedpublicstructLazyObject{publicLazyObjectSizeSize;publicIList<int>Result;}// takes size and create 'expensive' listprivateIList<int>Result(LazyObjectSizesize){IList<int>result=null;switch(size){caseLazyObjectSize.Small:result=CreateSomeExpensiveList(1,100);break;caseLazyObjectSize.Big:result=CreateSomeExpensiveList(1,1000);break;caseLazyObjectSize.Bigger:result=CreateSomeExpensiveList(1,10000);break;caseLazyObjectSize.Huge:result=CreateSomeExpensiveList(1,100000);break;caseLazyObjectSize.None:result=null;break;default:result=null;break;}returnresult;}// not an expensive item to create, but you get the point// delays creation of some expensive object until neededprivateIList<int>CreateSomeExpensiveList(intstart,intend){IList<int>result=newList<int>();for(intcounter=0;counter<(end-start);counter++){result.Add(start+counter);}returnresult;}publicLazyFactoryObject(){// empty constructor}publicLazyObjectGetLazyFactoryObject(LazyObjectSizesize){// yes, i know it is illiterate and inaccurateLazyObjectnoGoodSomeOne;// retrieves LazyObjectSize from list via out, else creates one and adds it to listif(!_LazyObjectList.TryGetValue(size,outnoGoodSomeOne)){noGoodSomeOne=newLazyObject();noGoodSomeOne.Size=size;noGoodSomeOne.Result=this.Result(size);_LazyObjectList.Add(size,noGoodSomeOne);}returnnoGoodSomeOne;}}

C++

This example is in C++.

importstd;classFruit{private:staticstd::map<std::string,Fruit*>types;std::stringtype_;// Note: constructor private forcing one to use static |GetFruit|.Fruit(conststd::string&type):type_{type}{}public:// Lazy Factory method, gets the |Fruit| instance associated with a certain// |type|.  Creates new ones as needed.staticFruit*getFruit(conststd::string&type){auto[it,inserted]=types.emplace(type,nullptr);if(inserted){it->second=newFruit(type);}returnit->second;}// For example purposes to see pattern in action.staticvoidprintCurrentTypes(){std::println("Number of instances made = {}",types.size());for(constauto&[type,fruit]:types){std::println({},type);}std::println();}};// staticstd::map<std::string,Fruit*>Fruit::types;intmain(intargc,char*argv[]){Fruit::getFruit("Banana");Fruit::printCurrentTypes();Fruit::getFruit("Apple");Fruit::printCurrentTypes();// Returns pre-existing instance from first time |Fruit| with "Banana" was// created.Fruit::getFruit("Banana");Fruit::printCurrentTypes();}// OUTPUT://// Number of instances made = 1// Banana//// Number of instances made = 2// Apple// Banana//// Number of instances made = 2// Apple// Banana//

Crystal

classFruitprivategettertype:String@@types={}ofString=>Fruitdefinitialize(@type)enddefself.get_fruit_by_type(type:String)@@types[type]||=Fruit.new(type)enddefself.show_allputs"Number of instances made: #{@@types.size}"@@types.eachdo|type,fruit|puts"#{type}"endputsenddefself.size@@types.sizeendendFruit.get_fruit_by_type("Banana")Fruit.show_allFruit.get_fruit_by_type("Apple")Fruit.show_allFruit.get_fruit_by_type("Banana")Fruit.show_all

Output:

Number of instances made: 1 Banana  Number of instances made: 2 Banana Apple  Number of instances made: 2 Banana Apple 

Haxe

This example is in Haxe. [1]

classFruit{privatestaticvar_instances=newMap<String,Fruit>();publicvarname(default,null):String;publicfunctionnew(name:String){this.name=name;}publicstaticfunctiongetFruitByName(name:String):Fruit{if(!_instances.exists(name)){_instances.set(name,newFruit(name));}return_instances.get(name);}publicstaticfunctionprintAllTypes(){trace([for(keyin_instances.keys())key]);}}

Usage

classTest{publicstaticfunctionmain(){varbanana=Fruit.getFruitByName("Banana");varapple=Fruit.getFruitByName("Apple");varbanana2=Fruit.getFruitByName("Banana");trace(banana==banana2);// true. same bananaFruit.printAllTypes();// ["Banana","Apple"]}}

Java

This example is in Java.

importjava.util.HashMap;importjava.util.Map;importjava.util.Map.Entry;publicclassProgram{/**     * @param args     */publicstaticvoidmain(String[]args){Fruit.getFruitByTypeName(FruitType.banana);Fruit.showAll();Fruit.getFruitByTypeName(FruitType.apple);Fruit.showAll();Fruit.getFruitByTypeName(FruitType.banana);Fruit.showAll();}}enumFruitType{none,apple,banana,}classFruit{privatestaticMap<FruitType,Fruit>types=newHashMap<>();/**     * Using a private constructor to force the use of the factory method.     * @param type     */privateFruit(FruitTypetype){}/**     * Lazy Factory method, gets the Fruit instance associated with a certain     * type. Instantiates new ones as needed.     * @param type Any allowed fruit type, e.g. APPLE     * @return The Fruit instance associated with that type.     */publicstaticFruitgetFruitByTypeName(FruitTypetype){Fruitfruit;// This has concurrency issues.  Here the read to types is not synchronized, // so types.put and types.containsKey might be called at the same time.// Don't be surprised if the data is corrupted.if(!types.containsKey(type)){// Lazy initialisationfruit=newFruit(type);types.put(type,fruit);}else{// OK, it's available currentlyfruit=types.get(type);}returnfruit;}/**     * Lazy Factory method, gets the Fruit instance associated with a certain     * type. Instantiates new ones as needed. Uses double-checked locking      * pattern for using in highly concurrent environments.     * @param type Any allowed fruit type, e.g. APPLE     * @return The Fruit instance associated with that type.     */publicstaticFruitgetFruitByTypeNameHighConcurrentVersion(FruitTypetype){if(!types.containsKey(type)){synchronized(types){// Check again, after having acquired the lock to make sure// the instance was not created meanwhile by another threadif(!types.containsKey(type)){// Lazy initialisationtypes.put(type,newFruit(type));}}}returntypes.get(type);}/**     * Displays all entered fruits.     */publicstaticvoidshowAll(){if(types.size()>0){System.out.println("Number of instances made = "+types.size());for(Entry<FruitType,Fruit>entry:types.entrySet()){Stringfruit=entry.getKey().toString();fruit=Character.toUpperCase(fruit.charAt(0))+fruit.substring(1);System.out.println(fruit);}System.out.println();}}}

Output

Number of instances made = 1 Banana  Number of instances made = 2 Banana Apple  Number of instances made = 2 Banana Apple 

JavaScript

This example is in JavaScript.

varFruit=(function(){vartypes={};functionFruit(){};// count own properties in objectfunctioncount(obj){returnObject.keys(obj).length;}var_static={getFruit:function(type){if(typeoftypes[type]=='undefined'){types[type]=newFruit;}returntypes[type];},printCurrentTypes:function(){console.log('Number of instances made: '+count(types));for(vartypeintypes){console.log(type);}}};return_static;})();Fruit.getFruit('Apple');Fruit.printCurrentTypes();Fruit.getFruit('Banana');Fruit.printCurrentTypes();Fruit.getFruit('Apple');Fruit.printCurrentTypes();

Output

Number of instances made: 1 Apple  Number of instances made: 2 Apple Banana  Number of instances made: 2 Apple Banana 

PHP

Here is an example of lazy initialization in PHP 7.4:

<?phpheader('Content-Type: text/plain; charset=utf-8');classFruit{privatestring$type;privatestaticarray$types=array();privatefunction__construct(string$type){$this->type=$type;}publicstaticfunctiongetFruit(string$type):Fruit{// Lazy initialization takes place hereif(!isset(self::$types[$type])){self::$types[$type]=newFruit($type);}returnself::$types[$type];}publicstaticfunctionprintCurrentTypes():void{echo'Number of instances made: '.count(self::$types)."\n";foreach(array_keys(self::$types)as$key){echo"$key\n";}echo"\n";}}Fruit::getFruit('Apple');Fruit::printCurrentTypes();Fruit::getFruit('Banana');Fruit::printCurrentTypes();Fruit::getFruit('Apple');Fruit::printCurrentTypes();/*OUTPUT:Number of instances made: 1AppleNumber of instances made: 2AppleBananaNumber of instances made: 2AppleBanana*/

Python

This example is in Python.

classFruit:def__init__(self,item:str):self.item=itemclassFruitCollection:def__init__(self):self.items={}defget_fruit(self,item:str)->Fruit:ifitemnotinself.items:self.items[item]=Fruit(item)returnself.items[item]if__name__=="__main__":fruits=FruitCollection()print(fruits.get_fruit("Apple"))print(fruits.get_fruit("Lime"))

Ruby

This example is in Ruby, of lazily initializing an authentication token from a remote service like Google. The way that @auth_token is cached is also an example of memoization.

require'net/http'classBloggerdefauth_token@auth_token||=(res=Net::HTTP.post_form(uri,params))&&get_token_from_http_response(res)end# get_token_from_http_response, uri and params are defined later in the classendb=Blogger.newb.instance_variable_get(:@auth_token)# returns nilb.auth_token# returns tokenb.instance_variable_get(:@auth_token)# returns token

Rust

Rust have std::cell::LazyCell. [2]

usestd::cell::LazyCell;letlazy:LazyCell=LazyCell::new(||42);

Scala

Scala has built-in support for lazy variable initiation. [3]

scala>valx={println("Hello");99}Hellox:Int=99scala>lazyvaly={println("Hello!!");31}y:Int=<lazy>scala>yHello!!res2:Int=31scala>yres3:Int=31

Smalltalk

This example is in Smalltalk, of a typical accessor method to return the value of a variable using lazy initialization.

height^heightifNil: [height:=2.0].

The 'non-lazy' alternative is to use an initialization method that is run when the object is created and then use a simpler accessor method to fetch the value.

initializeheight:=2.0height^height

Note that lazy initialization can also be used in non-object-oriented languages.

Theoretical computer science

In the field of theoretical computer science, lazy initialization [4] (also called a lazy array) is a technique to design data structures that can work with memory that does not need to be initialized. Specifically, assume that we have access to a table T of n uninitialized memory cells (numbered from 1 to n), and want to assign m cells of this array, e.g., we want to assign T[ki] := vi for pairs (k1, v1), ..., (km, vm) with all ki being different. The lazy initialization technique allows us to do this in just O(m) operations, rather than spending O(m+n) operations to first initialize all array cells. The technique is simply to allocate a table V storing the pairs (ki, vi) in some arbitrary order, and to write for each i in the cell T[ki] the position in V where key ki is stored, leaving the other cells of T uninitialized. This can be used to handle queries in the following fashion: when we look up cell T[k] for some k, we can check if T[k] is in the range {1, ..., m}: if it is not, then T[k] is uninitialized. Otherwise, we check V[T[k]], and verify that the first component of this pair is equal to k. If it is not, then T[k] is uninitialized (and just happened by accident to fall in the range {1, ..., m}). Otherwise, we know that T[k] is indeed one of the initialized cells, and the corresponding value is the second component of the pair.

See also

References

  1. "Lazy initialization - Design patterns - Haxe programming language cookbook". 2018-01-11. Retrieved 2018-11-09.
  2. "LazyCell in std::cell - Rust". doc.rust-lang.org. Retrieved 18 January 2025.
  3. Pollak, David (2009-05-25). Beginning Scala. Apress. ISBN   9781430219897.
  4. Moret, B. M. E.; Shapiro, H. D. (1991). Algorithms from P to NP, Volume 1: Design & Efficiency. Benjamin/Cummings Publishing Company. pp. 191–192. ISBN   0-8053-8008-6.