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

Small performance improvements #773

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Changes from all 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
15 changes: 10 additions & 5 deletions src/klein/_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,11 @@ def extractURLparts(request: IRequest) -> Tuple[str, str, int, str, str]:
server_port = request.getHost().port
else:
server_port = 0
if (bool(request.isSecure()), server_port) not in [
is_secure = bool(request.isSecure())
if (is_secure, server_port) not in [
(True, 443),
(False, 80),
(False, 0),
(True, 0),
]:
] or server_port == 0:
server_name = b"%s:%d" % (server_name, server_port)

script_name = b""
Expand All @@ -113,7 +112,7 @@ def extractURLparts(request: IRequest) -> Tuple[str, str, int, str, str]:
if not path_info.startswith(b"/"):
path_info = b"/" + path_info

url_scheme = "https" if request.isSecure() else "http"
url_scheme = "https" if is_secure else "http"

utf8Failures = []
try:
Expand Down Expand Up @@ -232,6 +231,12 @@ def process(r: object) -> Any:
returns an IRenderable, then render it and let the result of that
bubble back up.
"""
# isinstance() is faster than providedBy(), so this speeds up the
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is... maybe a good idea? Depends on how common this case is.

# very common case of returning pre-rendered results, at the cost
# of slightly slowing down other cases.
if isinstance(r, (bytes, str)):
return r

if isinstance(r, Response):
r = r._applyToRequest(request)

Expand Down
Loading