FC65: Expensive NOP

Just wasting resource

There is an interesting performance anti-pattern I call expensive NOP, a piece of code which does nothing useful, just wasting resource. Found one such case in PerfView code base:

Source code:

The most expensive part here is the regular expression Replace call. Notice this form is using a pattern string, so a regular expression object needs to be allocated every time (in .Net Framework, .Net Core has optimization for it).

Notice also according to the comment, the regular expression is designed to replace double quote character. But there is a check before it, the regular expression is not used when double quote character is not found. So it’s completely useless.

The result of the replacement and string.Concat call is appended to a StringBuilder. This is a bad API design. Here is proper implementation:

If you measure your code properly, you can find such funny costly code.