diff options
author | Mohamed Akram <mohd.akram@outlook.com> | 2021-10-18 23:58:54 +0400 |
---|---|---|
committer | David Lord <davidism@gmail.com> | 2021-11-09 08:52:20 -0800 |
commit | b756b1924a50248d8b0b127ec3c08704c3e47831 (patch) | |
tree | d5e3af5155340478b24db3080faa966e7df9b5f1 | |
parent | 99b6fc7f37449f009b5c114b8159ccd4d10c1167 (diff) | |
download | jinja2-b756b1924a50248d8b0b127ec3c08704c3e47831.tar.gz |
Reduce async overhead due to auto_await
-rw-r--r-- | CHANGES.rst | 2 | ||||
-rw-r--r-- | src/jinja2/async_utils.py | 7 |
2 files changed, 9 insertions, 0 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index e859ab3..ebd23b6 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 cedd7ba..35e6cb1 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) |