diff --git a/src/bokeh/driving.py b/src/bokeh/driving.py index 1a6aab26773..1aa5cd89c41 100644 --- a/src/bokeh/driving.py +++ b/src/bokeh/driving.py @@ -39,6 +39,7 @@ def update(i): from __future__ import annotations import logging # isort:skip + log = logging.getLogger(__name__) #----------------------------------------------------------------------------- @@ -153,7 +154,8 @@ def f(i: float) -> float: return partial(force, sequence=_advance(f)) def repeat(sequence: Sequence[int]) -> partial[Callable[[], None]]: - ''' Return a driver function that can advance a repeated of values. + """ Return a driver function that can advance a repeated of values. + .. code-block:: none @@ -164,10 +166,12 @@ def repeat(sequence: Sequence[int]) -> partial[Callable[[], None]]: Args: sequence (seq) : a sequence of values for the driver to bounce - ''' + """ + # Precompute len(sequence) N = len(sequence) - def f(i: int) -> int: - return sequence[i%N] + # Use a function instead of a closure to avoid slow closure cell dereference + def f(i: int, sequence=sequence, N=N) -> int: + return sequence[i % N] return partial(force, sequence=_advance(f)) def sine(w: float, A: float = 1, phi: float = 0, offset: float = 0) -> partial[Callable[[], None]]: @@ -200,7 +204,7 @@ def f(i: float) -> float: T = TypeVar("T") def _advance(f: Callable[[int], T]) -> Iterator[T]: - ''' Yield a sequence generated by calling a given function with + """ Yield a sequence generated by calling a given function with successively incremented integer values. Args: @@ -210,7 +214,8 @@ def _advance(f: Callable[[int], T]) -> Iterator[T]: Yields: f(i) where i increases each call - ''' + """ + # Use a local variable for faster 'i' access i = 0 while True: yield f(i)