From e635a668fe04ea25a5542500c2aca5f4995fd5b5 Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Thu, 12 Dec 2024 18:07:45 +0000 Subject: [PATCH] feat: skip boundscheck --- a_sync/a_sync/_kwargs.pyx | 1 + a_sync/a_sync/base.pyx | 3 +- a_sync/asyncio/gather.pyx | 34 ++++++++++------------ a_sync/iter.pyx | 8 ++--- a_sync/primitives/_loggable.pyx | 1 + a_sync/primitives/locks/event.pyx | 1 + a_sync/primitives/locks/prio_semaphore.pyx | 1 + 7 files changed, 25 insertions(+), 24 deletions(-) diff --git a/a_sync/a_sync/_kwargs.pyx b/a_sync/a_sync/_kwargs.pyx index 229e077f..748ba98c 100644 --- a/a_sync/a_sync/_kwargs.pyx +++ b/a_sync/a_sync/_kwargs.pyx @@ -1,3 +1,4 @@ +# cython: boundscheck=False """ This module provides utility functions for handling keyword arguments related to synchronous and asynchronous flags. """ diff --git a/a_sync/a_sync/base.pyx b/a_sync/a_sync/base.pyx index 143dbf7a..5ae8e9e3 100644 --- a/a_sync/a_sync/base.pyx +++ b/a_sync/a_sync/base.pyx @@ -1,3 +1,4 @@ +# cython: boundscheck=False import functools import inspect from contextlib import suppress @@ -192,7 +193,7 @@ cdef str _parse_flag_name_from_list(object cls, object items): cdef str flag cdef list[str] present_flags = [flag for flag in VIABLE_FLAGS if flag in items] if not present_flags: - c_logger.debug("There are too many flags defined on %s", cls) + c_logger.debug("There are no flags defined on %s", cls) raise exceptions.NoFlagsFound(cls, items.keys()) if len(present_flags) > 1: c_logger.debug("There are too many flags defined on %s", cls) diff --git a/a_sync/asyncio/gather.pyx b/a_sync/asyncio/gather.pyx index 9e523fbd..7748bc7d 100644 --- a/a_sync/asyncio/gather.pyx +++ b/a_sync/asyncio/gather.pyx @@ -1,3 +1,4 @@ +# cython: boundscheck=False """ This module provides an enhanced version of :func:`asyncio.gather`. """ @@ -126,31 +127,25 @@ async def gather( See Also: :func:`asyncio.gather` """ - is_mapping = _is_mapping(awaitables) - results = await ( - gather_mapping( + if is_mapping := _is_mapping(awaitables): + results = await gather_mapping( awaitables[0], return_exceptions=return_exceptions, exclude_if=exclude_if, tqdm=tqdm, **tqdm_kwargs, ) - if is_mapping - else ( - tqdm_asyncio.gather( - *( - (_exc_wrap(a) for a in awaitables) - if return_exceptions - else awaitables - ), - **tqdm_kwargs, - ) - if tqdm - else asyncio.gather(*awaitables, return_exceptions=return_exceptions) - ) # type: ignore [arg-type] - ) + elif tqdm: + results = await tqdm_asyncio.gather( + *((_exc_wrap(a) for a in awaitables) if return_exceptions else awaitables), + **tqdm_kwargs, + ) + else: + results = await asyncio.gather(*awaitables, return_exceptions=return_exceptions) + if exclude_if and not is_mapping: - results = [r for r in results if not exclude_if(r)] + return [r for r in results if not exclude_if(r)] + return results @@ -199,7 +194,8 @@ async def gather_mapping( # return data in same order as input mapping return {k: results[k] for k in mapping} -cdef inline bint _is_mapping(object awaitables): + +cdef inline bint _is_mapping(tuple awaitables): return len(awaitables) == 1 and isinstance(awaitables[0], Mapping) diff --git a/a_sync/iter.pyx b/a_sync/iter.pyx index 94d9a09c..0061e1fc 100644 --- a/a_sync/iter.pyx +++ b/a_sync/iter.pyx @@ -1,3 +1,4 @@ +# cython: boundscheck=False import asyncio import functools import inspect @@ -115,16 +116,15 @@ class _AwaitableAsyncIterableMixin(AsyncIterable[T]): cdef object type_argument = T # Default value cdef str type_string = ":obj:`T` objects" - cdef object base, args + cdef object base + cdef tuple args cdef str module, qualname, name for base in getattr(cls, "__orig_bases__", []): if not hasattr(base, "__args__"): continue args = get_args(base) - if args and not isinstance(args[0], TypeVar): - type_argument = args[0] - + if args and not isinstance(type_argument := args[0], TypeVar): module = getattr(type_argument, "__module__", "") qualname = getattr(type_argument, "__qualname__", "") name = getattr(type_argument, "__name__", "") diff --git a/a_sync/primitives/_loggable.pyx b/a_sync/primitives/_loggable.pyx index 8076fc04..10578c84 100644 --- a/a_sync/primitives/_loggable.pyx +++ b/a_sync/primitives/_loggable.pyx @@ -1,3 +1,4 @@ +# cython: boundscheck=False """ This module provides a mixin class to add debug logging capabilities to other classes. """ diff --git a/a_sync/primitives/locks/event.pyx b/a_sync/primitives/locks/event.pyx index 430dacc8..142b6f9a 100644 --- a/a_sync/primitives/locks/event.pyx +++ b/a_sync/primitives/locks/event.pyx @@ -1,3 +1,4 @@ +# cython: boundscheck=False """ This module provides an enhanced version of asyncio.Event with additional debug logging to help detect deadlocks. """ diff --git a/a_sync/primitives/locks/prio_semaphore.pyx b/a_sync/primitives/locks/prio_semaphore.pyx index a03356d5..f05fc7f2 100644 --- a/a_sync/primitives/locks/prio_semaphore.pyx +++ b/a_sync/primitives/locks/prio_semaphore.pyx @@ -1,3 +1,4 @@ +# cython: boundscheck=False """ This module provides priority-based semaphore implementations. These semaphores allow waiters to be assigned priorities, ensuring that higher priority waiters are