From: Sean Young <sean@mess.org>
To: linux-media@vger.kernel.org, Sean Young <sean@mess.org>,
Mauro Carvalho Chehab <mchehab@kernel.org>
Cc: Rik van Riel <riel@surriel.com>, linux-kernel@vger.kernel.org
Subject: [PATCH v5 09/20] media: rc: Use binary search for adding or updating a scancode
Date: Tue, 15 Sep 2026 09:32:19 +0100 [thread overview]
Message-ID: <042abeb49891d9d6be5035559c656da18eb22fc9.1789460680.git.sean@mess.org> (raw)
In-Reply-To: <cover.1789460680.git.sean@mess.org>
We can have up to 1024 scancodes entries which are always sorted, so make
this a little faster.
We need to either get an scancode entry, or find the insertion point
in the sorted array if no existing entry was found. The bsearch() function
cannot do this as it runs NULL is no matching entry is found. Therefore,
we provide an open coded binary search.
Signed-off-by: Sean Young <sean@mess.org>
---
drivers/media/rc/rc-main.c | 20 +++++++++++++-------
1 file changed, 13 insertions(+), 7 deletions(-)
diff --git a/drivers/media/rc/rc-main.c b/drivers/media/rc/rc-main.c
index 37ba19164875..ed746255b0a2 100644
--- a/drivers/media/rc/rc-main.c
+++ b/drivers/media/rc/rc-main.c
@@ -391,7 +391,7 @@ static unsigned int ir_establish_scancode(struct rc_dev *dev,
struct rc_map *rc_map,
u64 scancode, bool resize)
{
- unsigned int i;
+ unsigned int i, lo, hi;
lockdep_assert_held(&rc_map->lock);
@@ -406,15 +406,21 @@ static unsigned int ir_establish_scancode(struct rc_dev *dev,
if (dev->scancode_mask)
scancode &= dev->scancode_mask;
- /* First check if we already have a mapping for this ir command */
- for (i = 0; i < rc_map->len; i++) {
+ /*
+ * Binary search for an existing mapping for this ir command.
+ */
+ lo = 0;
+ hi = rc_map->len;
+ while (lo < hi) {
+ i = lo + (hi - lo) / 2;
if (rc_map->scan[i].scancode == scancode)
return i;
-
- /* Keytable is sorted from lowest to highest scancode */
- if (rc_map->scan[i].scancode >= scancode)
- break;
+ if (rc_map->scan[i].scancode < scancode)
+ lo = i + 1;
+ else
+ hi = i;
}
+ i = lo;
/* No previous mapping found, we might need to grow the table */
if (rc_map->size == rc_map->len) {
--
2.55.0
next prev parent reply other threads:[~2026-09-15 8:33 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <cover.1789460680.git.sean@mess.org>
2026-09-15 8:32 ` [PATCH v5 01/20] media: rc: Ensure registered is cleared in error path Sean Young
2026-09-15 8:32 ` [PATCH v5 02/20] media: rc: Ensure that rc_unregister_device() does not free input device Sean Young
2026-09-15 8:32 ` [PATCH v5 03/20] media: rc: Fix ABBA deadlock by making locks more fine grained Sean Young
2026-09-15 8:32 ` [PATCH v5 04/20] media: rc: Replace open coded krealloc() for keymap Sean Young
2026-09-15 13:07 ` Hans Verkuil
2026-09-15 14:56 ` Sean Young
2026-09-15 15:43 ` Sean Young
2026-09-15 8:32 ` [PATCH v5 05/20] media: rc: Add missing locking " Sean Young
2026-09-15 8:32 ` [PATCH v5 06/20] media: rc: Fix race between bpf(BPG_PROG_ATTACH) and device unregister Sean Young
2026-09-15 8:32 ` [PATCH v5 07/20] media: rc: mce_kbd: Fix inconsistent locking of keylock Sean Young
2026-09-15 8:32 ` [PATCH v5 08/20] media: ene_ir: Ensure teardown is done in the correct order Sean Young
2026-09-15 8:32 ` Sean Young [this message]
2026-09-15 8:32 ` [PATCH v5 10/20] media: rc: imon: Bind both interfaces via usb_driver_claim_interface() Sean Young
2026-09-15 8:32 ` [PATCH v5 11/20] media: ir_toy: Remove unused struct field Sean Young
2026-09-15 8:32 ` [PATCH v5 12/20] media: nuvoton-cir: " Sean Young
2026-09-15 8:32 ` [PATCH v5 13/20] media: ite-cir: Removed " Sean Young
2026-09-15 8:32 ` [PATCH v5 14/20] media: fintek-cir: Remove unused fields Sean Young
2026-09-15 8:32 ` [PATCH v5 15/20] media: mceusb: Remove unused field Sean Young
2026-09-15 8:32 ` [PATCH v5 16/20] media: serial_ir: Fix race condition where timer can be re-armed Sean Young
2026-09-15 8:32 ` [PATCH v5 17/20] media: rc: After rc_unregister_device() timers " Sean Young
2026-09-15 8:32 ` [PATCH v5 18/20] media: rc: Validate carrier range in LIRC_SET_REC_CARRIER ioctl Sean Young
2026-09-15 8:32 ` [PATCH v5 19/20] media: ir_toy: Validate the maximum tx carrier Sean Young
2026-09-15 8:32 ` [PATCH v5 20/20] media: meson-ir-tx: Prevent divide by zero in meson_irtx_prepare_pulse() Sean Young
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=042abeb49891d9d6be5035559c656da18eb22fc9.1789460680.git.sean@mess.org \
--to=sean@mess.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=mchehab@kernel.org \
--cc=riel@surriel.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®