News for hackers. Stuff that matters.
Re:Counting the number of matching characters in two ASCII s... by ComputerGuru on Friday May 21, 2021 @11:55AM (#27235673)
I love Daniel’s optimization posts, but this one seems a little too esoteric: it’s for when autovectorization isn’t fast enough (edit: or isn’t available) but you can’t or don’t want to use SIMD; it gives you a solution solidly better than the former but worse than the latter, and more opaque than either.

In all cases, obviously the real magic is in the 64-bit word comparison routine (which doesn’t get more than a passing mention); converting the former problem into the latter is extremely straightforward and obvious - if you know that optimized solutions for the latter exist (I personally didn’t know of this bit twiddling hack (aside from doing it via SIMD, obviously) and am grateful to learn about the approach in Muła’s code).

Re:Counting the number of matching characters in two ASCII s... by tyingq on Friday May 21, 2021 @12:06PM (#27235847)
Are there many cases where plain ASCII is okay anymore, anyway? Wouldn't proper UTF-whatever comparisons mandate doing it the slow way, if you wanted counts versus just "same/not the same"?
Re:Counting the number of matching characters in two ASCII s... by nonameiguess on Friday May 21, 2021 @04:07PM (#27239244)
The actual killer application of character count matching is bioinformatics. You only need four distinct characters to encode DNA, which of course, means in reality you only need two bits, but I don't know if anyone has bothered to develop a two-bit character encoding system and instructions that can operate on them. They have come up with extremely optimized implementations that compare byte streams, though, utilizing ASCII GCTA.
Re:Counting the number of matching characters in two ASCII s... by jakobnissen on Friday May 21, 2021 @06:34PM (#27240817)
Bioinformatician here. Yes, people have come up with many encoding systems: 2-bit encoding systems, 3-bit encoding systems (for N's), various encoding schemes that allows long runs of N's to be stored with little memory costs, and even reduced amino acid alphabets for e.g. 4-bit amino acid sequences.

In my experience, the performance bottlenecks in bioinformatics is usually IO, parsing, de/compression and needlessly slow algorithms. You rarely get meaningful speed improvements by internally encoding the sequences one way or another (storing small fixed-size sequences in machine integers is an exception). There can be other advantages, though, like reduced memory consumption.

Partly for that reason, most DNA data is just stored as gzipped text files.

Re:Counting the number of matching characters in two ASCII s... by GianFabien on Friday May 21, 2021 @07:47PM (#27241409)
Could be an area to use FPGA based arrays for far greater throughput. Some recent RISC-V designs include FPGA blocks.
Re:Counting the number of matching characters in two ASCII s... by TchoBeer on Friday May 21, 2021 @05:47PM (#27240378)
except amino acids are encoded for with three bases, meaning that the actual character encoding system is 6 bits.
Re:Counting the number of matching characters in two ASCII s... by masklinn on Friday May 21, 2021 @02:23PM (#27237894)
Proper comparison would have to be even slower as you’d need to deal with normalisation & equivalence.

The « slow way » of the post would also count matching code units which is utterly useless, really.

Re:Counting the number of matching characters in two ASCII s... by bombcar on Friday May 21, 2021 @12:15PM (#27235981)
That would be more interesting, something that could handle a UTF-8 string in a similar situation (I guess you'd compare the bytes and then do a walk to see if it was part of a multi-byte character? Maybe the high-bit would help?).
Re:Counting the number of matching characters in two ASCII s... by rocqua on Friday May 21, 2021 @01:16PM (#27236929)
For UTF-8 I believe the question is ill-posed. The "position" of a "character" in unicode is very hard to determine and depends largely on what you take to be a character.
Re:Counting the number of matching characters in two ASCII s... by bmn__ on Friday May 21, 2021 @03:05PM (#27238495)
Then UTF-8 should not be operated on directly. When reading from a file etc., first decode the input into an internal string format that has straight-forward linear access to each gc boundary.
Re:Counting the number of matching characters in two ASCII s... by eesmith on Friday May 21, 2021 @03:52PM (#27239075)
Even then, https://en.wikipedia.org/wiki/Unicode_equivalence . What does "matching character" mean given combining characters?
Re:Counting the number of matching characters in two ASCII s... by rocqua on Friday May 21, 2021 @04:38PM (#27239655)
Probably pick a canonical encoding and compare there.
Re:Counting the number of matching characters in two ASCII s... by ygra on Saturday May 22, 2021 @01:49AM (#27243891)
Yeah, that's what the normalization forms are for. But still, almost no question how to do things in Unicode can be answered in a single sentence without disregarding 99 % of the complexity.
Re:Counting the number of matching characters in two ASCII s... by Thaxll on Friday May 21, 2021 @02:54PM (#27238323)
HTTP headers.
Re:Counting the number of matching characters in two ASCII s... by joawowiij on Friday May 21, 2021 @12:59PM (#27236676)
hex, base64 encoded strings, etc
Re:Counting the number of matching characters in two ASCII s... by tyingq on Friday May 21, 2021 @01:07PM (#27236781)
That makes sense for "same/not-same" comparisons to me. I don't know why you would want counts of different characters though.
Re:Counting the number of matching characters in two ASCII s... by bombcar on Friday May 21, 2021 @02:01PM (#27237574)
"Password must be different by at least 6 characters" perhaps, though that's the inverse, really.

Fuzzy matching is another aspect that comes to mind.

Re:Counting the number of matching characters in two ASCII s... by sfink on Friday May 21, 2021 @05:28PM (#27240185)
Yes! Plain ASCII is great, even when you need to properly handle non-ASCII. When >90% of your strings are ASCII in practice, then having an "is ASCII" bit in your string headers can save a ton of memory, and gain more performance than you lose in flag checks and extra code.

Heck, if your usual encoding is UTF-8, the flag bit can be purely advisory and ignored when it doesn't matter.

Re:Counting the number of matching characters in two ASCII s... by bombcar on Friday May 21, 2021 @12:01PM (#27235784)
If the optimizing compiler can figure out to use SIMD shouldn’t it also be able to degrade to this when SIMD isn’t available?

And if we’re already down the “write it understandable and trust the compiler” path isn’t that the better way to go?

It’s interesting to me that C is now effectively a “higher level” language than it may appear on the surface because of optimization by compilers.

Re:Counting the number of matching characters in two ASCII s... by f00zz on Friday May 21, 2021 @12:55PM (#27236611)
> I personally didn’t know of this bit twiddling hack and am grateful to learn about the approach in Muła’s code

Yeah the last line had me scratching my head for a bit. Cool stuff.