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

-8

u/Beaverman May 25 '19

I don't understand linq and streams. What's wrong with the imperative for loop? How is it any easier to read than a for loop and a variable?

Linq and streams are not how the computer executes your code, it's not how you'd write the math. What's the point?

0

u/[deleted] May 25 '19

[deleted]

→ More replies (7)

0

u/[deleted] May 25 '19

[deleted]

→ More replies (3)

6

u/mrjackspade May 25 '19

Using a for loop is fine for something like a sum, but Linq is significantly easier to write and maintain when dealing with more complex generic functions, or chaining things together. Being able to define a Where filter implementation for an unknown iqueriable provider of an unknown underlying set type and call it with ~4 lines of code is a godsend, and being able to use Linq to chain together function calls for generic sets based on parameters is significantly more readable than writing a 500 line block of "if" statements.

When you're dealing with Linq you're talking about a lot more than just the syntax. It's an access system for dealing with IQueriables, IEnumerables, ICollections, etc, and interacting with those in an intuitive way using expression trees/delegates/etc.

If you don't understand the benefit it's likely just because you haven't written code yet that would see a benefit to using it.

2

u/Beaverman May 25 '19

Do you have a real world example we could discuss? That might be easier to talk meaningfully about instead of hypotheticals. I sums because it has examples directly in OP.

What sort of problems are you solving where you need to query unknown shit in unknown ways? Do you think you might have overgeneralized and overcomplicated the problem in some way?

But does it make sense to combine enumerables and queryables? Is accessing a list the same thing as accessing sets in a database? I'd say no.

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.

2

u/accidentalginger May 27 '19

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.

→ More replies (1)