Parsing integers quickly with AVX-512
lemire.me
lemire.me
https://kholdstare.github.io/technical/2020/05/26/faster-int...
Most of the overhead is found in dealing with variable length input, minus signs, and overflow checking.
https://gist.github.com/b7r6/0f80c18035d4db0dba298decc0b1ada...
We had a similar case with some AVX-optimized string operation in glibc.
If the length had been unknown (say, a C-style null-terminated string) then the code would have needed to be more complex. You would either have to find the length before-hand or done only aligned loads. BTW. ARM's SVE and RISC-V's Vector extension have special "first fault" load-instructions that make that case simpler: they instead modify the vector's mask/length to the lowest-numbered lane that would have faulted.
I don't know this is faster than simply doing the "mutiply by ten and add" sequence. I'd have to understand modern ALU behaviour, parallelism, shortcuts, chip cache dynamics you-name-it.
It would all have been so much simpler if we'd made decimal be hex instead. Octal doesn't work as well for me, no idea why. That said, it's much easier to ignore thumbs and count on 4 fingers of each hand than grow another 6
(I read the article. It descended into arcanum of instruction sets and assumptions which post date my DEC-10 ISA lessons, although I recall Digital had BCD instruction handling and a 6 bit byte model in a 36 bit word accordingly. DEC-10 instructions could take many, many clock cycles and have 5 components of from, to, via, because, maybe attached to them)
And while your comments on DEC-10 and 6-bit bytes might be interesting, there are very few people still working in CS who have seen a DEC-10 outside a museum. I find it always interesting to learn about computers that were obsolete before I wrote my first program, but their performance characteristics were vastly different than what we have today. And current college grads have always lived in a world with 64-bit SIMD machines that can do a scalar multiply in 3-4 cycles.
[Edit to add] I don't know why I'm salty about this, but Doug Lemire is real good and doesn't deserve to be blown off, especially not by someone referencing a computer that was obsolete before today's retirement-age CS people were in high school. VAX obsoleted the DEC-10 in 1977.
If you need to parse integers so quickly, why are you storing them as decimal strings?
If you're dealing with a massive CSV why not just compile to a more easily parsable format ahead of time?
The article mentions 0.8gb/s to 1.8gb/s throughput.
If youre downloading something off the internet, the bottleneck isn't going to be converting the strings to ints.
Further if you're downloading that much data, it suggests even more that you should be using a more space efficient representation.
We cannot really accomplish much by telling a bunch of other companies to change the format they use to send data to all of their customers.
First, that's a strong assumption. But...
> We check whether some value exceeds 9, in which case we had a non-digit character.
Huh? You already know where the end of the digits is. At least, I thought you did? So why would this be necessary?
If I'm seeing the misunderstanding here, the article with that statement is not talking about string length. They're talking about if non numerical input was given, ie, "a" would "exceed 9"
It would make more sense if it was talking about the string length. Then you might find a non-digit before the end. But it specifically says you know where the end of the digits is, not the end of the string.
Edit: Or, I'm trying to figure out what code might exist that found the end of the digits, but not always? The article says "assume you already know this", but computers aren't magic. There's code that figures out where the end of the digits is. So how does that code work, where it finds the end of the digits, but sometimes counts non-digits as digits?
as far as i can tell, "highload" (typically spelt in english) exists only in the russian-speaking communities and means "high performance".
Many applications these days suffer from "death by a thousand cuts" - there's no single thing which makes it slow, just lots and lots of slightly slow things piling on top of each other.