From af343991351eaaba162b36a1e82a7f53bbe53cab Mon Sep 17 00:00:00 2001 From: SpiritCroc Date: Thu, 20 Jan 2022 11:59:24 +0100 Subject: [PATCH] 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. --- changelog.d/5008.bugfix | 1 + .../android/sdk/api/pushrules/EventMatchCondition.kt | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 changelog.d/5008.bugfix diff --git a/changelog.d/5008.bugfix b/changelog.d/5008.bugfix new file mode 100644 index 00000000000..3c2f0091604 --- /dev/null +++ b/changelog.d/5008.bugfix @@ -0,0 +1 @@ +Big messages taking inappropriately long to evaluate .m.rule.roomnotif push rules diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/EventMatchCondition.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/EventMatchCondition.kt index eec5b0a402c..d82365cace8 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/EventMatchCondition.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/EventMatchCondition.kt @@ -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) }