Back to articles
Quickly detecting JSON Escapes with SWAR
NewsTools

Quickly detecting JSON Escapes with SWAR

via Dev.to TutorialJairusSW

Most JSON string data is boring. It is just ASCII text that does not contain " or \ , does not dip below 0x20 , and does not force any special-case escaping at all. The annoying part is that a serializer still has to prove that. The naive loop is obvious: for ( let i = 0 ; i < src . length ; i ++ ) { const code = src . charCodeAt ( i ); if ( code == 34 || code == 92 || code < 32 ) { // escape } } That is correct, but it is also one branch per code unit in the hottest part of the serializer. For json-as , I wanted the fast path to ask a cheaper question: Does this whole chunk contain anything interesting at all? That is where SWAR fits nicely. For this post, the companion code is here: 00-detector-functions 01-c-and-wasm-benchmark 02-assemblyscript-inspection What needs detecting? When serializing a JSON string, these are the lanes that matter: " because it must become \" \ because it must become \\ control characters < 0x20 non-ASCII UTF-16 code units, because they cannot stay on the p

Continue reading on Dev.to Tutorial

Opens in a new tab

Read Full Article
2 views

Related Articles