String intern pool |
In some modern programming languages, including Java programming language and C Sharp, the string intern pool is a data structure managed internally by the Platform (computing) or virtual machine to facilitate efficient implementation of certain String (computer science) processing tasks. The pool contains a single copy (called the intern ) of each distinct string that is currently represented by a string Object (computer science) in the system. By invoking a method of the string class (for example String.intern() in Java), the programmer has access to this unique string object.
The most common use for the string intern pool is for doing large numbers of string comparisons. For example, if we have n strings and we wish to find any duplicates, Big-O notation(n^{2}) string comparisons are needed. Unfortunately, string comparison can be slow (especially if the strings are long and don t vary much), and this many string comparisons may be unacceptable. The solution is to use the string intern pool to get the intern of each of these strings, and then we only need to do O(n^{2}) Reference (computer science) comparisons, which are much faster. Even if retrieving the interns is fairly slow (and with proper implementation, e.g. using a hash table, it need not be), only O(n) of these operations must be performed, meaning that for large enough n, using the intern pool will be faster.
One disadvantage of the string intern pool is that since every string object must be checked against the pool at creation, there is some performance penalty even if the intern pool is never used by the application. However, due to the possible benefits, this is generally considered acceptable in the languages in which it is implemented.|
|