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 Usage:

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 a single function, or a single source file, using static variables.

In a function:

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

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

fruit.h:

#ifndef _FRUIT_INCLUDED_#define _FRUIT_INCLUDED_structfruit{char*name;structfruit*next;intnumber;/* Other members */};structfruit*get_fruit(char*name);voidprint_fruit_list(FILE*file);#endif /* _FRUIT_INCLUDED_ */

fruit.c:

#include<string.h>#include<stdlib.h>#include<stddef.h>#include<stdio.h>#include"fruit.h"staticstructfruit*fruit_list;staticintseq;structfruit*get_fruit(char*name){structfruit*f;for(f=fruit_list;f;f=f->next)if(0==strcmp(name,f->name))returnf;if(!(f=malloc(sizeof(structfruit))))returnNULL;if(!(f->name=strdup(name))){free(f);returnNULL;}f->number=++seq;f->next=fruit_list;fruit_list=f;returnf;}voidprint_fruit_list(FILE*file){structfruit*f;for(f=fruit_list;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[]){inti;structfruit*f;if(argc<2){fprintf(stderr,"Usage: fruits fruit-name [...]\n");exit(1);}for(i=1;i<argc;i++){if((f=get_fruit(argv[i]))){printf("Fruit %s: number %d\n",argv[i],f->number);}}printf("The following fruits have been generated:\n");print_fruit_list(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++

Here is an example in C++.

#include<iostream>#include<map>#include<string>classFruit{public:staticFruit*GetFruit(conststd::string&type);staticvoidPrintCurrentTypes();private:// Note: constructor private forcing one to use static |GetFruit|.Fruit(conststd::string&type):type_(type){}staticstd::map<std::string,Fruit*>types;std::stringtype_;};// staticstd::map<std::string,Fruit*>Fruit::types;// Lazy Factory method, gets the |Fruit| instance associated with a certain// |type|.  Creates new ones as needed.Fruit*Fruit::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.voidFruit::PrintCurrentTypes(){std::cout<<"Number of instances made = "<<types.size()<<std::endl;for(constauto&[type,fruit]:types){std::cout<<type<<std::endl;}std::cout<<std::endl;}intmain(){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

Here is an example 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

Here is an example 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

Here is an example 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){// 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

Here is an example 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

Here is an example 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

Scala

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

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

Smalltalk

Here is an example 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 [3] (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 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

Related Research Articles

In programming languages, a closure, also lexical closure or function closure, is a technique for implementing lexically scoped name binding in a language with first-class functions. Operationally, a closure is a record storing a function together with an environment. The environment is a mapping associating each free variable of the function with the value or reference to which the name was bound when the closure was created. Unlike a plain function, a closure allows the function to access those captured variables through the closure's copies of their values or references, even when the function is invoked outside their scope.

In object-oriented programming, the decorator pattern is a design pattern that allows behavior to be added to an individual object, dynamically, without affecting the behavior of other instances of the same class. The decorator pattern is often useful for adhering to the Single Responsibility Principle, as it allows functionality to be divided between classes with unique areas of concern as well as to the Open-Closed Principle, by allowing the functionality of a class to be extended without being modified. Decorator use can be more efficient than subclassing, because an object's behavior can be augmented without defining an entirely new object.

Template metaprogramming (TMP) is a metaprogramming technique in which templates are used by a compiler to generate temporary source code, which is merged by the compiler with the rest of the source code and then compiled. The output of these templates can include compile-time constants, data structures, and complete functions. The use of templates can be thought of as compile-time polymorphism. The technique is used by a number of languages, the best-known being C++, but also Curl, D, Nim, and XL.

<span class="mw-page-title-main">D (programming language)</span> Multi-paradigm system programming language

D, also known as dlang, is a multi-paradigm system programming language created by Walter Bright at Digital Mars and released in 2001. Andrei Alexandrescu joined the design and development effort in 2007. Though it originated as a re-engineering of C++, D is now a very different language drawing inspiration from other high-level programming languages, notably Java, Python, Ruby, C#, and Eiffel.

In mathematics and computer science, a higher-order function (HOF) is a function that does at least one of the following:

<span class="mw-page-title-main">C syntax</span> Set of rules defining correctly structured programs

The syntax of the C programming language is the set of rules governing writing of software in C. It is designed to allow for programs that are extremely terse, have a close relationship with the resulting object code, and yet provide relatively high-level data abstraction. C was the first widely successful high-level language for portable operating-system development.

In computer programming, a function object is a construct allowing an object to be invoked or called as if it were an ordinary function, usually with the same syntax. In some languages, particularly C++, function objects are often called functors.

In the C++ programming language, a reference is a simple reference datatype that is less powerful but safer than the pointer type inherited from C. The name C++ reference may cause confusion, as in computer science a reference is a general concept datatype, with pointers and C++ references being specific reference datatype implementations. The definition of a reference in C++ is such that it does not need to exist. It can be implemented as a new name for an existing object.

In computer science, a mutator method is a method used to control changes to a variable. They are also widely known as setter methods. Often a setter is accompanied by a getter, which returns the value of the private member variable. They are also known collectively as accessors.

In object-oriented programming, a member variable is a variable that is associated with a specific object, and accessible for all its methods.

A class in C++ is a user-defined type or data structure declared with any of the keywords class, struct or union that has data and functions as its members whose access is governed by the three access specifiers private, protected or public. By default access to members of a C++ class declared with the keyword class is private. The private members are not accessible outside the class; they can be accessed only through member functions of the class. The public members form an interface to the class and are accessible outside the class.

C++11 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versions by the publication year of the specification, though it was formerly named C++0x because it was expected to be published before 2010.

In object-oriented computer programming, a null object is an object with no referenced value or with defined neutral (null) behavior. The null object design pattern, which describes the uses of such objects and their behavior, was first published as "Void Value" and later in the Pattern Languages of Program Design book series as "Null Object".

This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.

In computer programming, variadic templates are templates that take a variable number of arguments.

In C++ computer programming, copy elision refers to a compiler optimization technique that eliminates unnecessary copying of objects.

C++14 is a version of the ISO/IEC 14882 standard for the C++ programming language. It is intended to be a small extension over C++11, featuring mainly bug fixes and small improvements, and was replaced by C++17. Its approval was announced on August 18, 2014. C++14 was published as ISO/IEC 14882:2014 in December 2014.

Swift is a high-level general-purpose, multi-paradigm, compiled programming language developed by Apple Inc. and the open-source community. Swift compiles to machine code, as it is an LLVM-based compiler. Swift was first released in June 2014, and the Swift toolchain has shipped in Xcode since version 6, released in 2014.

A callable object, in computer programming, is any object that can be called like a function.

<span class="mw-page-title-main">Strongly typed identifier</span>

A strongly typed identifier is user-defined data type which serves as an identifier or key that is strongly typed. This is a solution to the "primitive obsession" code smell as mentioned by Martin Fowler. The data type should preferably be immutable if possible. It is common for implementations to handle equality testing, serialization and model binding.

References

  1. "Lazy initialization - Design patterns - Haxe programming language cookbook". 2018-01-11. Retrieved 2018-11-09.
  2. Pollak, David (2009-05-25). Beginning Scala. ISBN   9781430219897.
  3. 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.