Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Resource.resolve to avoid linear search of methods #9899

Merged
merged 11 commits into from
Nov 15, 2024
Merged
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 20 additions & 24 deletions aiohttp/web_urldispatcher.py
bdraco marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,9 @@ async def _default_expect_handler(request: Request) -> None:
class Resource(AbstractResource):
def __init__(self, *, name: Optional[str] = None) -> None:
super().__init__(name=name)
self._routes: List[ResourceRoute] = []
self._routes: Dict[str, ResourceRoute] = {}
self._any_route: Optional[ResourceRoute] = None
self._allowed_methods: Set[str] = set()

def add_route(
self,
Expand All @@ -337,13 +339,12 @@ def add_route(
*,
expect_handler: Optional[_ExpectHandler] = None,
) -> "ResourceRoute":
for route_obj in self._routes:
if route_obj.method == method or route_obj.method == hdrs.METH_ANY:
raise RuntimeError(
"Added route will never be executed, "
"method {route.method} is already "
"registered".format(route=route_obj)
)
if route := self._routes.get(method) or self._any_route:
bdraco marked this conversation as resolved.
Show resolved Hide resolved
raise RuntimeError(
"Added route will never be executed, "
f"method {route.method} is already "
"registered"
)

route_obj = ResourceRoute(method, handler, self, expect_handler=expect_handler)
self.register_route(route_obj)
Expand All @@ -353,23 +354,18 @@ def register_route(self, route: "ResourceRoute") -> None:
assert isinstance(
route, ResourceRoute
), f"Instance of Route class is required, got {route!r}"
self._routes.append(route)
if route.method == hdrs.METH_ANY:
self._any_route = route
else:
self._allowed_methods.add(route.method)
bdraco marked this conversation as resolved.
Show resolved Hide resolved
self._routes[route.method] = route

async def resolve(self, request: Request) -> _Resolve:
allowed_methods: Set[str] = set()

match_dict = self._match(request.rel_url.path_safe)
if match_dict is None:
return None, allowed_methods

for route_obj in self._routes:
route_method = route_obj.method
allowed_methods.add(route_method)

if route_method == request.method or route_method == hdrs.METH_ANY:
return (UrlMappingMatchInfo(match_dict, route_obj), allowed_methods)
else:
return None, allowed_methods
if (match_dict := self._match(request.rel_url.path_safe)) is None:
return None, set()
if route := self._routes.get(request.method) or self._any_route:
bdraco marked this conversation as resolved.
Show resolved Hide resolved
return UrlMappingMatchInfo(match_dict, route), self._allowed_methods
return None, self._allowed_methods

@abc.abstractmethod
def _match(self, path: str) -> Optional[Dict[str, str]]:
Expand All @@ -379,7 +375,7 @@ def __len__(self) -> int:
return len(self._routes)

def __iter__(self) -> Iterator["ResourceRoute"]:
return iter(self._routes)
return iter(self._routes.values())

# TODO: implement all abstract methods

Expand Down
Loading