Skip to content
Tutorials Center
My learning

Topics

Lesson 111 of 182 ยท Memory and Performance

Fast Lookups: SearchValues, FrozenDictionary and Caching

Speed up repeated lookups in C# with SearchValues, FrozenDictionary and FrozenSet, MemoryCache, Lazy<T> and ArrayPool, and learn when each tool is worth its complexity.

A surprising amount of program time goes into asking the same questions again and again. Is this character one of the delimiters? Which handler belongs to this command name? What is the price for product 42, which we already fetched a moment ago? The standard answers, a switch, a Dictionary, a plain loop, are fine for most code. This lesson covers the next gear: tools in the .NET library built for data that is built once and read many times, plus the patterns for not repeating expensive work at all.

You will meet SearchValues<T> for finding any of a set of characters or strings in text, FrozenDictionary and FrozenSet for read-only lookup tables, MemoryCache for caching results with expiry, Lazy<T> for creating a costly object only when it is needed, and ArrayPool<T> for borrowing big arrays instead of allocating them. Read measuring performance first: none of these tools should be used before you know a lookup is a real cost.

SearchValues: find any of a set quickly

Suppose you want to check whether a string contains any character from a set such as "<>&\"'". The simple way is text.IndexOfAny(['<', '>', '&', '"', '\'']). That works, but each call has to analyse the set again to choose the best algorithm. SearchValues<T> (in System.Buffers, .NET 8) does that analysis once, at creation, and the resulting object is then used with the span methods IndexOfAny, ContainsAny and friends. It picks vectorised (SIMD) code or a bitmap, depending on the set.

C# SearchValues for characters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

.NET 9 added SearchValues<string>, which finds the first of several substrings, optionally ignoring case. This is useful for scanning logs or text for keywords.

C# SearchValues for several words

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Use SearchValues when the set is fixed and the search runs often (parsers, escapers, tokenizers, validators). For a one-off search, IndexOfAny with a short array is fine.

Frozen collections: build once, read fast

A Dictionary<TKey, TValue> is built to support adding and removing. That flexibility costs a little on every lookup. When your table is filled once at startup and never changes (command names, HTTP status text, keyword sets, configuration), FrozenDictionary<TKey, TValue> and FrozenSet<T> from System.Collections.Frozen (.NET 8) can take extra time to build in exchange for faster reads. They analyse the keys and pick a specialised strategy.

C# FrozenDictionary and FrozenSet

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

There is no Add, Remove or indexer setter, and you cannot change a frozen collection after creation. The usual place for one is a static readonly field:

C#
private static readonly FrozenDictionary<string, Command> s_commands = BuildCommands();

Do not build a frozen collection inside a method that runs often: construction is more expensive than for a normal dictionary. The payoff comes from creating it once and reading it many times. If the table is small (a handful of entries) or short-lived, a plain Dictionary or even a switch is perfectly good. Measure before switching.

Caching with MemoryCache

A cache keeps the result of expensive work (a database query, a web call, a heavy computation) so the next request can reuse it. The key decisions are the key, how long an entry stays valid, and how many entries you allow. Microsoft.Extensions.Caching.Memory.MemoryCache handles expiry and size limits for you. It is part of the shared framework, so it works in the editor.

GetOrCreate returns the cached value or runs your factory once to produce it:

C# MemoryCache with GetOrCreate

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Four reads, but only two "database" calls. Things to know before relying on a cache:

  • Expiry. Use AbsoluteExpirationRelativeToNow (fixed lifetime) and/or SlidingExpiration (renewed on each access). Without one, entries live until memory pressure or removal.
  • Size limit. Set SizeLimit in the options and give each entry a Size, or the cache can grow without bound (the classic memory leak, see garbage collection).
  • Stale data. A cached value may be out of date. Decide how stale is acceptable, and invalidate with cache.Remove(key) when the source changes.
  • Cache stampede. GetOrCreate does not lock: if two threads miss at the same moment, both run the factory. For expensive factories, cache a Lazy<T> or a Task<T> instead, or use SemaphoreSlim per key.
C# A size-limited cache refuses extras

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

The cache never exceeds SizeLimit: when an insert would go over it, the entry is not stored (and a compaction may evict others). The output shows that the third item was not kept.

In a web application you register the cache with dependency injection (services.AddMemoryCache(), see dependency injection) and inject IMemoryCache. For multiple servers, use a distributed cache such as Redis, which needs extra packages and is outside this lesson.

Lazy initialization with Lazy of T

Sometimes the expensive thing is an object itself: a parsed configuration, a big lookup table, a connection. If it may never be needed, do not create it at startup. Lazy<T> creates the value on first access and remembers it. By default it is thread-safe: even if many threads ask at once, the factory runs once.

C# Lazy of T runs the factory once

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

For a lazily created singleton field, Lazy<T> is cleaner than hand-written double-checked locking. For static data, a static readonly field is already lazy: the CLR initialises it right before first use of the type.

ArrayPool: borrow instead of allocate

Large arrays (85,000 bytes and up) go to the large object heap and are collected only with gen 2 collections. If a service allocates a fresh 1 MB buffer per request, memory churn and pause times increase. ArrayPool<T>.Shared lets you rent a buffer, use it, and return it, so the same arrays are reused.

C# Renting and returning a buffer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Rules for pooled arrays:

  • The array may be bigger than you asked for. Always track the length you actually use (buffer.AsSpan(0, count)), never rely on buffer.Length.
  • Contents are not cleared on Rent, so old data may be there. Pass clearArray: true on Return when the data is sensitive.
  • Return it exactly once and never touch it afterwards. Using a returned array, or returning it twice, causes subtle data corruption that is very hard to debug.
  • Use try/finally to guarantee the return.
  • Pooling small, short-lived arrays is usually slower than allocating them. For small buffers use stackalloc (spans).

Choosing the right tool

Problem Tool
Find any of a fixed set of characters or words in text, often SearchValues<char> / SearchValues<string>
Fixed table, built at startup, read constantly FrozenDictionary / FrozenSet
Table that changes Dictionary / HashSet (dictionaries, sets)
Repeated expensive result that can be slightly stale MemoryCache with expiry and size limit
Expensive object that may never be needed Lazy<T>
Large temporary buffers in hot paths ArrayPool<T>
Small temporary buffer stackalloc + Span<T>

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does SearchValues.Create do that a plain IndexOfAny(char[]) call does not?

    The set is analysed at creation and the resulting object is reused, often with vectorised code. Keep it in a static readonly field rather than creating it per call.
  2. 2. When is FrozenDictionary a good choice?

    Frozen collections spend extra time at creation to make reads faster, and they cannot be modified. Building one repeatedly wastes the benefit.
  3. 3. Why should a MemoryCache normally have expiry and a size limit?

    An unbounded, never-expiring cache is effectively a memory leak and serves outdated values. Expiration and SizeLimit keep it under control.
  4. 4. What is true about a buffer returned by ArrayPool<byte>.Shared.Rent(1000)?

    Rent can return a larger array, does not clear it, and the caller must return it exactly once and stop using it afterwards.
  5. 5. How many times does a default Lazy<T> run its factory if 10 threads read Value at the same time?

    By default Lazy<T> uses ExecutionAndPublication thread-safety mode: one thread runs the factory and the others receive the same result.

Summary

  • SearchValues<T> precomputes a fast search for a fixed set of characters or strings; create it once and reuse it with span methods like IndexOfAny and ContainsAny.
  • FrozenDictionary and FrozenSet trade slower creation for faster reads; use them for static tables, not for hot-path construction.
  • MemoryCache caches expensive results; always set expiry and a size limit, and think about stale data and stampedes.
  • Lazy<T> delays creating costly objects until first use and is thread-safe by default.
  • ArrayPool<T> reuses large buffers; track the length you use, return exactly once, and do not pool tiny arrays.
  • Measure first: these tools help when the numbers say a lookup or allocation is a real cost.

Next: Unsafe Code and Interop