Optimizing select projectionsPart I
I spoke about designing for performance in my previous post and I thought it would be in interesting series of blog posts. The task I have is that we have a data source of some kind, in our case, I decided to make the task as simple as possible and define the source data as a Dictionary<string, object>, without allowing nesting of data.
We want the user to be able to provide a custom way to project data from that dictionary. Here are a few ways to do that:
And here is the data source in question:
Obviously the user can make the select projections as complex as they wish. But I think that these three sample give us a good representation of the complexity. We’ll also treat them a bit differently, with regards to their likelihood. So when testing, we’ll check that the first projection is run 10,000 times, then second projection is run 5,000 times and the last projection is run 1,000 times.
With the layout of the land settled, let us see how we can get this actually working. I’m going to use Jint and solve the issue in a very brute force manner.
Here is the initial implementation:
And this runs in 4.4 seconds on the 10K, 5K, 1K run.
- 10K in 2,756 ms
- 5K in 1,310 ms
- 1K in 314 ms
I’m pretty sure that we can do better, and we’ll look at that in the next post.