Hacker News
Push ifs up and fors down: The idiom, its algebra, and its limits
socializer
|next
[-]
swiftcoder
|root
|parent
|next
[-]
ninalanyon
|next
|previous
[-]
Speed was almost never the reason.
wallstop
|next
|previous
[-]
Of note, as of C#9 (and maybe prior), the dotnet runtime does this automatically whenever it is deemed safe. https://devblogs.microsoft.com/dotnet/performance-improvemen...
The same technique is applied as an optimization, when deemed safe, in all current gen c compilers (gcc, llvm, etc).
I'm very confused why neither measurements nor references to when this is done automatically in most modern languages is included in the article.
cogman10
|root
|parent
[-]
For example:
var map = new HashMap<String, String>();
map.put("foo", "bar");
for (var i : items) {
if ("bar".equals(map.get("foo")) {
doStuff(i);
}
}
Even though `map` isn't mutated, it's hard enough for the JVM to detect and the underlying `get` functions are complex enough that it'll run the `get("foo")` every time, which can be quiet expensive.
dieselgate
|next
|previous
[-]
mahboi
|root
|parent
|next
[-]
They're one of those good practices that look like bad practice to everyone who just got a CS degree. Seems ex-students are unsettled by asymmetry or want to minimize the number of return statements.
woadwarrior01
|root
|parent
|previous
[-]
https://docs.swift.org/latest/documentation/the-swift-progra...
OutOfHere
|next
|previous
[-]
pdpi
|root
|parent
|next
[-]
taolson
|root
|parent
|previous
[-]
https://github.com/taolson/Admiran/blob/main/examples/fizzBu...
/s
alterom
|next
|previous
[-]
"the loop runs without a branch, and is a candidate for vectorization".
That's it, that's the article. This matters a lot in huge-scale / scientific computing / HPF, where if you can express something as an operation on vectors on matrices, you win big (those ops parallelize well, can be run on GPUs, clusters, what have you).