14 ms·
Transcoding Latin 1 strings to UTF-8 strings at 12 GB/s using AVX-512
- londons_explore 3y agoSince values 0-127 are used far more frequently than 128-255 in latin-1, it might make more sense to simply have a fast path which simply loads 512 bits at a time (ie. 64 bytes), detects if any are 0x80 or above, and if not just outputs them verbatim.
- jojobas 3y agoEither way throughput will depend on the fraction of >192 characters, what input data gave 12GB/s seems to be a mystery.
- reaperhulk 3y agoThe article states it's the French version of the Mars wikipedia entry and the repository has a link to the file he used in the readme: https://raw.githubusercontent.com/lemire/unicode_lipsum/main/wikipedia_mars/french.latin1.txt https://raw.githubusercontent.com/lemire/unicode_lipsum/main...
- deleted 3y ago[deleted]
- twoodfin 3y agoI don’t know if the article has been updated since your comment, but this approach is discussed & benchmarked. For the benchmarked data set it’s a winner.
- wffurr 3y agoThe article was indeed updated since I read it and the parent comment this morning.
- NelsonMinar 3y agoThe article has a whole section about that, you might enjoy reading about it. He reports a ~20% speedup on his test data.
- jojobas 3y agoInteresting to see how a non-AVX, non-branching version would do, need a prefilled array of extra pointer advance (0/1) and seemingly two more for the bitbanging.
- deleted 3y ago[deleted]
- xiphias2 3y agoAnother option would be a vector of 256 16 bit entries and keeping the pointer advance vector as you suggested.
- londons_explore 3y agoEvery time someone writes some really carefully micro-optimized piece of code like this, I worry that the implementation won't be shared with the whole world. This code only makes people's lives better if many languages and frameworks that translates latin-1 to utf8 are updated to have this new faster implementation. If this took 3 days to write and benchmark, then to save 3 days of human time, we probably need to get this into the hands of hundreds of millions of people, saving each person a few hundred microseconds.
- SomeoneFromCA 3y agoIt is mostly an educational code. Once you learn AVX-512 you can get boosts in many areas.
- re-thc 3y ago> I worry that the implementation won't be shared with the whole world. Considering the author also created https://github.com/simdutf/simdutf https://github.com/simdutf/simdutf it's likely used or will be used in NodeJs amongst other things. Is that good enough?
- magicalhippo 3y ago> This code only makes people's lives better if many languages and frameworks that translates latin-1 to utf8 are updated to have this new faster implementation. Except CPUs evolve and what was once a fast way of doing things may no longer be very fast. And with ASM you got no compiler to generate better targeted instructions. I've seen many instances where significant performance was gained by swapping out and old hand-written ASM routine with a plain language version. If you ever add some optimized ASM to your code, do a performance check at startup or similar, and have the plain language version as a fallback.
- TinkersW 3y agoIt is written with intrinsics not ASM. Compilers understand intrinsics and can optimize around them, and CPUs evolve improved SIMD instruction sets at a snails pace. Intel doesn't even really support AVX512 yet for consumer hardware, and maybe never will, so this code is mostly only good for very modern AMD.
- SomeoneFromCA 3y agoAnother proof that Linus is not always right. There were many folks who just blindly regurgitated AVX 512 is evil, without even actually knowing a thing about it.
- wtallis 3y ago> Another proof that Linus is not always right. No, this is just a case of the right answer changing over time, as good AVX-512 implementations became available, long after its introduction. And nothing in this article even comes close to addressing the main concern with the early AVX-512 implementation: the significant performance penalty it imposes on other code due to Skylake's slow power state transitions. Microbenchmarks like this made AVX-512 look good even on Skylake, because they ignore the broader effects.
- nwallin 3y agoTo add to your point, this benchmark would not have run on Skylake at all. It uses the _mm512_maskz_compress_epi8 instruction, which wasn't introduced until Ice Lake.
- SomeoneFromCA 3y agoSo your point he is indeed always right? Or was right in that particular case (he was not)? If you remember, Linux complained not about the particular implementation of AVX-512, but the concept itself. It is also kinda looks ignorant of him (and anyone else who thinks the same way) to believe that AVX-512 is only about 512 or it has no potential, being a just simply better SIMD ISA compared to AVX1/2. What he did he just expressed himself in his trademark silly edgy maximalist way. It is an absolute pleasure to work with, gives great performance boost, and he should have more careful with his statements.
- a1369209993 3y ago> So your point he is indeed always right? No, their point is that this does not refute said hypothetical claim. That is, their point is that it is not, as you claimed: > Another proof that Linus is not always right. (I don't know if their point is correct, but it's of the form "your argument against X is invalid", not "X is correct".)
- justin101 3y agoWhere does one even go about finding 12Gb of pure latin text?
- martijnvds 3y agoThe Vatican?
- ant6n 3y agoThe latin in latin-1 refers to the alphabet, not the language. In fact latin-1 can encode many Western European languages.
- CoastalCoder 3y agoI believe it was a joke. But the humour may have been lost in translation. It's funnier in the original ASCII.
- mmastrac 3y agoThe high bit is generally used to indicate humour.
- lovasoa 3y agoNot sure whether that was sarcastic, but ISO-8859-1 (Latin 1) encodes most european languages, not just latin. https://en.wikipedia.org/wiki/ISO/IEC_8859-1 https://en.wikipedia.org/wiki/ISO/IEC_8859-1
- redox99 3y ago12GB/s seems a bit slow. I'd expect the only bottleneck to be memory bandwidth. A dual channel DDR4 system memory bandwidth is ~40GB/s, and DDR5 ~80GB/s. Since this operation requires both a read and a write, you'd expect half that.
- peppermint_gum 3y ago> A dual channel DDR4 system memory bandwidth is ~40GB/s, and DDR5 ~80GB/s. It's impossible to saturate the memory bandwidth on a modern CPU with a single thread, even if all you do is reads with absolutely no processing. The bottleneck is how fast outstanding cache misses can be satisfied. The article even links to a benchmark that attempts to measure what it calls "sustainable memory bandwidth": https://www.cs.virginia.edu/stream/ref.html https://www.cs.virginia.edu/stream/ref.html
- ko27 3y ago> Latin 1 standard is still in widespread inside some systems (such as browsers) That doesn't seem to be correct. UTF-8 is used by 98% of all the websites. I am not sure if it's even worth the trouble for libraries to implement this algorithm, since Latin-1 encoding is being phased out. https://w3techs.com/technologies/details/en-utf8 https://w3techs.com/technologies/details/en-utf8
- fulafel 3y agoIt's the default HTTP character set. It's not clear whether the above stat page is about what charsets are explicitly specified. Also headers, mostly relevant for header values, are I think ISO-8859-1.
- ko27 3y agoSince HTML5 UTF-8 is the default charset. And for headers, they are parsed as ASCII encoded in almost all cases although ISO-8859-1 is supported.
- fulafel 3y agoI tried to find confirmation of this but found only: https://html.spec.whatwg.org/multipage/semantics.html#charset https://html.spec.whatwg.org/multipage/semantics.html#charse... > The Encoding standard requires use of the UTF-8 character encoding and requires use of the "utf-8" encoding label to identify it. Those Sounds to me like it tells you that you have to explicitly declare the charset as UTF-8, so you don't get the HTTP default of Latin-1. (But that's just one "living standard" not exactly synonymous with with HTML5 and it might change, or might have been different last week..)
- ko27 3y ago> so you don't get the HTTP default of Latin-1. That's not what your linked spec says. You can try it yourself, in any browser. If you omit the encoding the browser uses heuristics to guess, but it will always work if you write UTF-8 even without meta charset or encoding header.
- masfuerte 3y agoIs this useful? Most Latin 1 text is really Windows 1252, which has additional characters that don't have the same regular mapping to unicode. So this conversion will mangle curly quotes and the Euro sign, among others.
- dotancohen 3y ago> Most Latin 1 text is really Windows 1252 I'd say that the vast majority of Latin-1 that I've encountered is just ASCII. Where have you seen Windows-1252 presented with a Latin-1 header or other encoding declaration that declared it as Latin-1?
- masfuerte 3y agoWindows 1252 served as Latin 1 used to be common enough that browsers interpret a Latin 1 declaration as Windows 1252. Nowadays it seems moderately common for such text to be served with a utf-8 declaration, so it gets mangled in other ways. Or it gets imported into a CMS with no conversion or the wrong conversion, which has a similar result. You're right, ASCII is more common, but single-byte encoded prose that goes beyond ASCII is usually Windows 1252 in my experience.
- dotancohen 3y ago> Windows 1252 served as Latin 1 used to be common enough that browsers > interpret a Latin 1 declaration as Windows 1252. Thank you, I had not encountered this and I was dealing a lot with improperly-encoded text when I was running gibberish.co.il (over a decade ago). What systems were serving this? IIS would be my first guess, an Intuit product would be my second.