Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 350333a

Browse files
authored
Merge pull request #2076 from matrix-org/erikj/as_perf
Make AS's faster
2 parents 639d9ae + 69efd77 commit 350333a

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

synapse/appservice/__init__.py

+20-18
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
from synapse.api.constants import EventTypes
16+
from synapse.util.caches.descriptors import cachedInlineCallbacks
1617

1718
from twisted.internet import defer
1819

@@ -124,29 +125,23 @@ def _check_namespaces(self, namespaces):
124125
raise ValueError(
125126
"Expected bool for 'exclusive' in ns '%s'" % ns
126127
)
127-
if not isinstance(regex_obj.get("regex"), basestring):
128+
regex = regex_obj.get("regex")
129+
if isinstance(regex, basestring):
130+
regex_obj["regex"] = re.compile(regex) # Pre-compile regex
131+
else:
128132
raise ValueError(
129133
"Expected string for 'regex' in ns '%s'" % ns
130134
)
131135
return namespaces
132136

133-
def _matches_regex(self, test_string, namespace_key, return_obj=False):
134-
if not isinstance(test_string, basestring):
135-
logger.error(
136-
"Expected a string to test regex against, but got %s",
137-
test_string
138-
)
139-
return False
140-
137+
def _matches_regex(self, test_string, namespace_key):
141138
for regex_obj in self.namespaces[namespace_key]:
142-
if re.match(regex_obj["regex"], test_string):
143-
if return_obj:
144-
return regex_obj
145-
return True
146-
return False
139+
if regex_obj["regex"].match(test_string):
140+
return regex_obj
141+
return None
147142

148143
def _is_exclusive(self, ns_key, test_string):
149-
regex_obj = self._matches_regex(test_string, ns_key, return_obj=True)
144+
regex_obj = self._matches_regex(test_string, ns_key)
150145
if regex_obj:
151146
return regex_obj["exclusive"]
152147
return False
@@ -166,7 +161,14 @@ def _matches_user(self, event, store):
166161
if not store:
167162
defer.returnValue(False)
168163

169-
member_list = yield store.get_users_in_room(event.room_id)
164+
does_match = yield self._matches_user_in_member_list(event.room_id, store)
165+
defer.returnValue(does_match)
166+
167+
@cachedInlineCallbacks(num_args=1, cache_context=True)
168+
def _matches_user_in_member_list(self, room_id, store, cache_context):
169+
member_list = yield store.get_users_in_room(
170+
room_id, on_invalidate=cache_context.invalidate
171+
)
170172

171173
# check joined member events
172174
for user_id in member_list:
@@ -219,10 +221,10 @@ def is_interested_in_user(self, user_id):
219221
)
220222

221223
def is_interested_in_alias(self, alias):
222-
return self._matches_regex(alias, ApplicationService.NS_ALIASES)
224+
return bool(self._matches_regex(alias, ApplicationService.NS_ALIASES))
223225

224226
def is_interested_in_room(self, room_id):
225-
return self._matches_regex(room_id, ApplicationService.NS_ROOMS)
227+
return bool(self._matches_regex(room_id, ApplicationService.NS_ROOMS))
226228

227229
def is_exclusive_user(self, user_id):
228230
return (

tests/appservice/test_appservice.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
from mock import Mock
2020
from tests import unittest
2121

22+
import re
23+
2224

2325
def _regex(regex, exclusive=True):
2426
return {
25-
"regex": regex,
27+
"regex": re.compile(regex),
2628
"exclusive": exclusive
2729
}
2830

0 commit comments

Comments
 (0)