Beizsoft.Interning 1.0.0

An interning/reference-sharing library.

Install-Package Beizsoft.Interning -Version 1.0.0
dotnet add package Beizsoft.Interning --version 1.0.0
<PackageReference Include="Beizsoft.Interning" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Beizsoft.Interning --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Beizsoft.Interning, 1.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Beizsoft.Interning as a Cake Addin
#addin nuget:?package=Beizsoft.Interning&version=1.0.0

// Install Beizsoft.Interning as a Cake Tool
#tool nuget:?package=Beizsoft.Interning&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Beizsoft.Interning

A .NET interning/reference sharing library.

What?

Interning is the re-use of references for classes with the same content. There are many cases where interning might be useful, but the simplest use-case is for entirely immutable data classes. While .NET comes with a built-in interning implementation for strings, any other immutable class could be interned.

There are two major benefits to interning:

  1. Memory usage.
  2. Comparison performance.

1 should be self-explanatory; if you re-use the same reference for identical classes, you are, of course, going to end up with lower memory usage. 2 might not be quite so obvious: if your class is interned by default, all comparisons can be simple, performant ReferenceEquals calls. Hashing can be a simple RuntimeHelpers.GetHashCode call, or hashes can be pre-calculated once, and only once, for all given instances of a class.

How?

There are code examples below in the Usage section, but for an overview, this library uses the concept of an 'interning key' (a read only struct containing all the data required to build a class) which lives on the stack, and a dictionary-based lookup to ensure only once instance is used for any given reference state.

Classes can be interned-by-default by giving them a private constructor, and a static For method in the constructor's place, or they can be interned as required by allowing non-interned construction via a public constructor.

Usage

The repository contains simple examples of how to use the library.

Users may choose to change the interning provider for any given interned class/interning key pair, with the default being a concurrent dictionary-based implementation, and the alternatives being a simple unsynchronized dictionary, and an implementation which uses a ReaderWriterLockSlim to synchronize interning.

To change the static interning provider, users must call the Intern<T1,T2>.Initialize method with ConcurrentInterner<T1,T2>.Instance, LockedInterner<T1,T2>.Instance or UnsynchronizedInterner<T1,T2>.Instance. Custom implementations can also be enabled via this mechanism.

Should the user attempt to change the static interner after any interning has occurred, an InvalidOperationException will be thrown.

Because the built-in interning providers are public, and it is perfectly possible for users to implement their own interning providers, users may choose, if they find a use-case, to have 'sharded' interning, where they use different interning providers directly (even if their code is identical) to have separate instances in different contexts.

Beizsoft.Interning

A .NET interning/reference sharing library.

What?

Interning is the re-use of references for classes with the same content. There are many cases where interning might be useful, but the simplest use-case is for entirely immutable data classes. While .NET comes with a built-in interning implementation for strings, any other immutable class could be interned.

There are two major benefits to interning:

  1. Memory usage.
  2. Comparison performance.

1 should be self-explanatory; if you re-use the same reference for identical classes, you are, of course, going to end up with lower memory usage. 2 might not be quite so obvious: if your class is interned by default, all comparisons can be simple, performant ReferenceEquals calls. Hashing can be a simple RuntimeHelpers.GetHashCode call, or hashes can be pre-calculated once, and only once, for all given instances of a class.

How?

There are code examples below in the Usage section, but for an overview, this library uses the concept of an 'interning key' (a read only struct containing all the data required to build a class) which lives on the stack, and a dictionary-based lookup to ensure only once instance is used for any given reference state.

Classes can be interned-by-default by giving them a private constructor, and a static For method in the constructor's place, or they can be interned as required by allowing non-interned construction via a public constructor.

Usage

The repository contains simple examples of how to use the library.

Users may choose to change the interning provider for any given interned class/interning key pair, with the default being a concurrent dictionary-based implementation, and the alternatives being a simple unsynchronized dictionary, and an implementation which uses a ReaderWriterLockSlim to synchronize interning.

To change the static interning provider, users must call the Intern<T1,T2>.Initialize method with ConcurrentInterner<T1,T2>.Instance, LockedInterner<T1,T2>.Instance or UnsynchronizedInterner<T1,T2>.Instance. Custom implementations can also be enabled via this mechanism.

Should the user attempt to change the static interner after any interning has occurred, an InvalidOperationException will be thrown.

Because the built-in interning providers are public, and it is perfectly possible for users to implement their own interning providers, users may choose, if they find a use-case, to have 'sharded' interning, where they use different interning providers directly (even if their code is identical) to have separate instances in different contexts.

Release Notes

https://github.com/iBeizsley/Beizsoft.Logging.Console/releases/tag/1.0.0

NuGet packages (3)

Showing the top 3 NuGet packages that depend on Beizsoft.Interning:

Package Downloads
Beizsoft.Names
A string-ID library.
Beizsoft.Factories.Interned
An interning extension for Beizsoft.Factories.
Beizsoft.Equations
An equation library.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.0 182 6/23/2021
1.0.0-rc1 140 6/19/2021