Skip to content

Add UTF8 byte matching optimization #787

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

Merged
merged 3 commits into from
Nov 10, 2024
Merged

Conversation

milseman
Copy link
Member

@milseman milseman commented Nov 8, 2024

Adds a new instruction to match a sequence of UTF-8 bytes directly, rather than individual match-scalar calls.

@milseman milseman requested a review from natecook1000 November 8, 2024 22:55
@milseman
Copy link
Member Author

milseman commented Nov 8, 2024

Comparing against saved benchmark result origin_main
=== Regressions ======================================================================
- CompilerMessages_All                    87.3ms	85.4ms	1.95ms		2.3%
- CompilerMessages_All_Scalar             78ms	76.2ms	1.84ms		2.4%
- EmailRFCNoMatches_All_Scalar            44.4ms	44.2ms	287µs		0.6%
- ReluctantQuant_Whole                    3.57ms	3.37ms	195µs		5.8%
- SubtractionCCC_All_Scalar               5.9ms	5.78ms	117µs		2.0%
- AnchoredNotFound_All_Scalar             5.85ms	5.76ms	90.9µs		1.6%
- ReluctantQuant_Whole_Scalar             3.57ms	3.49ms	75.3µs		2.2%
- IntersectionCCC_All                     5.47ms	5.42ms	53.2µs		1.0%
- IntersectionCCC_All_Scalar              5.47ms	5.43ms	46.6µs		0.9%
- NotFound_All_Scalar                     2.51ms	2.49ms	22.2µs		0.9%
=== Improvements =====================================================================
- EmailLookahead_All                      18.7ms	21.8ms	-3.11ms		-14.2%
- EmailLookahead_All_Scalar               17.5ms	20.5ms	-3ms		-14.6%
- symDiffCCC_All_Scalar                   16.4ms	17.6ms	-1.15ms		-6.5%
- symDiffCCC_All                          16.6ms	17.5ms	-969µs		-5.5%
- EmojiRegex_All                          78.1ms	79.1ms	-968µs		-1.2%
- EmailLookaheadNoMatches_All             21.1ms	22.1ms	-951µs		-4.3%
- EmailRFC_All                            31.8ms	32.7ms	-900µs		-2.8%
- FSPathsRegex                            4.7ms	5.55ms	-843µs		-15.2%
- DiceRollsInText_All_Scalar              26.8ms	27.6ms	-835µs		-3.0%
- DiceRollsInText_All                     28.3ms	29.1ms	-804µs		-2.8%
- EmojiRegex_All_Scalar                   41.6ms	42.4ms	-789µs		-1.9%
- FSPathsRegex_Scalar                     4.57ms	5.29ms	-721µs		-13.6%
- LiteralSearch_All_Scalar                1.67ms	2.36ms	-693µs		-29.4%
- EmailRFCNoMatches_All                   47.1ms	47.8ms	-686µs		-1.4%
- EmailRFC_All_Scalar                     31.1ms	31.7ms	-682µs		-2.2%
- HangulSyllable_All_Scalar               1.89ms	2.57ms	-678µs		-26.4%
- EmailLookaheadList_Scalar               3.9ms	4.56ms	-656µs		-14.4%
- EmailLookaheadList                      4.03ms	4.68ms	-653µs		-14.0%
- LiteralSearchNotFound_All_Scalar        1.62ms	2.19ms	-574µs		-26.2%
- LiteralSearch_All                       2.64ms	3.18ms	-541µs		-17.0%
- HangulSyllable_All                      2.65ms	3.18ms	-533µs		-16.8%
- LiteralSearchNotFound_All               2.6ms	3ms	-398µs		-13.3%
- HangulSyllable_First_Scalar             774µs	1.03ms	-258µs		-25.0%
- InvertedCCC_All_Scalar                  5.72ms	5.95ms	-230µs		-3.9%
- Words_All                               4.41ms	4.63ms	-223µs		-4.8%
- DiceNotation                            4.1ms	4.32ms	-216µs		-5.0%
- DiceNotation_Scalar                     3.99ms	4.18ms	-186µs		-4.5%
- HangulSyllable_First                    1.24ms	1.41ms	-169µs		-12.0%
- EmailBuiltinCharacterClass_All          9.05ms	9.22ms	-162µs		-1.8%
- EmailLookaheadNoMatches_All_Scalar      19.5ms	19.6ms	-155µs		-0.8%
- InvertedCCC_All                         5.82ms	5.96ms	-144µs		-2.4%
- AnchoredNotFound_First_Scalar           5.84ms	5.98ms	-139µs		-2.3%
- Words_All_Scalar                        4.16ms	4.29ms	-127µs		-3.0%
- MACAddress                              2.32ms	2.44ms	-126µs		-5.2%
- MACAddress_Scalar                       2.26ms	2.37ms	-114µs		-4.8%
- IPv6Address_Scalar                      2.21ms	2.32ms	-107µs		-4.6%
- GraphemeBreakNoCap_All                  1.62ms	1.72ms	-103µs		-6.0%
- AnchoredNotFound_First                  8.09ms	8.19ms	-99.2µs		-1.2%
- GraphemeBreakNoCap_All_Scalar           1.54ms	1.64ms	-96.9µs		-5.9%
- EmailBuiltinCharacterClass_All_Scalar   8.93ms	9.03ms	-96.4µs		-1.1%
- IPv6Address                             2.31ms	2.39ms	-84.7µs		-3.5%
- ReluctantQuantWithTerminal_Whole_Scalar 5.93ms	6.01ms	-82.3µs		-1.4%
- BasicBuiltinCharacterClass_All          2.98ms	3.05ms	-73.6µs		-2.4%
- BasicBuiltinCharacterClass_All_Scalar   2.33ms	2.41ms	-73.5µs		-3.1%
- ReluctantQuantWithTerminal_Whole        5.93ms	5.99ms	-66.7µs		-1.1%
- URLWithWordBoundaries_All               1.76ms	1.82ms	-58µs		-3.2%
- URLWithWordBoundaries_All_Scalar        1.64ms	1.69ms	-52µs		-3.1%
- IPv4Address                             1.96ms	2ms	-43.6µs		-2.2%
- Numbers_All_Scalar                      2.02ms	2.06ms	-40µs		-1.9%
- IPv4Address_Scalar                      1.88ms	1.91ms	-33.6µs		-1.8%
- BasicCCC_All                            2.98ms	3.01ms	-26.5µs		-0.9%
- BasicRangeCCC_All_Scalar                3.09ms	3.11ms	-24.7µs		-0.8%
- Numbers_All                             2.51ms	2.53ms	-23.8µs		-0.9%
- CaseInsensitiveCCC_All                  3.31ms	3.33ms	-23µs		-0.7%
- Lines_All_Scalar                        775µs	796µs	-20.2µs		-2.5%
- Css_All_Scalar                          2.09ms	2.11ms	-16.5µs		-0.8%
- EagarQuantWithTerminal_Whole_Scalar     439µs	454µs	-14.7µs		-3.2%
- EagarQuantWithTerminal_Whole            443µs	457µs	-13.8µs		-3.0%
- URLWithWordBoundaries_All_SimpleWordBoundaries 563µs	573µs	-10.5µs		-1.8%

@milseman
Copy link
Member Author

milseman commented Nov 8, 2024

For FSPathsRegex, prior to this the hottest past was matchScalar, accounting for 32ms (15.9%) of execution. With this, the optimizer uses matchUTF for 6ms (3.4%), which is now pretty far down the list. The reason the benchmark only shows a 15% improvement is because other things like backtracking have bubbled up (but without regression) to dominate the slowness. Those will be the topic of future improvements.

self.utf8.formIndex(after: &cur)
}

guard cur <= end else { return nil }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be an assertion, but it's probably fine here.

Copy link
Member

@natecook1000 natecook1000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@milseman
Copy link
Member Author

milseman commented Nov 9, 2024

@swift-ci please test

@milseman milseman merged commit ce9d3b9 into swiftlang:main Nov 10, 2024
3 checks passed
@milseman milseman deleted the utf8_content branch November 10, 2024 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants