From a0a639d78d5dae327909305e1174de55db0ab0d5 Mon Sep 17 00:00:00 2001 From: Kurt Griffiths Date: Wed, 22 Apr 2015 10:05:56 -0500 Subject: [PATCH] perf(Request): Remove extra datetime namespace lookup Hoist datetime methods so that they can be called directly, thus avoiding an additional namespace lookup operation when calling them. --- falcon/request.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/falcon/request.py b/falcon/request.py index ce35499..27d6000 100644 --- a/falcon/request.py +++ b/falcon/request.py @@ -46,6 +46,11 @@ WSGI_CONTENT_HEADERS = ('CONTENT_TYPE', 'CONTENT_LENGTH') _maybe_wrap_wsgi_stream = True +# PERF(kgriffs): Avoid an extra namespace lookup when using these functions +strptime = datetime.strptime +now = datetime.now + + class Request(object): """Represents a client's HTTP request. @@ -928,7 +933,7 @@ class Request(object): return None try: - date = datetime.strptime(param_value, format_string).date() + date = strptime(param_value, format_string).date() except ValueError: msg = "The date value does not match the required format" raise HTTPInvalidParam(msg, name) @@ -958,8 +963,7 @@ class Request(object): log_line = ( DEFAULT_ERROR_LOG_FORMAT. - format(datetime.now(), self.method, self.path, - query_string_formatted) + format(now(), self.method, self.path, query_string_formatted) ) if six.PY3: