diff options
author | Ben Bangert <ben@groovie.org> | 2020-10-13 09:21:03 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-13 09:21:03 -0700 |
commit | 2cf13312e63beed9e47ecbc84b1e02fb2f151c68 (patch) | |
tree | 381d7d13867926bb271416de1a2408db515d5aea | |
parent | bb646454916f64b0032a2e8c1adf17641a95bf95 (diff) | |
parent | 51cee63401ab129631719f637e2ee900f73a0682 (diff) | |
download | routes-2cf13312e63beed9e47ecbc84b1e02fb2f151c68.tar.gz |
Merge pull request #89 from gsakkis/master
Static prefix optimisation
-rw-r--r-- | routes/mapper.py | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/routes/mapper.py b/routes/mapper.py index e88feef..c113ccc 100644 --- a/routes/mapper.py +++ b/routes/mapper.py @@ -1,4 +1,6 @@ """Mapper and Sub-Mapper""" +import collections +import itertools as it import re import threading @@ -628,12 +630,20 @@ class Mapper(SubMapperParent): route.makeregexp(clist) regexps = [] - routematches = [] + prefix2routes = collections.defaultdict(list) for route in self.matchlist: if not route.static: - routematches.append(route) regexps.append(route.makeregexp(clist, include_names=False)) - self._routematches = routematches + # Group the routes by static prefix + prefix = ''.join(it.takewhile(lambda p: isinstance(p, str), + route.routelist)) + if route.minimization and not prefix.startswith('/'): + prefix = '/' + prefix + prefix2routes[prefix.rstrip("/")].append(route) + self._prefix2routes = prefix2routes + # Keep track of all possible prefix lengths in decreasing order + self._prefix_lens = sorted(set(len(p) for p in prefix2routes), + reverse=True) # Create our regexp to strip the prefix if self.prefix: @@ -694,7 +704,9 @@ class Mapper(SubMapperParent): if not valid_url: return (None, None, matchlog) - for route in self.matchlist: + matchlist = it.chain.from_iterable(self._prefix2routes.get(url[:prefix_len], ()) + for prefix_len in self._prefix_lens) + for route in matchlist: if route.static: if debug: matchlog.append(dict(route=route, static=True)) |