]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Add a fast path to stack_context when there are no active contexts.
authorBen Darnell <ben@bendarnell.com>
Mon, 17 Feb 2014 04:06:18 +0000 (23:06 -0500)
committerBen Darnell <ben@bendarnell.com>
Mon, 17 Feb 2014 04:06:18 +0000 (23:06 -0500)
This case is increasingly common now that coroutines are decoupled
from stack contexts.

tornado/stack_context.py

index b1e82b0e04eea1f06ded2fe99b6b873e6026e17c..2e845ab272f1d4f4dcce217cfd3f16d665385038 100644 (file)
@@ -266,6 +266,18 @@ def wrap(fn):
     # TODO: Any other better way to store contexts and update them in wrapped function?
     cap_contexts = [_state.contexts]
 
+    if not cap_contexts[0][0] and not cap_contexts[0][1]:
+        # Fast path when there are no active contexts.
+        def null_wrapper(*args, **kwargs):
+            try:
+                current_state = _state.contexts
+                _state.contexts = cap_contexts[0]
+                return fn(*args, **kwargs)
+            finally:
+                _state.contexts = current_state
+        null_wrapper._wrapped = True
+        return null_wrapper
+
     def wrapped(*args, **kwargs):
         ret = None
         try: