Yan Cui
I help clients go faster for less using serverless technologies.
This article is brought to you by
MongoDB 8.0 is here to change the game. Faster reads and inserts, and brand-new vector search to support modern AI-powered apps.
I did some performance optimization work a little while back, and one of the changes which yielded a significant result was when I migrated some server side components (which are CPU intensive and performs a large number of loops) from using ADO.NET DataSets to using POCOs (plain old CLR object).
The looping was then done using LINQ to Objects, and I discovered a nice little extension to LINQ called i4o – which stands for Index for Objects – to help make the loops faster. However, I wasn’t able to observe any difference in performance, which contradicts with the findings on Aaron’s Technology Musing…
Digging a little deeper into the i4o source code (admittedly I didn’t do this myself, credit to Mike Barker for doing this!), it turns out that there are a number of drawbacks in i4o which aren’t immediately obvious or mentioned anywhere in the documentation. The biggest problem for us was that it only supports equality comparison, which means it would simply ignore the index you have on the MatchID property if you try to run this query:
var result = from m in Matches where m.MatchID >= 1 select m;
but it’ll use the index on MatchID if you run this query instead:
var result = from m in Matches where m.MatchID == 1 select m;
The conclusion?
i4o is an awesome tool that can turbo boost your LINQ query, but ONLY put indices on properties which you will be doing equality comparison in your queries otherwise you’ll just be wasting some memory space holding indices which would be used at all.
Whenever you’re ready, here are 3 ways I can help you:
- Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
- I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
- Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.