Additional ARM Neon SIMD optimizations - #141
Open
samyron wants to merge 1 commit into
Open
Conversation
…kups (vqtbl4q_u8) instead of direct character compares and a logical OR reduction. Additionally, this commit fixes a shift-by-64 undefined behavior when a match lands on the last lane of a block. Depending on where the index lands, at best the string is escaped incorrectly, at worst it cases a segmentation fault: ``` => true irb(main):002> corrupt = "a" * 15 + "<" + "a" * 20 => "aaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaa" irb(main):003> actual = ERB::Util.html_escape(corrupt) => "aaaaaaaaaaaaaaa<aaaaaaaaaaaaaaa&ruby#39;aaaa" irb(main):004> crash = "a" * 15 + ">" + "a" * 15 # 31 bytes => "aaaaaaaaaaaaaaa>aaaaaaaaaaaaaaa" irb(main):005> ERB::Util.html_escape(crash) (irb):5: [BUG] Segmentation fault at 0x00000001230affe9 ``` This was run on a Macbook Air M1. ``` == 1k no matches == ruby 3.4.8 (2025-12-17 revision 995b59f666) +PRISM [arm64-darwin24] Warming up -------------------------------------- sm/neon-vtbl 1.191M i/100ms Calculating ------------------------------------- sm/neon-vtbl 11.896M (± 0.6%) i/s (84.06 ns/i) - 59.546M in 5.005745s Comparison: master: 9406864.5 i/s sm/neon-vtbl: 11895905.7 i/s - 1.26x faster == 1k few matches == ruby 3.4.8 (2025-12-17 revision 995b59f666) +PRISM [arm64-darwin24] Warming up -------------------------------------- sm/neon-vtbl 277.531k i/100ms Calculating ------------------------------------- sm/neon-vtbl 2.722M (± 1.7%) i/s (367.34 ns/i) - 13.877M in 5.098968s Comparison: master: 1485804.8 i/s sm/neon-vtbl: 2722269.4 i/s - 1.83x faster == 1k many matches == ruby 3.4.8 (2025-12-17 revision 995b59f666) +PRISM [arm64-darwin24] Warming up -------------------------------------- sm/neon-vtbl 178.565k i/100ms Calculating ------------------------------------- sm/neon-vtbl 1.787M (± 1.1%) i/s (559.45 ns/i) - 9.107M in 5.095378s Comparison: master: 1462148.5 i/s sm/neon-vtbl: 1787482.0 i/s - 1.22x faster ```
Author
|
I understand if you desire to keep the SIMD implementations the same between SSE2 and Neon. However, ARM provides table lookup instructions which are very convenient for this use case. Even better that every character that needs to be escaped is less than 64 so we can use a single This is the relevant assembly on The generated assembly on this branch: I also think the benchmark for |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Further optimize the NEON SIMD escape path using vectorized table lookups (vqtbl4q_u8) instead of direct character compares and a logical OR reduction.
Additionally, this commit fixes a shift-by-64 undefined behavior when a match lands on the last lane of a block. Depending on where the index lands, at best the string is escaped incorrectly, at worst it cases a segmentation fault:
This was run on a Macbook Air M1.