]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Reduce async overhead due to auto_await 1516/head
authorMohamed Akram <mohd.akram@outlook.com>
Mon, 18 Oct 2021 19:58:54 +0000 (23:58 +0400)
committerDavid Lord <davidism@gmail.com>
Tue, 9 Nov 2021 16:52:20 +0000 (08:52 -0800)
CHANGES.rst
src/jinja2/async_utils.py

index e859ab3fa7b84811cedffb8f29d4b786b024ead4..ebd23b6a93ad615ea35ac9f319da8f8403e0586a 100644 (file)
@@ -9,6 +9,8 @@ Unreleased
     :issue:`1535`
 -   Fix how the native environment treats leading and trailing spaces
     when parsing values on Python 3.10. :pr:`1537`
+-   Improve async performance by avoiding checks for common types.
+    :issue:`1514`
 
 
 Version 3.0.2
index cedd7ba85ec188d3cf5f0f5aed4328896764abc5..35e6cb109029b0c07a9a3cd052a45daa2db8dd64 100644 (file)
@@ -44,7 +44,14 @@ def async_variant(normal_func):  # type: ignore
     return decorator
 
 
+_common_primitives = {int, float, bool, str, list, dict, tuple, type(None)}
+
+
 async def auto_await(value: t.Union[t.Awaitable["V"], "V"]) -> "V":
+    # Avoid a costly call to isawaitable
+    if type(value) in _common_primitives:
+        return t.cast("V", value)
+
     if inspect.isawaitable(value):
         return await t.cast("t.Awaitable[V]", value)