Menu

I Only Wanted Six Sums. GroupBy Gave Me 100 MB
📰

I Only Wanted Six Sums. GroupBy Gave Me 100 MB

DEV Community: dotnet·Sukhpinder Singh·about 1 month ago
#T3yHcOyI
#devtodotnet#devto#dev#totals#rows
Long article (1354 words) — no AI summary yet.
Reading 0:00
15s threshold

A reporting endpoint I wrote months ago started turning up in our gen0 collection graphs, and for a while I couldn't work out why. All it does is total an amount column per region and hand back six numbers. Six. Nothing that small should be making the garbage collector sweat, but the allocation counter said otherwise, and that counter has never once flattered me.

The offending line was the one I'd have waved through in review without a second look:

var totals = rows . GroupBy ( r => r . Region ) . ToDictionary ( g => g . Key , g => g . Sum ( r => ( long ) r . Amount ));

Enter fullscreen mode

Exit fullscreen mode

It reads clean and does exactly what it says. And to produce those six sums, it first builds a grouping that holds every single row, bucketed by key, so it can walk each bucket and add things up. I only ever look at the totals.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.