Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Lazy initialization
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
In [[computer programming]], '''lazy initialization''' is the tactic of delaying the creation of an [[Object (computer science)|object]], the calculation of a [[Value (computer science)|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. 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 [[Multithreading (computer architecture)|multithreaded]] code, access to lazy-initialized objects/state must be [[mutual exclusion|synchronized]] to guard against [[race condition]]s. ==The "lazy factory"== In a [[software design pattern]] view, lazy initialization is often used together with a [[factory method pattern]]. This combines three ideas: * Using a factory method to create instances of a [[Class (computer programming)|class]] ([[factory method pattern]]) * Storing the instances in a [[Associative array|map]], and returning the ''same'' instance to each request for an instance with ''same'' parameters ([[multiton pattern]]) * Using lazy initialization to instantiate the object the first time it is requested (lazy initialization pattern) ==Examples== ===ActionScript 3=== The following is an example of a class with lazy initialization implemented in [[ActionScript]]: <syntaxhighlight lang="actionscript"> package examples.lazyinstantiation { public class Fruit { private var _typeName:String; private static var instancesByTypeName:Dictionary = new Dictionary(); public function Fruit(typeName:String):void { this._typeName = typeName; } public function get typeName():String { return _typeName; } public static function getFruitByTypeName(typeName:String):Fruit { return instancesByTypeName[typeName] ||= new Fruit(typeName); } public static function printCurrentTypes():void { for each (var fruit:Fruit in instancesByTypeName) { // iterates through each value trace(fruit.typeName); } } } } </syntaxhighlight> Basic use: <syntaxhighlight lang="actionscript"> package { import examples.lazyinstantiation; public class Main { public function Main():void { Fruit.getFruitByTypeName("Banana"); Fruit.printCurrentTypes(); Fruit.getFruitByTypeName("Apple"); Fruit.printCurrentTypes(); Fruit.getFruitByTypeName("Banana"); Fruit.printCurrentTypes(); } } } </syntaxhighlight> ===C=== In [[C (programming language)|C]], lazy evaluation would normally be implemented inside one function, or one source file, using [[static variable]]s. In a function: <syntaxhighlight lang="C"> #include <string.h> #include <stdlib.h> #include <stddef.h> #include <stdio.h> struct fruit { char *name; struct fruit *next; int number; /* Other members */ }; struct fruit *get_fruit(char *name) { static struct fruit *fruit_list; static int seq; struct fruit *f; for (f = fruit_list; f; f = f->next) if (0 == strcmp(name, f->name)) return f; if (!(f = malloc(sizeof(struct fruit)))) return NULL; if (!(f->name = strdup(name))) { free(f); return NULL; } f->number = ++seq; f->next = fruit_list; fruit_list = f; return f; } /* Example code */ int main(int argc, char *argv[]) { int i; struct fruit *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); } } return 0; } </syntaxhighlight> Using one source file instead allows the state to be shared between multiple functions, while still hiding it from non-related functions. fruit.h: <syntaxhighlight lang="C"> #ifndef _FRUIT_INCLUDED_ #define _FRUIT_INCLUDED_ struct fruit { char *name; struct fruit *next; int number; /* Other members */ }; struct fruit *get_fruit(char *name); void print_fruit_list(FILE *file); #endif /* _FRUIT_INCLUDED_ */ </syntaxhighlight> fruit.c: <syntaxhighlight lang="C"> #include <string.h> #include <stdlib.h> #include <stddef.h> #include <stdio.h> #include "fruit.h" static struct fruit *fruit_list; static int seq; struct fruit *get_fruit(char *name) { struct fruit *f; for (f = fruit_list; f; f = f->next) if (0 == strcmp(name, f->name)) return f; if (!(f = malloc(sizeof(struct fruit)))) return NULL; if (!(f->name = strdup(name))) { free(f); return NULL; } f->number = ++seq; f->next = fruit_list; fruit_list = f; return f; } void print_fruit_list(FILE *file) { struct fruit *f; for (f = fruit_list; f; f = f->next) fprintf(file, "%4d %s\n", f->number, f->name); } </syntaxhighlight> main.c: <syntaxhighlight lang="C"> #include <stdlib.h> #include <stdio.h> #include "fruit.h" int main(int argc, char *argv[]) { int i; struct fruit *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); return 0; } </syntaxhighlight> ===C#=== In .NET Framework 4.0 Microsoft has included a <code>Lazy</code> class that can be used to do lazy loading. Below is some dummy code that does lazy loading of Class <code>Fruit</code> <syntaxhighlight lang="csharp"> var lazyFruit = new Lazy<Fruit>(); Fruit fruit = lazyFruit.Value; </syntaxhighlight> Here is a dummy example in [[C Sharp (programming language)|C#]]. The <code>Fruit</code> class itself doesn't do anything here, The [[class variable]] <code>_typesDictionary</code> is a Dictionary/Map used to store <code>Fruit</code> instances by <code>typeName</code>. <syntaxhighlight lang="csharp"> using System; using System.Collections; using System.Collections.Generic; public class Fruit { private string _typeName; private static IDictionary<string, Fruit> _typesDictionary = new Dictionary<string, Fruit>(); private Fruit(string typeName) { this._typeName = typeName; } public static Fruit GetFruitByTypeName(string type) { Fruit fruit; if (!_typesDictionary.TryGetValue(type, out fruit)) { // Lazy initialization fruit = new Fruit(type); _typesDictionary.Add(type, fruit); } return fruit; } public static void ShowAll() { if (_typesDictionary.Count > 0) { Console.WriteLine("Number of instances made = {0}", _typesDictionary.Count); foreach (KeyValuePair<string, Fruit> kvp in _typesDictionary) { Console.WriteLine(kvp.Key); } Console.WriteLine(); } } public Fruit() { // required so the sample compiles } } class Program { static void Main(string[] args) { Fruit.GetFruitByTypeName("Banana"); Fruit.ShowAll(); Fruit.GetFruitByTypeName("Apple"); Fruit.ShowAll(); // returns pre-existing instance from first // time Fruit with "Banana" was created Fruit.GetFruitByTypeName("Banana"); Fruit.ShowAll(); Console.ReadLine(); } } </syntaxhighlight> A fairly straightforward 'fill-in-the-blanks' example of a Lazy Initialization design pattern, except that this uses an [[Enumerated type|enumeration]] for the type <syntaxhighlight lang="csharp"> namespace DesignPatterns.LazyInitialization; public class LazyFactoryObject { // internal collection of items // IDictionary makes sure they are unique private IDictionary<LazyObjectSize, LazyObject> _LazyObjectList = new Dictionary<LazyObjectSize, LazyObject>(); // enum for passing name of size required // avoids passing strings and is part of LazyObject ahead public enum LazyObjectSize { None, Small, Big, Bigger, Huge } // standard type of object that will be constructed public struct LazyObject { public LazyObjectSize Size; public IList<int> Result; } // takes size and create 'expensive' list private IList<int> Result(LazyObjectSize size) { IList<int> result = null; switch (size) { case LazyObjectSize.Small: result = CreateSomeExpensiveList(1, 100); break; case LazyObjectSize.Big: result = CreateSomeExpensiveList(1, 1000); break; case LazyObjectSize.Bigger: result = CreateSomeExpensiveList(1, 10000); break; case LazyObjectSize.Huge: result = CreateSomeExpensiveList(1, 100000); break; case LazyObjectSize.None: result = null; break; default: result = null; break; } return result; } // not an expensive item to create, but you get the point // delays creation of some expensive object until needed private IList<int> CreateSomeExpensiveList(int start, int end) { IList<int> result = new List<int>(); for (int counter = 0; counter < (end - start); counter++) { result.Add(start + counter); } return result; } public LazyFactoryObject() { // empty constructor } public LazyObject GetLazyFactoryObject(LazyObjectSize size) { // yes, i know it is illiterate and inaccurate LazyObject noGoodSomeOne; // retrieves LazyObjectSize from list via out, else creates one and adds it to list if (!_LazyObjectList.TryGetValue(size, out noGoodSomeOne)) { noGoodSomeOne = new LazyObject(); noGoodSomeOne.Size = size; noGoodSomeOne.Result = this.Result(size); _LazyObjectList.Add(size, noGoodSomeOne); } return noGoodSomeOne; } } </syntaxhighlight> ===C++=== This example is in [[C++]]. <syntaxhighlight lang="cpp"> import std; class Fruit { public: static Fruit* GetFruit(const std::string& type); static void PrintCurrentTypes(); private: // Note: constructor private forcing one to use static |GetFruit|. Fruit(const std::string& type) : type_(type) {} static std::map<std::string, Fruit*> types; std::string type_; }; // static std::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(const std::string& type) { auto [it, inserted] = types.emplace(type, nullptr); if (inserted) { it->second = new Fruit(type); } return it->second; } // For example purposes to see pattern in action. void Fruit::PrintCurrentTypes() { std::println("Number of instances made = {}", types.size()); for (const auto& [type, fruit] : types) { std::println({}, type); } std::println(); } int main() { 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 // </syntaxhighlight> === Crystal === <syntaxhighlight lang="ruby"> class Fruit private getter type : String @@types = {} of String => Fruit def initialize(@type) end def self.get_fruit_by_type(type : String) @@types[type] ||= Fruit.new(type) end def self.show_all puts "Number of instances made: #{@@types.size}" @@types.each do |type, fruit| puts "#{type}" end puts end def self.size @@types.size end end Fruit.get_fruit_by_type("Banana") Fruit.show_all Fruit.get_fruit_by_type("Apple") Fruit.show_all Fruit.get_fruit_by_type("Banana") Fruit.show_all </syntaxhighlight> Output: <pre> Number of instances made: 1 Banana Number of instances made: 2 Banana Apple Number of instances made: 2 Banana Apple </pre> === Haxe === This example is in [[Haxe]].<ref>{{Cite news|url=https://code.haxe.org/category/design-patterns/lazy-initialization.html|title=Lazy initialization - Design patterns - Haxe programming language cookbook|date=2018-01-11|access-date=2018-11-09|language=en}}</ref> <syntaxhighlight lang="haxe"> class Fruit { private static var _instances = new Map<String, Fruit>(); public var name(default, null):String; public function new(name:String) { this.name = name; } public static function getFruitByName(name:String):Fruit { if (!_instances.exists(name)) { _instances.set(name, new Fruit(name)); } return _instances.get(name); } public static function printAllTypes() { trace([for(key in _instances.keys()) key]); } } </syntaxhighlight>Usage<syntaxhighlight lang="haxe"> class Test { public static function main () { var banana = Fruit.getFruitByName("Banana"); var apple = Fruit.getFruitByName("Apple"); var banana2 = Fruit.getFruitByName("Banana"); trace(banana == banana2); // true. same banana Fruit.printAllTypes(); // ["Banana","Apple"] } } </syntaxhighlight> ===Java=== {{Warning |This example is not thread-safe, see the [[Talk:Lazy initialization#Java code: thread safety flaw and pertinence|talk page]]. Instead see the examples in [[Double-checked locking#Usage in Java]].}} This example is in [[Java (programming language)|Java]]. <syntaxhighlight lang="java"> import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; public class Program { /** * @param args */ public static void main(String[] args) { Fruit.getFruitByTypeName(FruitType.banana); Fruit.showAll(); Fruit.getFruitByTypeName(FruitType.apple); Fruit.showAll(); Fruit.getFruitByTypeName(FruitType.banana); Fruit.showAll(); } } enum FruitType { none, apple, banana, } class Fruit { private static Map<FruitType, Fruit> types = new HashMap<>(); /** * Using a private constructor to force the use of the factory method. * @param type */ private Fruit(FruitType type) { } /** * 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. */ public static Fruit getFruitByTypeName(FruitType type) { Fruit fruit; // 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 initialisation fruit = new Fruit(type); types.put(type, fruit); } else { // OK, it's available currently fruit = types.get(type); } return fruit; } /** * 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. */ public static Fruit getFruitByTypeNameHighConcurrentVersion(FruitType type) { if (!types.containsKey(type)) { synchronized (types) { // Check again, after having acquired the lock to make sure // the instance was not created meanwhile by another thread if (!types.containsKey(type)) { // Lazy initialisation types.put(type, new Fruit(type)); } } } return types.get(type); } /** * Displays all entered fruits. */ public static void showAll() { if (types.size() > 0) { System.out.println("Number of instances made = " + types.size()); for (Entry<FruitType, Fruit> entry : types.entrySet()) { String fruit = entry.getKey().toString(); fruit = Character.toUpperCase(fruit.charAt(0)) + fruit.substring(1); System.out.println(fruit); } System.out.println(); } } } </syntaxhighlight> '''Output''' <pre> Number of instances made = 1 Banana Number of instances made = 2 Banana Apple Number of instances made = 2 Banana Apple </pre> ===JavaScript=== This example is in [[JavaScript]]. <syntaxhighlight lang="javascript"> var Fruit = (function() { var types = {}; function Fruit() {}; // count own properties in object function count(obj) { return Object.keys(obj).length; } var _static = { getFruit: function(type) { if (typeof types[type] == 'undefined') { types[type] = new Fruit; } return types[type]; }, printCurrentTypes: function () { console.log('Number of instances made: ' + count(types)); for (var type in types) { console.log(type); } } }; return _static; })(); Fruit.getFruit('Apple'); Fruit.printCurrentTypes(); Fruit.getFruit('Banana'); Fruit.printCurrentTypes(); Fruit.getFruit('Apple'); Fruit.printCurrentTypes(); </syntaxhighlight> '''Output''' <pre> Number of instances made: 1 Apple Number of instances made: 2 Apple Banana Number of instances made: 2 Apple Banana </pre> ===PHP=== Here is an example of lazy initialization in [[PHP]] 7.4: <syntaxhighlight lang="php"> <?php header('Content-Type: text/plain; charset=utf-8'); class Fruit { private string $type; private static array $types = array(); private function __construct(string $type) { $this->type = $type; } public static function getFruit(string $type) { // Lazy initialization takes place here if (!isset(self::$types[$type])) { self::$types[$type] = new Fruit($type); } return self::$types[$type]; } public static function printCurrentTypes(): 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: 1 Apple Number of instances made: 2 Apple Banana Number of instances made: 2 Apple Banana */ </syntaxhighlight> ===Python=== This example is in [[Python (programming language)|Python]]. <syntaxhighlight lang="python"> class Fruit: def __init__(self, item: str): self.item = item class FruitCollection: def __init__(self): self.items = {} def get_fruit(self, item: str) -> Fruit: if item not in self.items: self.items[item] = Fruit(item) return self.items[item] if __name__ == "__main__": fruits = FruitCollection() print(fruits.get_fruit("Apple")) print(fruits.get_fruit("Lime")) </syntaxhighlight> ===Ruby=== This example is in [[Ruby (programming language)|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]]. <syntaxhighlight lang="ruby"> require 'net/http' class Blogger def auth_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 class end b = Blogger.new b.instance_variable_get(:@auth_token) # returns nil b.auth_token # returns token b.instance_variable_get(:@auth_token) # returns token </syntaxhighlight> ===Rust=== [[Rust (programming language)|Rust]] have {{Code|std::cell::LazyCell}}.<ref>{{cite web |title=LazyCell in std::cell - Rust |url=https://doc.rust-lang.org/std/cell/struct.LazyCell.html |website=doc.rust-lang.org |access-date=18 January 2025}}</ref> <syntaxhighlight lang="rust"> use std::cell::LazyCell; let lazy = LazyCell::new(|| 42); </syntaxhighlight> ===Scala=== [[Scala (programming language)|Scala]] has built-in support for lazy variable initiation.<ref>{{Cite book |last1=Pollak |first1=David |date=2009-05-25 |url=https://books.google.com/books?id=Qt-bRFetWw0C&q=scala+lazy+variables&pg=PA30 |title=Beginning Scala |publisher=Apress |isbn=9781430219897}}</ref> <syntaxhighlight lang="scala" highlight="1,4,6,9"> scala> val x = { println("Hello"); 99 } Hello x: Int = 99 scala> lazy val y = { println("Hello!!"); 31 } y: Int = <lazy> scala> y Hello!! res2: Int = 31 scala> y res3: Int = 31 </syntaxhighlight> ===Smalltalk=== This example is in [[Smalltalk]], of a typical [[accessor method]] to return the value of a variable using lazy initialization. <syntaxhighlight lang="smalltalk"> height ^height ifNil: [height := 2.0]. </syntaxhighlight> 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. <syntaxhighlight lang="smalltalk"> initialize height := 2.0 height ^height </syntaxhighlight> Note that lazy initialization can also be used in non-[[object-oriented language]]s. == Theoretical computer science == In the field of [[theoretical computer science]], '''lazy initialization'''<ref>{{Cite book|last1=Moret|first1=B. M. E.|title=Algorithms from P to NP, Volume 1: Design & Efficiency|last2=Shapiro|first2=H. D.|publisher=Benjamin/Cummings Publishing Company|year=1991|isbn=0-8053-8008-6|pages=191β192}}</ref> (also called a '''lazy array''') is a technique to design [[data structure]]s 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''[''k<sub>i</sub>''] := ''v<sub>i</sub>'' for pairs (''k''<sub>1</sub>, ''v''<sub>1</sub>), ..., (''k<sub>m</sub>'', ''v<sub>m</sub>'') with all ''k<sub>i</sub>'' 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 (''k<sub>i</sub>'', ''v<sub>i</sub>'') in some arbitrary order, and to write for each ''i'' in the cell ''T''[''k<sub>i</sub>''] the position in ''V'' where key ''k<sub>i</sub>'' 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== * [[Double-checked locking]] * [[Lazy loading]] * [[Proxy pattern]] * [[Singleton pattern]] ==References== {{Reflist}} ==External links== * Article "[https://www.infoworld.com/article/2077568/java-tip-67--lazy-instantiation.html Java Tip 67: Lazy instantiation] - Balancing performance and resource usage" by [[Philip Bishop]] and [[Nigel Warren]] * [http://javapractices.com/Topic34.cjp Java code examples] * [https://web.archive.org/web/20191202055631/http://devx.com/tips/Tip/18007 Use Lazy Initialization to Conserve Resources] * [https://wiki.c2.com/?LazyInitialization Description from the Portland Pattern Repository] * [https://web.archive.org/web/20060911223210/http://weblogs.java.net/blog/binod/archive/2005/09/lazy_initializa.html Lazy Initialization of Application Server Services] * [https://sourceforge.net/projects/jsiner/ Lazy Inheritance in JavaScript] * [https://kaliko.com/blog/lazy-loading-in-c-net/ Lazy Inheritance in C#] {{Design patterns}} {{DEFAULTSORT:Lazy Initialization}} [[Category:Software design patterns]] [[Category:Programming language comparisons]] <!-- Hidden categories below --> [[Category:Articles with example C code]] [[Category:Articles with example C++ code]] [[Category:Articles with example C Sharp code]] [[Category:Articles with example Java code]] [[Category:Articles with example JavaScript code]] [[Category:Articles with example PHP code]] [[Category:Articles with example Python (programming language) code]] [[Category:Articles with example Ruby code]] [[Category:Articles with example Smalltalk code]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Cite book
(
edit
)
Template:Cite news
(
edit
)
Template:Cite web
(
edit
)
Template:Code
(
edit
)
Template:Design patterns
(
edit
)
Template:Reflist
(
edit
)
Template:Warning
(
edit
)