From b756b1924a50248d8b0b127ec3c08704c3e47831 Mon Sep 17 00:00:00 2001 From: Mohamed Akram Date: Mon, 18 Oct 2021 23:58:54 +0400 Subject: [PATCH] Reduce async overhead due to auto_await --- CHANGES.rst | 2 ++ src/jinja2/async_utils.py | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index e859ab3fa..ebd23b6a9 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 cedd7ba85..35e6cb109 100644 --- a/src/jinja2/async_utils.py +++ b/src/jinja2/async_utils.py @@ -44,7 +44,14 @@ def wrapper(*args, **kwargs): # 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)