CrcLookupTableCache Class
Definition
Thread-safe cache of precomputed CRC lookup tables keyed by width, polynomial, and input reflection - amortizes the per-tuple build cost of BuildLookupTable(int, ulong, bool) across every Crc instance that uses the same CrcStandard.
public class CrcLookupTableCache
- Inheritance
-
CrcLookupTableCache
- Inherited Members
- Extension Methods
Examples
using Bodu.IO.Hashing.Checksums;
// Most callers do not interact with the cache directly - Crc resolves it through Crc.GlobalCache.
var crc = new Crc(CrcStandard.CRC32_ISOHDLC);
// Use a scoped cache for an isolated test, then restore the default.
CrcLookupTableCache previous = Crc.GlobalCache;
try
{
Crc.GlobalCache = new CrcLookupTableCache();
// ... run isolated tests against a fresh cache ...
}
finally
{
Crc.GlobalCache = previous;
}
Remarks
Building a CRC lookup table is cheap in absolute terms but not free, and the same table is needed every time a
Crc instance is constructed for a given CrcStandard.
CrcLookupTableCache memoizes tables under the unique key (width, polynomial, reflectIn): the
first lookup for a tuple builds and stores the table; subsequent lookups return the same shared array.
Default and custom caches. A process-wide default lives at GlobalCache and is used implicitly by every Crc instance - most callers never need to construct a cache by hand. Reach for a custom CrcLookupTableCache only when you want a scoped cache (e.g. per AppDomain, per test fixture) that the global default should not see, or when running diagnostic code that needs deterministic cache state. Custom caches can be installed by assigning to GlobalCache.
Thread safety and aliasing. Internally backed by ConcurrentDictionary<TKey, TValue>; concurrent GetLookupTable(int, ulong, bool) calls are safe and the build delegate runs at most once per key. The returned array is shared across all callers with the same parameters and must be treated as read-only - mutating it would corrupt every Crc instance that uses the same standard.
Constructors
CrcLookupTableCache()
Initializes a new instance of the CrcLookupTableCache class.
public CrcLookupTableCache()
Methods
GetLookupTable(int, ulong, bool)
Returns the cached lookup table for the specified CRC parameters, building it on first access.
public ReadOnlyMemory<ulong> GetLookupTable(int size, ulong polynomial, bool reflectIn)
Parameters
sizeintpolynomialulongThe CRC polynomial.
reflectInbooltrue if input bytes are reflected during CRC processing.
Returns
- ReadOnlyMemory<ulong>
A read-only view of the shared lookup table for the supplied parameter set. The view wraps the cached array directly, so successive calls for the same key observe the same backing storage without further allocation.
Remarks
The returned view is shared across all callers with the same parameters. Returning ReadOnlyMemory<T> prevents the caller from mutating the shared backing array at compile time; internal callers that require a raw ulong array can use Bodu.IO.Hashing.Checksums.CrcLookupTableCache.GetLookupTableArray(System.Int32,System.UInt64,System.Boolean).
Exceptions
- ArgumentOutOfRangeException
sizeis outside the supported range.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |