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.
Join the DZone community and get the full member experience.
Join For Freeit'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:
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:
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.
this is single-threaded, of course, and it is faster than all the previous multi-threaded versions we had before.
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Superior Stream Processing: Apache Flink's Impact on Data Lakehouse Architecture
-
What Is JHipster?
-
Leveraging FastAPI for Building Secure and High-Performance Banking APIs
-
How To Ensure Fast JOIN Queries for Self-Service Business Intelligence
Comments