Powered by Mono

The importance of micro optimizations

by Marek Habersack 7. October 2009 12:54

Many years ago when coding in C/C++ or assembler I learned one important rule - DO micro-optimize your code. Unlike macro scale optimization, which should be done after your code is generally working and feature complete, micro optimization should be applied as you go. Why? Because sticking to a few simple rules (which don't affect the overall way in which the code works) as you code is easier than having to fish for the small inefficiencies in your source later on. Some of the optimizations can be performed by the compiler (like taking a constant expression out of loop) but some still depend on the coder doing the Right Thing™.

It seems that, a month ago, I proved that I had forgotten the rules I learned and applied in the past by committing code which affected Mono ASP.NET's performance in a really major way. I'm posting this blog entry as a memento to myself and to others who, like me, might have forgotten that such rules still apply in the 21st century despite all our smart compilers, sophisticated virtual machines and runtimes.

The committed code did (eventually) something like this:

void Method (string s)
{
string s2 = GetSomeString ()

CallMethod (s + '@' + s2);
}

The CallMethod line caused (as we discovered yesterday) a drammatic decrease in MojoPortal's performance - its home page would open on my computer, using local connection in 2.56s (yes, seconds) on average. Gonzalo went hunting for the issue and this morning I read a mail from him pin-pointing (after lots and lots of time wasted on it) the performance loss to that very line. I fixed the issue in our code and suddenly the load time for the page went down to ~107ms (yes, milliseconds)! One might ask whether it was because concatenating two strings and a char is so inefficient? Yes, it is inefficient (as the operation has to convert a char to a string, then allocate and concatenate two more strings) but not enough to justify such performance loss. The key here is the frequency at which the code is being called - GetSection gets called hundreds (if not thousands) of times during one request, and the operation's summed up time contributes to the peformance loss.

This is exactly what I described at the beginning of this post - a micro optimization, a right thing to do in the right place. It can be compared to adding two integer or float variables whose values don't change in a loop which takes thousands of iterations and is invoked by some code very often. The addition is not very expensive if your code performs it once in a while, but in the scenario when it repeatedly calls the code, you will see major loss of performance. The solution is to take the operation outside of the loop. This is precisely what happened here.

So, even in this day and age - ladies and gentlemen, do micro-optimize and pay attention to where your "slightly inefficient" code lives :)

Update:

As Jon suggested below, I should have suggested possible solutions to the issue:

  • CallMethod could take 3 arguments instead of one (depends on what the method does, of course) - that was the solution chosen in this instance to fix the issue as the string concatenation wasn't at all necessary.
  • Using "@" instead of '@' would avoid costly (culture-sensitive) conversion of a char to string

Update:

I'm officially an idiot. The real cause of the slowness was that we've been bypassing the cache because of different key being generated to query cache and another to insert into it. My only excuse - lack of sleep :P (I know, a poor one :D). The post's main point still stands, though - just ignore the rambling about string concatenation. Now, where's my brown paper bag...

Tags:

General | Mono | ASP.NET

Book meme, per instructions :)

by Marek Habersack 18. May 2009 19:50

Following a fellow hackerette's instructions, here's my meme:

"Computer science has a subdiscipline called Information Retrieval 
(IR for short) that focuses almost entirely on this problem."

And the original Andreia's instructions copied here:

  • Grab the nearest book.
  • Open it to page 56.
  • Find the fifth sentence.
  • Post the text of the sentence in your journal along with these instructions.
  • Don't dig for your favorite book, the cool book, or the intellectual one: pick the CLOSEST.

So, Andreia, what's the page number for next week? :)

Tags:

General | fun

Back to blogging

by Marek Habersack 13. May 2009 02:17

So, it has been over a year since I last blogged. A lot has happened in the 15 months in Mono - we have released version 2.0 and continued new releases all the way to the latest 2.4 which came with lots of performance and stability improvements. ASP.NET in Mono now has support for almost all .NET 3.5 controls (except for the LinqDataSource which is not fully implemented, but it's on its way), we have System.Web.Routing, beginnings of System.Web.DynamicData (more on that in some later post) and integrated System.Web.Mvc after Microsoft released it under an opensource license. But you are all well aware of those events, so there's no point in continuing the list here.

As I feared, I wasn't the most active blogger, for various reasons. This time, though, I do hope to keep you updated on what's going on in ASP.NET in Mono and, perhaps, about other things I find interesting. So, check back from time to time and maybe I'll manage to put together a few words which make sense :)

Tags:

Mono | General

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen | Modified by Mooglegiant