Lazy initialization explained

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.

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:package examples.lazyinstantiationBasic Usage:package

C

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

In a function:

  1. include
  2. include
  3. include
  4. include

struct fruit ;

struct fruit *get_fruit(char *name)

/* Example code */

int main(int argc, char *argv[]) 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:

  1. ifndef _FRUIT_INCLUDED_
  2. define _FRUIT_INCLUDED_

struct fruit ;

struct fruit *get_fruit(char *name);void print_fruit_list(FILE *file);

  1. endif /* _FRUIT_INCLUDED_ */

fruit.c:

  1. include
  2. include
  3. include
  4. include
  5. include "fruit.h"

static struct fruit *fruit_list;static int seq;

struct fruit *get_fruit(char *name)

void print_fruit_list(FILE *file)

main.c:

  1. include
  2. include
  3. include "fruit.h"

int main(int argc, char *argv[])

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 Fruitvar lazyFruit = new Lazy;Fruit fruit = 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.

using System;using System.Collections;using System.Collections.Generic;

public class Fruit

class ProgramA fairly straightforward 'fill-in-the-blanks' example of a Lazy Initialization design pattern, except that this uses an enumeration for the typenamespace DesignPatterns.LazyInitialization;

public class LazyFactoryObject

C++

Here is an example in C++.

  1. include
  2. include
  3. include

class Fruit ;

// staticstd::map 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)

// For example purposes to see pattern in action.void Fruit::PrintCurrentTypes

int main

// OUTPUT://// Number of instances made = 1// Banana//// Number of instances made = 2// Apple// Banana//// Number of instances made = 2// Apple// Banana//

Crystal

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.each do |type, fruit| puts "#" end puts end

def self.size @@types.size endend

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

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] class Fruit Usageclass Test

Java

Here is an example in Java.

import java.util.HashMap;import java.util.Map;import java.util.Map.Entry;

public class Program

enum FruitType

class Fruit

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.

var Fruit = (function);

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:

class Fruit

Fruit::getFruit('Apple');Fruit::printCurrentTypes;

Fruit::getFruit('Banana');Fruit::printCurrentTypes;

Fruit::getFruit('Apple');Fruit::printCurrentTypes;

/*OUTPUT:

Number of instances made: 1Apple

Number of instances made: 2AppleBanana

Number of instances made: 2AppleBanana

Python

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

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'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 classend

b = 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> val x = Hello x: Int = 99 scala> lazy val y = y: Int = scala> y Hello!! res2: Int = 31 scala> y res3: 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 ^height ifNil: [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.

initialize height := 2.0

height ^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[''k<sub>i</sub>''] := 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[''k<sub>i</sub>''] 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 : 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). 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

External links

Notes and References

  1. News: Lazy initialization - Design patterns - Haxe programming language cookbook. 2018-01-11. 2018-11-09. en.
  2. Book: Beginning Scala. 9781430219897. Pollak. David. 2009-05-25.
  3. Book: Moret. B. M. E.. Algorithms from P to NP, Volume 1: Design & Efficiency. Shapiro. H. D.. Benjamin/Cummings Publishing Company. 1991. 0-8053-8008-6. 191–192.