-
Notifications
You must be signed in to change notification settings - Fork 109
perf: SIMD scan ASCII runs in input loop #288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dyxushuai
wants to merge
7
commits into
rockorager:main
Choose a base branch
from
dyxushuai:perf/loop-ascii-simd
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
caa7637
perf: SIMD scan ASCII runs in input loop
dyxushuai 72ae497
refactor: share ASCII run scanner
dyxushuai 6da8289
fix: refine ASCII run boundary handling
dyxushuai 109f291
bench: isolate parser instances
dyxushuai 4a06cd6
fix: include buffered bytes in read loop
dyxushuai c222cf5
test: cover ASCII boundary and incomplete UTF-8 cases
dyxushuai d5d295b
fix: treat variation selectors as combining
dyxushuai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| const std = @import("std"); | ||
| const uucode = @import("uucode"); | ||
|
|
||
| /// Returns the length of a contiguous run of printable ASCII bytes (0x20..0x7E). | ||
| pub fn printableRunLen(input: []const u8) usize { | ||
| const VecLenOpt = std.simd.suggestVectorLength(u8); | ||
| if (VecLenOpt) |VecLen| { | ||
| const Vec = @Vector(VecLen, u8); | ||
| const lo: Vec = @splat(0x20); | ||
| const hi: Vec = @splat(0x7E); | ||
| var i: usize = 0; | ||
| while (i + VecLen <= input.len) : (i += VecLen) { | ||
| const chunk = @as(*const [VecLen]u8, @ptrCast(input[i..].ptr)).*; | ||
| const vec: Vec = chunk; | ||
| const ok = (vec >= lo) & (vec <= hi); | ||
| if (!@reduce(.And, ok)) { | ||
| var j: usize = 0; | ||
| while (j < VecLen) : (j += 1) { | ||
| const b = input[i + j]; | ||
| if (b < 0x20 or b > 0x7E) return i + j; | ||
| } | ||
| } | ||
| } | ||
| while (i < input.len) : (i += 1) { | ||
| const b = input[i]; | ||
| if (b < 0x20 or b > 0x7E) return i; | ||
| } | ||
| return input.len; | ||
| } | ||
|
|
||
| var i: usize = 0; | ||
| while (i < input.len) : (i += 1) { | ||
| const b = input[i]; | ||
| if (b < 0x20 or b > 0x7E) return i; | ||
| } | ||
| return input.len; | ||
| } | ||
|
|
||
| /// Returns the safe fast-path length for ASCII runs. | ||
| /// | ||
| /// This behaves like printableRunLen, but if the next codepoint is a combining | ||
| /// mark (Mn/Mc/Me, including keycaps/variation selectors), it leaves the last | ||
| /// ASCII byte for the parser to avoid breaking grapheme clusters. If the | ||
| /// following UTF-8 sequence is incomplete, it also leaves the last ASCII byte. | ||
| pub fn fastPathLen(input: []const u8) usize { | ||
| const run = printableRunLen(input); | ||
| if (run == 0) return 0; | ||
| if (run < input.len) { | ||
| const next = input[run..]; | ||
| const first = next[0]; | ||
| if (first >= 0x80) { | ||
| const seq_len = std.unicode.utf8ByteSequenceLength(first) catch return run; | ||
| if (next.len < seq_len) return run - 1; | ||
| const cp = std.unicode.utf8Decode(next[0..seq_len]) catch return run; | ||
| const gc = uucode.get(.general_category, cp); | ||
| switch (gc) { | ||
| .mark_nonspacing, | ||
| .mark_spacing_combining, | ||
| .mark_enclosing, | ||
| => return run - 1, | ||
| else => {}, | ||
| } | ||
dyxushuai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| return run; | ||
| } | ||
dyxushuai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| test "printableRunLen: empty" { | ||
| try std.testing.expectEqual(@as(usize, 0), printableRunLen("")); | ||
| } | ||
|
|
||
| test "printableRunLen: ascii run" { | ||
| try std.testing.expectEqual(@as(usize, 4), printableRunLen("abcd")); | ||
| } | ||
|
|
||
| test "printableRunLen: stops at control" { | ||
| try std.testing.expectEqual(@as(usize, 1), printableRunLen("a\nb")); | ||
| } | ||
|
|
||
| test "printableRunLen: stops at utf8" { | ||
| try std.testing.expectEqual(@as(usize, 5), printableRunLen("hello世界")); | ||
| } | ||
dyxushuai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| test "fastPathLen: keeps ascii before utf8" { | ||
| try std.testing.expectEqual(@as(usize, 5), fastPathLen("hello世界")); | ||
| } | ||
|
|
||
| test "fastPathLen: holds for combining mark" { | ||
| try std.testing.expectEqual(@as(usize, 0), fastPathLen("a\u{0301}")); | ||
| } | ||
|
|
||
| test "fastPathLen: holds for keycap" { | ||
| try std.testing.expectEqual(@as(usize, 0), fastPathLen("1\u{20E3}")); | ||
| } | ||
|
|
||
| test "fastPathLen: holds for incomplete utf8" { | ||
| try std.testing.expectEqual(@as(usize, 0), fastPathLen("a\xE2")); | ||
| } | ||
dyxushuai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.