[parser.c] Various string unescape optimizations #920
Closed
+117
−23
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR contains 4 commits which optimize
json_string_unescape. Admittedly, I almost didn't create this PR as I don't love the Apple Aarch64-specific code... however I decided to leave it up to the maintainers of this library to decide if this code is worth accepting.I omitted the SWAR / ARM Neon unless the code is being compiled on Apple platforms as a quick benchmark on my x86 laptop with the SWAR
find_backslashand customjson_memcpycode showed it was no fastermaster. Looking at thememcpyandmemchron Ubuntu showed a AVX optimized libc. At the time the code was structured a bit differently and it was only copyingsizeof(uint64_t)bytes at a time on x86. I could try implementingfind_backslashandjson_memcpyusing SSE2 if desired. I should note that onmemchron macOS does seem to be optimized using ARM Neon instructions. Looking at the assembly, the implementation is similar though not quite the same. I think the performance improvement comes from reduced function call overhead / branching asfind_backslashesends up inlined.With respect to keeping track of the
additional_backslashesinstead ofhas_more, if the string to unescape ends with a long run of characters that do not have any more backslashes, we can save amemchrand directly copy to the end of the string.I'm cool if you decide not to accept any of these and simply close this PR. These are just some random things I've been testing out.
Benchmarks
These were run on an Macbook Air M1.
With only the
addtional_backslashescommit:The benchmark:
additional_backslashesandfind_backslashes:additional_backslashes,find_backslashes,json_memcpy:additional_backslashes,find_backslashes,json_memcpy,unescape_unicode: