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<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 one 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++

This example is in C++.

importstd;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::println("Number of instances made = {}",types.size());for(constauto&[type,fruit]:types){std::println({},type);}std::println();}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

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){// 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::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

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.

<span class="mw-page-title-main">Singleton pattern</span> Design pattern in object-oriented software development

In object-oriented programming, the singleton pattern is a software design pattern that restricts the instantiation of a class to a singular instance. It is one of the well-known "Gang of Four" design patterns, which describe how to solve recurring problems in object-oriented software. The pattern is useful when exactly one object is needed to coordinate actions across a system.

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.

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

In computing, type introspection is the ability of a program to examine the type or properties of an object at runtime. Some programming languages possess this capability.

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.

<span class="mw-page-title-main">Foreach loop</span> Control flow statement for traversing items in a collection

In computer programming, foreach loop is a control flow statement for traversing items in a collection. foreach is usually used in place of a standard for loop statement. Unlike other for loop constructs, however, foreach loops usually maintain no explicit counter: they essentially say "do this to everything in this set", rather than "do this x times". This avoids potential off-by-one errors and makes code simpler to read. In object-oriented languages, an iterator, even if implicit, is often used as the means of traversal.

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.

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.

The curiously recurring template pattern (CRTP) is an idiom, originally in C++, in which a class X derives from a class template instantiation using X itself as a template argument. More generally it is known as F-bound polymorphism, and it is a form of F-bounded quantification.

C++11 is a version of a joint technical standard, ISO/IEC 14882, by the International Organization for Standardization (ISO) and International Electrotechnical Commission (IEC), for the C++ programming language. C++11 replaced the prior version of the C++ standard, named 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.

Substitution failure is not an error (SFINAE) is a principle in C++ where an invalid substitution of template parameters is not in itself an error. David Vandevoorde first introduced the acronym SFINAE to describe related programming techniques.

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

In computer programming, variable shadowing occurs when a variable declared within a certain scope has the same name as a variable declared in an outer scope. At the level of identifiers, this is known as name masking. This outer variable is said to be shadowed by the inner variable, while the inner identifier is said to mask the outer identifier. This can lead to confusion, as it may be unclear which variable subsequent uses of the shadowed variable name refer to, which depends on the name resolution rules of the language.

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

In computer programming, string interpolation is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values. It is a form of simple template processing or, in formal terms, a form of quasi-quotation. The placeholder may be a variable name, or in some languages an arbitrary expression, in either case evaluated in the current context.

In object-oriented programming, an indexer allows instances of a particular class or struct to be indexed just like arrays. It is a form of operator overloading.

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

<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. "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.