From d@v|@ @end|ng |rom po@|t@co  Wed Apr  5 17:53:55 2023
From: d@v|@ @end|ng |rom po@|t@co (Davis Vaughan)
Date: Wed, 5 Apr 2023 11:53:55 -0400
Subject: [Rd] An alternative algorithm for `which()`
Message-ID: <CABzLhzzy82=y2cM1kJpN=QygcM6eZwGxPWPBo14KRQ0s5Qw6vw@mail.gmail.com>

Hi all,

I've sent in a bugzilla patch for an alternative C algorithm for
`which()` which uses less memory and is often faster in many real life
scenarios. I've documented it in full on the bugzilla page, with many
examples:
https://bugs.r-project.org/show_bug.cgi?id=18495

The short version is that the performance comes from making the loops
branchless, which seems to be particularly helpful for `which()`. With
`which(x)`, I'd argue that branches are often hard for the compiler to
predict since in most real data there is typically no indication that
if the i-th element of `x` is `TRUE`, then the i+1-th element might
also be `TRUE`.

I've received a few comments on the bugzilla page, but I'd love it if
anyone else could chime in and provide their own thoughts!

Thanks,
Davis Vaughan



[Rd] An alternative algorithm for `which()`
Davis Vaughan