Live data from Hacker News

Branchless Rust: Making a Filter 4x Faster by Removing an If

greyblake.com

51–60 of 124 posts

Re: Branchless Rust: Making a Filter 4x Faster by Removing an If

#51

Earlier quoted context omitted.

Nice! I saw the code and thought, I bet there’s a way to do some SIMD here… never touched intrinsics in Rust before so I really appreciate you writing it up!

See the following pdf for example on how to do this with SSSE3 (pages 104-133) or even SSE2 (pages 151-173) https://deplinenoise.files.wordpress.com/2015/03/gdc2015_afr...

Great link, nice find!

Re: Branchless Rust: Making a Filter 4x Faster by Removing an If

#52

This article is 100% AI written. The data was interesting, the commentary overly verbose and hard to gain useful insights from.

It was fun to read and insightful for me, not too artificial, and not too verbose.

I'm glad my internal AI detector doesn't win over my curiosity to learn.

Re: Branchless Rust: Making a Filter 4x Faster by Removing an If

#53
I like how we have pretty much established how branchless coding is superior to branched coding. However I wonder if the compiler itself could recognize these patterns and turn branches into branchless instead, rather than making the code harder to read? as removing if conditions of course have a readability impact on the code.

Re: Branchless Rust: Making a Filter 4x Faster by Removing an If

#54

Earlier quoted context omitted.

I heard there's a way to arrange code such that the compiler can autobectorize easier. I wonder if there's a way to do that here? Would probably have to pass `-C target-cpu=native` to cargo so that llvm is allowed to use AVX512.

Good question. I personally doubt that the compress instruction is easy to coax compilers into generating, as there are many edge cases to consider. For example, you'll notice here that we perform a full vector store of 8 elements unconditionally, even if only a few of the elements are active. This is safe , though, because the output buffer is as large as the input buffer, and we're chunking by 8, so we'll never tra…

I think WUFFS "iterate loops" might help. WUFFS requires that processing a chunk of N items has code to process one at a time, which means it'll work for any N. However you can optionally provide specialisations for doing K at a time and the compiler is responsible for carving the input up as appropriate so e.g. N = K + K + 1 + 1 + 1 your K-at-a-time code runs twice, the extras are handled 1-at-a-time.

So this divides up the problem, the compiler can vectorize your 8-at-a-time code without needing to handle edge cases where N isn't a multiple of 8, and if a later pass notices we actually never end up using those edge cases they're dead code, if it doesn't they're just a rarely-taken branch once.

Re: Branchless Rust: Making a Filter 4x Faster by Removing an If

#55

I like how we have pretty much established how branchless coding is superior to branched coding. However I wonder if the compiler itself could recognize these patterns and turn branches into branchless instead, rather than making the code harder to read? as removing if conditions of course have a readability impact on the code.

Branchless coding is superior to branched coding whenever the branches are more or less random, which happens frequently when checking some properties of input numbers, like their sign or whether they fall inside certain intervals, or when sorting an array that comes in random order.

When a branch alternative will be taken much more frequently than the other, then branched coding with an "if" becomes superior.

So neither is better in general than the other, whenever the program must choose between alternatives, you must think about whether one is more likely than the other, or if both have similar probabilities.

For instance, when sorting an array, the optimal algorithm is not the same when you expect the input array to have a random order and when you expect it to be already almost sorted.

Re: Branchless Rust: Making a Filter 4x Faster by Removing an If

#56
post #18

This article is 100% AI written. The data was interesting, the commentary overly verbose and hard to gain useful insights from.

I'm apparently not good at spotting it. I was put off by the overly dramatic presentation. It gets tiring that the author apparently finds this more exciting than I do, and writes like it's enthralling. I just assumed it was an excess of enthusiasm or the first experience with this kind of thing. If it's AI, I'm way behind the game noticing it.

There are lots of indicators in this text, and this breathless presentation is very much how modern LLMs present things.

Claude also loves to describe things as being "real", particularly saying "X is real".

In this case,

> The reallocations were real, but they were never the bottleneck.

There was never any indication or setup in the text that they weren't real, but it's how it justifies wasted effort, it insists that some phenomenon it corrected but failed to solve the problem "was real".

Another giveaway are nonsensical analogies:

> The predictor is like a barista who starts making your usual order the moment you walk in. If you are a regular, this is fantastic: the coffee is ready when you reach the counter. If you order something random every day, the barista keeps pouring drinks into the sink.

If you order "something random every day", then you don't have a usual order for them to be making, it's an analogy that doesn't work.

And of course, the smoking gun is:

> The smoking gun

It probably won't be a good indicator forever as it has been noticed so much, but it's a particular favourite of the current generation of anthropic models.

Re: Branchless Rust: Making a Filter 4x Faster by Removing an If

#57
post #15

Earlier quoted context omitted.

Virtually every CPU has branch prediction, going back to at least the original Pentium (1993), maybe earlier. If you're running on a very old CPU, yes, the regular algo should be faster.

I think the Pentium is more or less the first microprocessor with branch prediction. Certainly the most mainstream. PowerPC 601 arrived at more or less the same time, and the Alpha 21064 was a year earlier. There were a few minicomputers and mainframes before that with branch predictors. Arguably the 486 could have done with a branch predictor (even a single entry loop predictor would have helped), and maybe the 386…

Pentium was the first CPU with a branch predictor that many people could afford to buy.

Before dynamic branch prediction, where the prediction for every branch is updated after each branch execution, depending on its history, static branch prediction had been used for decades, since around 1960, typically using the rule that forwards branches are unlikely to be taken, but backwards branches are likely to be taken. An alternative was to have an instruction bit where the compiler stored its prediction about the probability of a branch being taken.

Dynamic branch predictors began to be used since the mid seventies.

I do not remember now if any notable monolithic CPU had a dynamic branch predictor before Pentium, but prior multi-chip CPUs certainly existed.

Re: Branchless Rust: Making a Filter 4x Faster by Removing an If

#59

This article is 100% AI written. The data was interesting, the commentary overly verbose and hard to gain useful insights from.

Agree.

Interesting topic but why destroy your own credibility and reputation by shoveling llm-assisted slop to us here at hn?

The post should be flagged, and in general, i wish hn would adopt a no-tolerance policy to enhanced posting like this.

So what if the original text, if it existed in a human written form at all, had weird textual quirks and prose issues the author wished to hide. That texture's what makes humans interesting to engage with in the first place.

Post reply on HN