Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Making Code Faster: That Pesky Dictionary

DZone's Guide to

Making Code Faster: That Pesky Dictionary

Oh no! A dictionary ate up almost 50% of an application's performance. Read on to learn how this terrifying problem was solved.

· Performance Zone
Free Resource

It's a fact of life: if you are looking at high-performance code, a dictionary is probably going to be one of those things that will be painful. Let's see how this looks in our current program:

Image title

Are you kidding me?! Here I am worrying about every little bit and byte to try to get the most performance out of the system, and Dictionary is eating up almost 50% of my performance?

Let's look more deeply into this:

Image title

So that's about 3 μs, which is pretty fast...but it isn’t fast enough.

Now, know that there are about 200,000 unique values in the dictionary (look at the Insert calls in the profiler output), and we have some knowledge about the problem space.

The ID that we use has eight characters, so at most, we can have a hundred million IDs. An array of that size would be roughly 762 MB in size, so that is doable. However, we also can be fairly certain that the IDs are generated in some sequential manner, so there is a strong likelihood that we don’t need all of this space.

I wrote the following function:

private static void Increment(ref long[] array, int id, long value)
 {
     if (id < array.Length)
     {
         array[id] += value;
         return;
     }
     UnlikelyGrowArray(ref array, id, value);
 }

 private static void UnlikelyGrowArray(ref long[] array, int id, long value)
 {
     var size = array.Length*2;
     while (id >= size)
         size *= 2;
     Array.Resize(ref array, size);
     Increment(ref array, id, value);
 }

Change the stats to start with an array of 256 longs and run it. The results are nice.

Image title

This is single-threaded, of course, and it is faster than all the previous multi-threaded versions we had before.

Topics:
refactoring ,dictionary ,performance ,speed

Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}