169 days ago · Tech · hide · Fediverse post

I am looking into some regex work, and I ran into a performance problem. I need to run a particular regex over a large number (millions) of strings. That caused my spidey sense to… tingle. The code in question looked something like this:long matches = CountMatchingEntries(new Regex(@"\s+user id\s+"));Creating a regex for each invocation is… expensive. That is why we have the RegexOptions.Compiledflag, after all. And the Regex class is thread-safe, so I did the equivalent of this code:// class level static Regex s_regex = new Regex(@"\s+user id\s+", RegexOptions.Compiled); // inside a method long matches = CountMatchingEntries(s_regex);The performance of the system immediately took a big, stinking performance regression all over my benchmarks. At first, I was sure that I wasn’t doing something properly, so I re-wrote this using the modern approach, with source generators, like this:public partial class MyClass { <span class="token punctuation">[</span><span class="token…

Commenting on Bubbles has been removed. Why? If you want to talk about this post, write to the author, use the comments on their blog if it has them, or reply with a "RE:" post on your own blog. Suggestions? feedback@bubbles.town