Ayende @ Rahien
Read post

"Optimizing" concurrent regexes

A .NET performance investigation reveals that sharing a compiled Regex instance across concurrent threads causes significant slowdowns — up to 6x — due to how the .NET Regex engine manages its internal RegexRunner state. The runner is checked out exclusively per call using Interlocked.Exchange, so concurrent callers must allocate new runner instances, causing heavy GC pressure. The issue affects both RegexOptions.Compiled and source-generated regexes. The workaround is to use ThreadLocal<Regex> to avoid concurrent access, at the cost of higher memory usage. A GitHub issue has been filed against the .NET runtime.

    #performance#.net#c#
Jun 19•4m read time•From ayende.com
Post cover image
42.4K Impressions
Ayende @ Rahien's image
Ayende @ Rahien

Ayende is a software engineer, blogger, and open-source contributor known for his expertise in softw...

44 Followers

•

626 Upvotes

Would you recommend this post?

Copy link
WhatsApp
Facebook
X
New Squad
  • © 2026 Daily Dev Ltd.
  • Guidelines
  • Explore
  • Tags
  • Sources
  • Squads
  • Leaderboard