All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vincent Fazio <vfazio@gmail.com>
To: linux-gpio@vger.kernel.org
Cc: brgl@kernel.org, Vincent Fazio <vfazio@gmail.com>
Subject: [libgpiod][PATCH 03/14] bindings: python: _internal: collapse isinstance checks
Date: Mon, 19 Oct 2026 19:37:17 -0500	[thread overview]
Message-ID: <20261020003728.13669-4-vfazio@gmail.com> (raw)
In-Reply-To: <20261020003728.13669-1-vfazio@gmail.com>

The two isinstance checks can be combined to a single call that uses a
tuple of types to check against.

When 8c9d5b4 was prototyped, it originally used a single call but it
was falsely micro-optimized to use two separate calls due to the single
call being slower.

The performance overhead was likely due to the tuple of types being
created on each iteration of the loop. Using a module variable with the
proper types avoids this overhead.

  $ python3 -m timeit -s "x = 42; T = (int, str)" "isinstance(x, T)"
  20000000 loops, best of 5: 13.9 nsec per loop
  $ python3 -m timeit -s "x = 42" "isinstance(x, int) or isinstance(x, str)"
  20000000 loops, best of 5: 14.6 nsec per loop
  $ python3 -m timeit -s "x = 42" "isinstance(x, (int, str))"
  10000000 loops, best of 5: 30 nsec per loop

Fixes: 8c9d5b4 ("bindings: python: make config iteration consistent")
Signed-off-by: Vincent Fazio <vfazio@gmail.com>
---
 bindings/python/gpiod/_internal.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/bindings/python/gpiod/_internal.py b/bindings/python/gpiod/_internal.py
index a4c997d..f0c7450 100644
--- a/bindings/python/gpiod/_internal.py
+++ b/bindings/python/gpiod/_internal.py
@@ -14,6 +14,8 @@ if TYPE_CHECKING:
 
 __all__ = ["config_iter", "poll_fd"]
 
+_LINE_ID_TYPES = (int, str)
+
 
 def poll_fd(fd: int, timeout: timedelta | float | None = None) -> bool:
     sec: float | None
@@ -30,7 +32,7 @@ def config_iter(
     config: dict[Iterable[int | str] | int | str, LineSettings | None],
 ) -> Generator[tuple[int | str, LineSettings | None]]:
     for key, settings in config.items():
-        if isinstance(key, int) or isinstance(key, str):
+        if isinstance(key, _LINE_ID_TYPES):
             yield key, settings
         else:
             for subkey in key:
-- 
2.43.0


  parent reply	other threads:[~2026-10-20  0:37 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-20  0:37 [libgpiod][PATCH 00/14] bindings: python: cleanup and support Python 3.15 Vincent Fazio
2026-10-20  0:37 ` [libgpiod][PATCH 01/14] bindings: python: _internal: sort exports Vincent Fazio
2026-10-20  0:37 ` [libgpiod][PATCH 02/14] bindings: python: _internal: simplify poll_fd calculation Vincent Fazio
2026-10-20  0:37 ` Vincent Fazio [this message]
2026-10-20  0:37 ` [libgpiod][PATCH 04/14] bindings: python: setup: use a context manager to manage files Vincent Fazio
2026-10-20  0:37 ` [libgpiod][PATCH 05/14] bindings: python: line: sort exports Vincent Fazio
2026-10-20  0:37 ` [libgpiod][PATCH 06/14] bindings: python: line_request: simplify __bool__ expression Vincent Fazio
2026-10-20  0:37 ` [libgpiod][PATCH 07/14] bindings: python: chip: " Vincent Fazio
2026-10-20  0:37 ` [libgpiod][PATCH 08/14] bindings: python: chip: drop redundant exception in reraise Vincent Fazio
2026-10-20  0:37 ` [libgpiod][PATCH 09/14] bindings: python: chip: use literals for dict/list and declare types Vincent Fazio
2026-10-20 13:02   ` Bartosz Golaszewski
2026-10-20  0:37 ` [libgpiod][PATCH 10/14] bindings: python: bump ruff linter dependency Vincent Fazio
2026-10-20  0:37 ` [libgpiod][PATCH 11/14] bindings: python: include Python.h before other headers Vincent Fazio
2026-10-20 13:07   ` Bartosz Golaszewski
2026-10-20  0:37 ` [libgpiod][PATCH 12/14] bindings: python: advertise Python 3.15 support Vincent Fazio
2026-10-20  0:37 ` [libgpiod][PATCH 13/14] bindings: python: fix directory validation in artifact script Vincent Fazio
2026-10-20  0:37 ` [libgpiod][PATCH 14/14] bindings: python: bump dependencies " Vincent Fazio
2026-10-20 13:10 ` [libgpiod][PATCH 00/14] bindings: python: cleanup and support Python 3.15 Bartosz Golaszewski
2026-10-20 13:25   ` Vincent Fazio
2026-10-20 13:32     ` Bartosz Golaszewski
2026-10-20 14:37       ` Vincent Fazio
2026-10-20 13:10 ` (subset) " Bartosz Golaszewski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261020003728.13669-4-vfazio@gmail.com \
    --to=vfazio@gmail.com \
    --cc=brgl@kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.