In computer science, interning is re-using objects of equal value on-demand instead of creating new objects. This
creational pattern
In software engineering, creational design patterns are design patterns that deal with object creation
Object may refer to:
General meanings
* Object (philosophy), a thing, being, or concept
** Object (abstract), an object which does not ex ...
is frequently used for numbers and strings in different programming languages. In many object-oriented languages such as
Python, even
primitive types
In computer science, primitive data types are a set of basic data types from which all other data types are constructed. Specifically it often refers to the limited set of data representations in use by a particular processor, which all compiled ...
such as integer numbers are objects. To avoid the overhead of constructing a large number of integer objects, these objects get reused through interning.
For interning to work the interned objects must be immutable, since state is shared between multiple variables.
String interning
In computer science, string interning is a method of storing only one copy of each distinct string value, which must be immutable. Interning strings makes some string processing tasks more time-efficient or space-efficient at the cost of requiring ...
is a common application of interning, where many strings with identical values are needed in the same program.
History
Lisp
Lisp (historically LISP, an abbreviation of "list processing") is a family of programming languages with a long history and a distinctive, fully parenthesized Polish notation#Explanation, prefix notation.
Originally specified in the late 1950s, ...
introduced the notion of interned strings for its
symbols
A symbol is a mark, sign, or word that indicates, signifies, or is understood as representing an idea, object, or relationship. Symbols allow people to go beyond what is known or seen by creating linkages between otherwise different concep ...
. The LISP 1.5 Programmers Manual describes a function called
intern
which either evaluates to an existing symbol of the supplied name, or if none exists, creates a new symbol of that name. This idea of interned symbols persists in more recent dialects of Lisp, such as
Clojure
Clojure (, like ''closure'') is a dynamic programming language, dynamic and functional programming, functional dialect (computing), dialect of the programming language Lisp (programming language), Lisp on the Java (software platform), Java platfo ...
in special forms such a
(def symbol)
which perform symbol creation and interning.
In the
object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impl ...
paradigm interning is an important mechanism in the
flyweight pattern
In computer programming, the flyweight software design pattern refers to an Object (computer science), object that minimizes Computer memory, memory usage by sharing some of its data with other similar objects. The flyweight pattern is one of twe ...
, where an interning method is called to store the intrinsic state of an object such that this can be shared among different objects which share different extrinsic state, avoiding needless duplication.
Interning continues to be an important technique for managing memory use in programming language implementations; for example, the Java Language Specification requires that identical string literals (that is, literals that contain the same sequence of code points) must refer to the same instance of class String, because string literals are "interned" so as to share unique instances. In the Python programming language small integers are interned, though the details of exactly which are dependent on language version.
Motivation
Interning saves memory and can thus improve performance and
memory footprint
Memory footprint refers to the amount of main memory that a program uses or references while running.
The word footprint generally refers to the extent of physical dimensions that an object occupies, giving a sense of its size. In computing, t ...
of a program.
[{{Cite book, last=Oaks, first=Scott, url=https://www.worldcat.org/oclc/878059649, title=Java performance : the definitive guide, date=2014, publisher=O'Reilly Media, isbn=978-1-4493-6354-3, location=Sebastopol, CA, oclc=878059649] The downside is time required to search for existing values of objects which are to be interned.
See also
*
Flyweight pattern
In computer programming, the flyweight software design pattern refers to an Object (computer science), object that minimizes Computer memory, memory usage by sharing some of its data with other similar objects. The flyweight pattern is one of twe ...
*
Hash consing
References
External links
Design Patterns - University of Washington
A standard library package for interning in Go - The Go Blog
Software optimization
String (computer science)