summaryrefslogtreecommitdiff
path: root/django/urls
diff options
context:
space:
mode:
authorJake Howard <git@theorangeone.net>2024-06-10 17:32:08 +0100
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2025-05-13 12:05:58 +0100
commitf920937c8a63df6bea220e4386f59cdb45b2e355 (patch)
tree8c36cb3d9f585a44f6412d8d55cff50c0d2a092b /django/urls
parentf66c79e93d41d2cd32be244170b54080c0796584 (diff)
Fixed #35518 -- Optimized RoutePattern by using string operations for converter-less routes.
Diffstat (limited to 'django/urls')
-rw-r--r--django/urls/resolvers.py30
1 files changed, 19 insertions, 11 deletions
diff --git a/django/urls/resolvers.py b/django/urls/resolvers.py
index 3d3bbfd38a..13043835dd 100644
--- a/django/urls/resolvers.py
+++ b/django/urls/resolvers.py
@@ -322,17 +322,25 @@ class RoutePattern(CheckURLMixin):
self.name = name
def match(self, path):
- match = self.regex.search(path)
- if match:
- # RoutePattern doesn't allow non-named groups so args are ignored.
- kwargs = match.groupdict()
- for key, value in kwargs.items():
- converter = self.converters[key]
- try:
- kwargs[key] = converter.to_python(value)
- except ValueError:
- return None
- return path[match.end() :], (), kwargs
+ # Only use regex overhead if there are converters.
+ if self.converters:
+ if match := self.regex.search(path):
+ # RoutePattern doesn't allow non-named groups so args are ignored.
+ kwargs = match.groupdict()
+ for key, value in kwargs.items():
+ converter = self.converters[key]
+ try:
+ kwargs[key] = converter.to_python(value)
+ except ValueError:
+ return None
+ return path[match.end() :], (), kwargs
+ # If this is an endpoint, the path should be exactly the same as the route.
+ elif self._is_endpoint:
+ if self._route == path:
+ return "", (), {}
+ # If this isn't an endpoint, the path should start with the route.
+ elif path.startswith(self._route):
+ return path.removeprefix(self._route), (), {}
return None
def check(self):