- Frugal Cafe
- Posts
- FC68: Finding the perfect place to cache a string
FC68: Finding the perfect place to cache a string

Continue with optimizing PerfView’s xml generation performance, finding this expensive allocation stack:

TraceEventStackSource.GetFrameName is responsible for 43% allocations, 495 mb in total. Here is the source code:

Notice the expensive string generated is only depending on a single object, a TraceProcess object. So TraceProcess class is the perfect place to cache the string. We just need to add a new property:

The FrameName property is calculated on first use, and then reused.
This is an effective caching mechanism, both for CPU and memory allocation. But in certain scenarios, this would be a working set nightmare. Luckily, this should not affect PerfView because there are limited number of TraceProcess objects, so added memory usage is not an problem.