r/simd • u/milksop • Dec 26 '24
Mask calculation for single line comments
Hi,
I'm trying to apply simdjson-style techniques to tokenizing something very similar, a subset of Python dicts, where the only problematic difference compared to json is that that there are comments that should be ignored (starting with '#' and continuing to '\n').
The comments themselves aren't too interesting so I'm open to any way of ignoring/skipping them. The trouble though, is that a lone double quote character in a comment invalidates double quote handling if the comment body is not treated specially.
At first glance it seems like #->\n could be treated similarly to double quotes, but because comments could also contain # (and also multiple \ns don't toggle the "in-comment" state) I haven't been able to figure out a way to generate a suitable mask to ignore comments.
Does anyone have any suggestions on this, or know of something similar that's been figured out already?
Thanks
4
u/camel-cdr- Dec 26 '24 edited Dec 27 '24
Here is an idea:
If you have a mask of '#' and '\n', you could do a segmented copy scan of the first element in the segment and match on if it's equal to '#'. (see also "Scan Primitives for Vector Computers")
Example (
\is\n):This exploits the idea, that each "segment" started by an '#' needs to be masked out, but a segmented started by an '\n' should be retained.
In RVV it can be implemented using a vcompress+viota+vrgather:
I'm not sure how to best implement a segmented copy scan with x86. I suppose you could emulate a prefix sum/+-scan.
4
u/YumiYumiYumi Dec 28 '24 edited Dec 28 '24
For AVX2+BMI2, this is what I came up with:
Note that PDEP/PEXT is very slow on AMD processors before Zen3.
How to handle continuation across vectors is left as an exercise for the reader.
4
u/Falvyu Dec 29 '24
For AVX512+VBMI2, you can use a segmented-scan with bitwise-or to compute a mask of commented areas.
The idea behind segmented scan: input sets a value, scans propagates it until mask is set. Rinse and repeat until the end of the SIMD register.
For instance, if you want to strip all comments from a string, the prolog + main loop body (tail not included) would be as follows:
The segmented or scan (
segscan_or_u64()) can be implemented using a loop (unrolled form here), but can be reduced down to:Note #1: compilers optimize the expression so that
(~mreset) | vis only computed once )Note #2: Some further optimizations should be possible if you assume that
(v[i] == 1) & (mreset[i] == 1)does not happen (which holds true, as character can't be both#and\n).For AVX512 without VBMI2 (pre-Icelake), you'll have to emulate the 8-bits vcompress (e.g. use 32-bits vcompress and widen/narrow data).
For SSE4/AVX2, masks can be extracted using comparison+movemask, and then you'll have to emulate vcompress.
Full code can be found here (
noquotebranch only handles comment, code onmasterhandles ' " ' but does no error checking )Disclaimer: I haven't benchmarked these implementations, but code has been tested.
3
u/aqrit Dec 27 '24
"In-comment" transitions: https://stackoverflow.com/a/70901525
How to combine that with "xor-scan" double quote processing is unknown (to me).
2
u/milksop Dec 27 '24
For future searchers reference, I settled on something like simdzone for now (a branchy loop to filter comments).
In addition to the helpful references in other comments, I thought maybe https://archive.is/JwC25 would be a possible branch-free solution, but I wasn't able to find a way to apply that to my problem (though quite possibly only due to my limited experience and knowledge.)
3
u/Wunkolo Dec 28 '24
Others are mentioning the same parallel-prefix-xor trick for bit-masking so I'll throw my write-up of this trick into the pile too:
https://wunkolo.github.io/post/2020/05/pclmulqdq-tricks/#prefix-xor
1
u/-Y0- Mar 18 '25
Yeah. I think you need to calculate them both, right? Whichever starts first wins. A string
" test # not comment"ignores comment and# "not a string"ignores string.1
u/Ok_Path_4731 Dec 23 '25
https://news.ycombinator.com/item?id=46366687
Am facing a similar challange, camel-cdr pointed me to this discussion :) Thanks camel-cdr.. Will try to process the info :)