Skip to content

Commit

Permalink
Merge pull request #1516 from mohd-akram/async-perf
Browse files Browse the repository at this point in the history
Reduce async overhead due to auto_await
  • Loading branch information
davidism committed Nov 9, 2021
2 parents 99b6fc7 + b756b19 commit c0130ea
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/jinja2/async_utils.py
Expand Up @@ -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)

Expand Down

0 comments on commit c0130ea

Please sign in to comment.