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.
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.
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.
You seem stuck in the idea that you and you alone can be the only one that is right. I’ve been in all roles of an engineering function, from junior to principal to management to founder. You’re missing the largest cost of an engineering practice: headcount. I’m going to take an educated guess and say you’re probably a senior or principal engineer, judging by your specific arguments. I’ll reformulate my approach.
Hand optimized will almost always be faster, whether it’s to replace the duties of an ORM, a multi-tiered cache, or even pure algorithms. You can regress this argument infinitely, however, to the point of justifying writing assembly or offloading to FPGAs or ASICs. Here’s the thing: these are optimizations to bottlenecks. They suck as general solutions. If you have a crazy query built off of joining five different tables from two separate databases with specific ordering and query criteria, yeah, LINQ to SQL or Entity Framework or NHibernate or anything else out there will totally shit the bed. At that point, you would need to optimize. But stored procedures are supported by these frameworks, and you can still leverage the strengths of the abstraction to provide greater clarity. Clarity is key here. Developers are expensive, and spending 80 hours to write the most optimal SQL for every situation is insane to justify for businesses that are small or have rapidly shifting requirements — and ensuring that the other engineers can even quickly understand that optimization is also expensive, either because the headcount is staffed by mythical 10Xers that can quickly grok it, or because the amount of effort spent in understanding it took 10 times as long. Companies with billions in their coffers can afford that, but even they lose velocity, which harms the competitive angle. Or, instead of being another victim to the vice of premature optimization, you can have a cleanly abstracted DAL that still lets you leverage the use of LINQ across multiple data sources without sacrificing perf because the specific places where perf is being damaged by the AST compiler can be mitigated with hand optimization for those calls.
Bottom line: LINQ enhances readability generally more than hand optimized code. Readability matters more often than slight performance gains, and performance gains can be made by optimizing where needed.
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.