perf(api): Optimize HTTPStatus handling

Modify the _compose_status_response() function to no longer use getattr to get
the body for HTTPStatus. This will make the code more readable and more
performant.

As part of this work, rework _compose_error_response() to no longer call
_compose_status_response(), saving a few CPU cycles in that path as well. If
we ever end up needing to duplicate this code a third time, consider factoring
out into a separate helper function at that point.
This commit is contained in:
Kurt Griffiths
2015-08-24 22:41:28 -05:00
parent 6b3654ddfa
commit 2ebd618eb3

View File

@@ -502,19 +502,25 @@ class API(object):
def _compose_status_response(self, req, resp, http_status):
"""Composes a response for the given HTTPStatus instance."""
# PERF(kgriffs): The code to set the status and headers is identical
# to that used in _compose_error_response(), but refactoring in the
# name of DRY isn't worth the extra CPU cycles.
resp.status = http_status.status
if http_status.headers is not None:
resp.set_headers(http_status.headers)
if getattr(http_status, "body", None) is not None:
resp.body = http_status.body
# NOTE(kgriffs): If http_status.body is None, that's OK because
# it's acceptable to set resp.body to None (to indicate no body).
resp.body = http_status.body
def _compose_error_response(self, req, resp, error):
"""Composes a response for the given HTTPError instance."""
# Use the HTTPStatus handler function to set status/headers
self._compose_status_response(req, resp, error)
resp.status = error.status
if error.headers is not None:
resp.set_headers(error.headers)
if error.has_representation:
media_type, body = self._serialize_error(req, error)