Stories
Slash Boxes
Comments

SoylentNews is people

posted by janrinok on Wednesday May 16 2018, @11:51PM   Printer-friendly
from the as-easy-as-that dept.

Most strings found on the Internet are encoded using a particular unicode format called UTF-8. However, not all strings of bytes are valid UTF-8. The rules as to what constitute a valid UTF-8 string are somewhat arcane. Yet it seems important to quickly validate these strings before you consume them.

In a previous post, I pointed out that it takes about 8 cycles per byte to validate them using a fast finite-state machine. After hacking code found online, I showed that using SIMD instructions, we could bring this down to about 3 cycles per input byte.

Is that the best one can do? Not even close.

Many strings are just ASCII, which is a subset of UTF-8. They are easily recognized because they use just 7 bits per byte, the remaining bit is set to zero. Yet if you check each and every byte with silly scalar code, it is going to take over a cycle per byte to verify that a string is ASCII. For much better speed, you can vectorize the problem in this manner:

Essentially, we are loading up a vector register, comparing each entry with zero and turning on a flag (using a logical OR) whenever a character outside the allowed range is found. We continue until the very end no matter what, and only then do we examine our flags.

We can use the same general idea to validate UTF-8 strings. My code is available.

If you are almost certain that most of your strings are ASCII, then it makes sense to first test whether the string is ASCII, and only then fall back on the more expensive UTF-8 test.

So we are ten times faster than a reasonable scalar implementation. I doubt this scalar implementation is as fast as it can be… but it is not naive… And my own code is not nearly optimal. It is not using AVX to say nothing of AVX-512. Furthermore, it was written in a few hours. I would not be surprised if one could double the speed using clever optimizations.

The exact results will depend on your machine and its configuration. But you can try the code.

The counter-rolling can actually be done logarithmically by shifting 1,2,4, etc., eg:

[4,0,0,0] + ([0,4,0,0]-[1,1,1,1]) = [4,3,0,0]

[4,3,0,0] + ([0,0,4,3]-[2,2,2,2]) = [4,3,2,1]

but in this case the distances didn’t seem big enough to beat the linear method.

The distances can even be larger than the register size I believe if the last value in the register is carried over to the first element of the next. It’s a good way to delineate inline variable-length encodings.


Original Submission

 
This discussion has been archived. No new comments can be posted.
Display Options Threshold/Breakthrough Mark All as Read Mark All as Unread
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
  • (Score: 1, Funny) by Anonymous Coward on Thursday May 17 2018, @01:26AM (3 children)

    by Anonymous Coward on Thursday May 17 2018, @01:26AM (#680582)

    Dafuq? U r using C code on your blog??????

    Learn to code, dude. JavaScript.

    Starting Score:    0  points
    Moderation   +1  
       Flamebait=2, Interesting=1, Funny=2, Total=5
    Extra 'Funny' Modifier   0  

    Total Score:   1  
  • (Score: 3, Interesting) by Snotnose on Thursday May 17 2018, @02:22AM (2 children)

    by Snotnose (1623) on Thursday May 17 2018, @02:22AM (#680594)

    Modded you up from flamebait, even though I know you're kidding.

    My point is that while one part of my brain knows all about out of sequence ordering, long pipelines, parallel pipelines, vector math, etc etc etc; another part of my mind, the one I use when I program, is still stuck on the Z-80 architecture. Something I didn't realize until I ran across that article a week or three ago.

    --
    Why is tamales pronounced tamales but females is pronounced females instead of females?
    • (Score: 3, Funny) by JoeMerchant on Thursday May 17 2018, @02:41AM

      by JoeMerchant (3937) on Thursday May 17 2018, @02:41AM (#680600)

      part of my mind, the one I use when I program, is still stuck on the Z-80 architecture.

      I'm sorry, I thought it was incurable when I first encountered it, apparently I was right.

      Viva la 6502!

      --
      Україна досі не є частиною Росії. https://en.interfax.com.ua/news/general/878601.html Слава Україні 🌻
    • (Score: 0) by Anonymous Coward on Thursday May 17 2018, @05:41PM

      by Anonymous Coward on Thursday May 17 2018, @05:41PM (#680787)

      My point is you're a douche.