Making code fasterThat pesky dictionary
It is a fact of life, if you are looking at a high performance code, a dictionary is probably going to be one of those things that will be painful. Let us see how this look like in our current program:
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 almost 50% of my performance?
Let us look more deeply into this:
So that is 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 8 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. But 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.
So I wrote the following function:
Changed the stats to start with an array of 256 longs, and run it, the results are nice.
587 ms and allocated 2,576 kb with peak working set of 296,484 kb
And our costs look like:
This is single threaded, of course, and it is faster than all the previous multi threaded versions we had before.