diff options
author | George Sakkis <george.sakkis@gmail.com> | 2020-06-21 12:39:38 +0300 |
---|---|---|
committer | George Sakkis <george.sakkis@gmail.com> | 2020-08-01 12:01:34 +0300 |
commit | 51cee63401ab129631719f637e2ee900f73a0682 (patch) | |
tree | 381d7d13867926bb271416de1a2408db515d5aea /routes | |
parent | bb646454916f64b0032a2e8c1adf17641a95bf95 (diff) | |
download | routes-51cee63401ab129631719f637e2ee900f73a0682.tar.gz |
Static prefix optimisation
Diffstat (limited to 'routes')
-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)) |