From: Mohamed Akram Date: Mon, 18 Oct 2021 19:58:54 +0000 (+0400) Subject: Reduce async overhead due to auto_await X-Git-Tag: 3.0.3~8^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F1516%2Fhead;p=thirdparty%2Fjinja.git Reduce async overhead due to auto_await --- diff --git a/CHANGES.rst b/CHANGES.rst index e859ab3f..ebd23b6a 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 diff --git a/src/jinja2/async_utils.py b/src/jinja2/async_utils.py index cedd7ba8..35e6cb10 100644 --- a/src/jinja2/async_utils.py +++ b/src/jinja2/async_utils.py @@ -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)