Skip to content

Commit 1c1cf14

Browse files
committed
Speed up event match regex evaluation for big messages
`regex.containsMatchIn()` for `.*@room.*` can take significantly longer than checking for `@room` (some real-world events I was getting took around 15 seconds with this, significantly slowing down the sync parsing). Checking `containsMatchIn()` does not lead to different results when having leading and trailing stars however, it will match in the same cases as when these are omitted. For testing purposes, I sent myself some Lorem Ipsum with 5000 words (not containing any @room). Without this change, the regex evaluation takes about 16 seconds. With this change, the regex evaluation now takes significantly less then a second.
1 parent c40fc52 commit 1c1cf14

File tree

5 files changed

+6
-1
lines changed

5 files changed

+6
-1
lines changed
Binary file not shown.
Binary file not shown.

matrix-sdk-android-rx/build/reports/ktlint/ktlintMainSourceSetCheck/ktlintMainSourceSetCheck.txt

Whitespace-only changes.

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/EventMatchCondition.kt

+6-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ class EventMatchCondition(
5656
if (wordsOnly) {
5757
value.caseInsensitiveFind(pattern)
5858
} else {
59-
val modPattern = if (pattern.hasSpecialGlobChar()) pattern.simpleGlobToRegExp() else "*$pattern*".simpleGlobToRegExp()
59+
val modPattern = if (pattern.hasSpecialGlobChar())
60+
// Regex.containsMatchIn() is way faster without leading and trailing
61+
// stars, that don't make any difference for the evaluation result
62+
pattern.removePrefix("*").removeSuffix("*").simpleGlobToRegExp()
63+
else
64+
pattern.simpleGlobToRegExp()
6065
val regex = Regex(modPattern, RegexOption.DOT_MATCHES_ALL)
6166
regex.containsMatchIn(value)
6267
}

0 commit comments

Comments
 (0)