Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of String.IsInterned?

Tags:

string

c#

.net

In the String class there is a method IsInterned(). I never use this method. Please help me to understand the best uses of this method.

like image 977
Vijjendra Avatar asked Jun 21 '11 05:06

Vijjendra


People also ask

What is an interned string object used for?

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- or space-efficient at the cost of requiring more time when the string is created or interned.

What is string intern in C#?

The Intern method uses the intern pool to search for a string equal to the value of str . If such a string exists, its reference in the intern pool is returned. If the string does not exist, a reference to str is added to the intern pool, then that reference is returned.


1 Answers

Consider that interning is an optimisation; it trades a decrease in some desirable qualities for an increase in others. In particular interning has the following good qualities:

  1. Memory is not wasted on duplicate strings.
  2. Equality comparison between strings known to both be interned is extremely fast.
  3. Equality comparison between strings that happen to be interned is still much faster than if they aren't interned.
  4. Other comparisons get a performance benefit in some cases.

It has the following bad qualities:

  1. Strings are not garbage collected as often (if at all), so memory that could be reclaimed is used on strings that are never seen again, or for a very long time. (Intern all your strings and you could end up with really nasty memory use).

As an optimisation, we use it where the either the good qualities out-weigh the bad or where the bad qualities don't hold (if we know the string is going to be around for the lifetime of the application anyway, or know it will be used many times, then the bad part doesn't hold).

And by the same token we don't use it where the bad qualities will out-weigh the good. (Most of the time).

IsInterned() can be used to find a part-way point.

Consider I have a string property Name:

public string Name { get; set; }

Let's say I know that it's common to look for objects with a given Name, or to try to find objects with the same Name or otherwise do a lot of equality comparisons on it. OR Let's say I know there will be a lot of other objects with the same Name. OR Both.

In these cases I might consider interning:

private string _name;
public string Name
{
  get { return _name; }
  set { _name = string.Intern(value); }
}

Of course, whether this was a good idea or not depends on the good and bad qualities of interning mentioned above.

In-between using and not using is the possibility:

private string _name;
public string Name
{
  get { return _name; }
  set { _name = string.IsInterned(value) ?? value; }
}

Here if the string value is already interned then we the down-sides of interning are already at work and we don't suffer any more, so we take advantage of it. But if value is not already interned then we just use it as-is.

This is also an optimisation, that optimises for a different case. It only benefits if a reasonable number of the values seen are likely to be interned by some other code (or because they match literals in the assembly), otherwise it just wastes time doing lookups. It's probably less often useful than Intern() which in turn is less often useful than just using the strings and ignoring interning, but that does show a time when it could be useful.

like image 116
Jon Hanna Avatar answered Oct 29 '22 06:10

Jon Hanna