Remembering the Wires – The Daily WTF

As we saw yesterday, padding can be difficult.

Jasmine received a number of tickets all complaining that the ASP .Net application file was taking too long to download. Users often had to wait up to 15 minutes before their browser finished downloading the results of the web application.

This particular module generated a file by running a string Response.Writes to build the file as part of the HTTP response. This line flowed 200,000 times in the file creation process:

myResponse.Write("P;P#,##0." + new string('0', 2) + "_);;[Red]\\(#,##0." + new string('0', 2) + "\\)\n");

It’s not entirely clear to me what they’re writing here – it looks like some kind of format template. But the content doesn’t really matter. String concatenation operations appear to handle decimal places- 0.00. Considering that new string is hard coded to two zeros, no need to concatenate the strings, they could just put 00 exactly in the output string.

But for fun, let’s count the arrays this operation creates, noting that arrays are immutable in C#, so each concatenation creates a new array.

First, let’s give literals credit – C# is smart, and string literals only create one instance, so we can ignore them. The new string operation creates two new strings. Then each chaining creates a new sequence – four.

That’s a total of 6 new string instances, which isn’t a lot, but considering that if they just made a literal, there wouldn’t be 0 new string instances, it’s still a significant increase. But this line is executed in a loop, 200,000 times, which means we’re spamming 1,200,000 instances of the string in each execution of that loop. And this line is a representative line; the whole loop has many such lines, which allows this code to stress-test the garbage collector quite effectively.

Without any changes to the logic and just switching to string formatting and string builders, Jasmine was able to reduce the generation of this file from 15 minutes to a few seconds.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *