Thursday, January 4, 2018 [Tweets] [Favorites]

A Branchless UTF-8 Decoder

Chris Wellons (via Matías N. Goldberg):

The CPU must correctly predict the length of the code point or else it will suffer a hazard. An incorrect guess will stall the pipeline and slow down decoding.

[…]

This reads four bytes regardless of the actual length. Avoiding doing something is branching, so this can’t be helped. The unneeded bits are shifted out based on the length. That’s all it takes to decode UTF-8 without branching.

1 Comment

Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment