Skip to content

Interning Strings

abe545 edited this page Oct 29, 2014 · 1 revision

#Interning Strings

By default, two instances of the exact same string will be duplicated in memory. Ordinarily, this is not an issue. However, sometimes these duplicate strings can take up more memory than you would like. If your StoredProcedure returns a lot of duplicate strings, you can reduce this memory overhead by calling String.Intern() on a given string. Since that is a lot of boilerplate code that most of us wish to avoid (auto-props are great!), I've added the Intern attribute. By adding it to your string property, the value will be automatically interned for you:

public class Model
{
    [Intern]
    public string Name { get; set; }
}

Interning All the Strings

If every string property that your stored procedure returns has a number of duplicates, you can apply global interning to all string properties by calling the WithDataTransformer method:

var results = StoredProcedure.Create("usp_getDuplicateStrings")
                             .WithDataTransformer(new InternAllStringsTransformer())
                             .WithResults<string>()
                             .Execute();
Clone this wiki locally