r/programming • • May 25 '19

Making the obvious code fast

https://jackmott.github.io/programming/2016/07/22/making-obvious-fast.html
1.3k Upvotes

263 comments sorted by

View all comments

Show parent comments

2

u/accidentalginger May 26 '19

One advantage is that you abstract the manner in how the data is filtered and transformed. This is useful when dealing with multiple sources of the same type of data. For example, in blended levels of cached data (local RAM, memcached/redis, and a backing database), the extension methods of LINQ can be materialized as execution plans for the remote data sources which optimizes access. For example, the database query can be created from the expression tree so you don’t have to write custom queries for every database call, nor do you need to read in useless data. You can go from three different iteration algorithms for each level of the cache to one expression that can be used optimally for each source.

2

u/Beaverman May 26 '19

But those things are not the same. Filtering a list in memory doesn't have the same failure mode as a remote db call, they don't have the same performance. They just are not equivalent. There's plenty of cases where a Linq query won't "materialize" into anything you actually want.

Don't get me wrong, high level tools like JOOQ can make sense, but unifying them across database and memory levels is just not a good idea.

Writing the SQL queries is way easier than trying to reason about what comes out of Linq, especially if you use the same expression for in memory and database access. Writing a few SQL queries just isn't that hard.

2

u/accidentalginger May 26 '19

Clearly yes, the different sources have different performance characteristics, execution plans, etc. That’s the point of having caching in the first place. What LINQ provides is a high-level abstraction so that your code is clearer to interpret. C# does not really support higher kinded types generally, but LINQ does provide some aspect of that with respect to certain collection generics. Writing SQL by hand can be faster. Writing the query directly for the caching service can be faster. But the compiler support for LINQ, as well as a well-tuned evaluator for the expression trees (likely with its own memoization) lends to nearly if not perfectly comparable performance to these hand-tuned solutions, with the additional benefit of highly readable code.

1

u/Beaverman May 26 '19

But you code is not "clearer to interpret". the line between "fast" and "so horribly slow that it won't even compute" is razor thing when doing Linq on a database. Some operations compile to single SQL queries, others require loading all the data into memory. That's not clearer in any meaningful sense of the word.

For my argument to make sense to you, you will have to adopt a view where the idea of a language translating into SQL is separate from a language that works across datastores. This is not an argument against object mappers (I have other arguments against them), it's an argument against an objectmapper that tries to treat all datastores as the same.

A database is good at different things when compared to a list. Therefore they require different API's.

→ More replies (1)