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.

Change-Id: If3b13304668ad961abb81d5442f37a3a3a1d48b7
This commit is contained in:
SpiritCroc 2022-01-20 11:59:24 +01:00
parent e3c4a9157e
commit bd507a5bd2
1 changed files with 6 additions and 1 deletions

View File

@ -56,7 +56,12 @@ class EventMatchCondition(
if (wordsOnly) {
value.caseInsensitiveFind(pattern)
} else {
val modPattern = if (pattern.hasSpecialGlobChar()) pattern.simpleGlobToRegExp() else "*$pattern*".simpleGlobToRegExp()
val modPattern = if (pattern.hasSpecialGlobChar())
// Regex.containsMatchIn() is way faster without leading and trailing
// stars, that don't make any difference for the evaluation result
pattern.removePrefix("*").removeSuffix("*").simpleGlobToRegExp()
else
pattern.simpleGlobToRegExp()
val regex = Regex(modPattern, RegexOption.DOT_MATCHES_ALL)
regex.containsMatchIn(value)
}