In computer science, interning is re-using objects of equal value on-demand instead of creating new objects. This creational pattern[1] is frequently used for numbers and strings in different programming languages. In many object-oriented languages such as Python, even primitive types such as integer numbers are objects. To avoid the overhead of constructing a large number of integer objects, these objects get reused through interning.[2]

For interning to work, the interned objects must be immutable, since state is shared between multiple variables. String interning is a common application of interning, where many strings with identical values are needed in the same program.

History

edit

Lisp introduced the notion of interned strings for its symbols. The LISP 1.5 Programmers Manual [3] 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 in special forms such a (def symbol) which perform symbol creation and interning.[4]

In the object-oriented programming paradigm interning is an important mechanism in the flyweight pattern, 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.[5]

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.[6] In the Python programming language small integers are interned,[7] though the details of exactly which are dependent on language version.

Motivation

edit

Interning saves memory and can thus improve performance and memory footprint of a program.[8] The downside is time required to search for existing values of objects which are to be interned.

See also

edit

References

edit
  1. ^ "Design Patterns" (PDF). University of Washington.
  2. ^ Jaworski, Michał (2019). Expert Python programming : become a master in Python by learning coding best practices and advanced programming concepts in Python 3.7. Tarek Ziadé (Third ed.). Birmingham, U.K. ISBN 978-1-78980-677-9. OCLC 1125343555.{{cite book}}: CS1 maint: location missing publisher (link)
  3. ^ Levin, Michael I. (1965). LISP 1.5 programmer's manual : the Computation Center and Research Laboratory of Electronics, Massachusetts Institute of Technology. John McCarthy, Massachusetts Institute of Technology. Computation Center, Massachusetts Institute of Technology. Research Laboratory of Electronics (2nd ed.). Cambridge: M.I.T. Press. ISBN 0-262-13011-4. OCLC 1841373.
  4. ^ "Clojure - Special Forms". clojure.org. Retrieved 2021-11-04.
  5. ^ Design patterns : elements of reusable object-oriented software. Erich Gamma, Richard Helm, Ralph E. Johnson, John Vlissides. Reading, Mass.: Addison-Wesley. 1995. ISBN 0-201-63361-2. OCLC 31171684.{{cite book}}: CS1 maint: others (link)
  6. ^ "Java Language Specification. Chapter 3. Lexical Structure". docs.oracle.com. Retrieved 2021-11-04.
  7. ^ "PEP 237 -- Unifying Long Integers and Integers". Python.org. Retrieved 2021-11-04.
  8. ^ Oaks, Scott (2014). Java performance : the definitive guide. Sebastopol, CA: O'Reilly Media. ISBN 978-1-4493-6354-3. OCLC 878059649.
edit

📚 Artikel Terkait di Wikipedia

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

String (computer science)

formal languages, which are used in mathematical logic and theoretical computer science, a string is a finite sequence of symbols that are chosen from a set

Sergey Brin

of 19. In 1993, he interned at Wolfram Research, the developers of Mathematica. Brin began his graduate study in computer science at Stanford University

Tarek Mansour

from MIT with degrees in mathematics and computer science in 2018. While a student at MIT, Mansour interned at Goldman Sachs in 2016, and later worked

Brad A. Myers

his BS degree in Computer Science and MS degree in Computer Science and Engineering under MIT in 1980, whilst simultaneously interning at Xerox Parc for

California Institute of Technology

Engineering, Computer Science, Electrical Engineering, Mechanical Engineering and Physics. The most popular majors of the class of 2023 were Computer Science, Mechanical

Dylan Field

founded Figma in 2012 with Evan Wallace, whom he had met while they were computer science students at Brown University. In 2012, Field received a Thiel Fellowship

William Hockey

attended Emory University where he majored in computer science and economics. He spent a summer interning at Bain & Co. where he met his future co-founder