From: Ben Darnell Date: Sun, 15 May 2011 04:25:31 +0000 (-0700) Subject: Small speedups X-Git-Tag: v2.0.0~80 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=af29183d93dda9d7953b3efac9e7f955ea55b4cc;p=thirdparty%2Ftornado.git Small speedups --- diff --git a/tornado/ioloop.py b/tornado/ioloop.py index a7bdffc3c..f1a4794b7 100644 --- a/tornado/ioloop.py +++ b/tornado/ioloop.py @@ -341,8 +341,9 @@ class IOLoop(object): from that IOLoop's thread. add_callback() may be used to transfer control from other threads to the IOLoop's thread. """ + if not self._callbacks: + self._wake() self._callbacks.append(stack_context.wrap(callback)) - self._wake() def _wake(self): try: diff --git a/tornado/iostream.py b/tornado/iostream.py index 3a9efcfef..cb67f3ae4 100644 --- a/tornado/iostream.py +++ b/tornado/iostream.py @@ -524,7 +524,7 @@ def _merge_prefix(deque, size): >>> _merge_prefix(d, 100); print d deque(['abcdefghij']) """ - if len(deque) == 1 and len(deque[0]) < size: + if len(deque) == 1 and len(deque[0]) <= size: return prefix = [] remaining = size diff --git a/tornado/stack_context.py b/tornado/stack_context.py index c5570b499..61b426061 100644 --- a/tornado/stack_context.py +++ b/tornado/stack_context.py @@ -55,8 +55,6 @@ import logging import sys import threading -NoneType = type(None) - class _State(threading.local): def __init__(self): self.contexts = () @@ -149,6 +147,8 @@ def wrap(fn): different execution context (either in a different thread or asynchronously in the same thread). ''' + if fn is None or fn.__class__ is _StackContextWrapper: + return fn # functools.wraps doesn't appear to work on functools.partial objects #@functools.wraps(fn) def wrapped(callback, contexts, *args, **kwargs): @@ -180,8 +180,6 @@ def wrap(fn): callback(*args, **kwargs) else: callback(*args, **kwargs) - if isinstance(fn, (_StackContextWrapper, NoneType)): - return fn return _StackContextWrapper(wrapped, fn, _state.contexts) @contextlib.contextmanager diff --git a/tornado/web.py b/tornado/web.py index 5a021f334..07021315d 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -206,12 +206,7 @@ class RequestHandler(object): HTTP specification. If the value is not a string, we convert it to a string. All header values are then encoded as UTF-8. """ - if isinstance(value, datetime.datetime): - t = calendar.timegm(value.utctimetuple()) - value = email.utils.formatdate(t, localtime=False, usegmt=True) - elif isinstance(value, int) or isinstance(value, long): - value = str(value) - else: + if isinstance(value, basestring): value = utf8(value) # If \n is allowed into the header, it is possible to inject # additional headers or split the request. Also cap length to @@ -219,6 +214,13 @@ class RequestHandler(object): safe_value = re.sub(b(r"[\x00-\x1f]"), b(" "), value)[:4000] if safe_value != value: raise ValueError("Unsafe header value %r", value) + elif isinstance(value, datetime.datetime): + t = calendar.timegm(value.utctimetuple()) + value = email.utils.formatdate(t, localtime=False, usegmt=True) + elif isinstance(value, int) or isinstance(value, long): + value = str(value) + else: + raise TypeError("Unsupported header value %r" % value) self._headers[name] = value _ARG_DEFAULT = []