Skip to content

hidapi/libusb: maintain in-memory cache of vendor/product strings#571

Open
slouken wants to merge 2 commits into
libusb:masterfrom
slouken:0001-hidapi-libusb-maintain-in-memory-cache-of-vendor-pro.patch
Open

hidapi/libusb: maintain in-memory cache of vendor/product strings#571
slouken wants to merge 2 commits into
libusb:masterfrom
slouken:0001-hidapi-libusb-maintain-in-memory-cache-of-vendor-pro.patch

Conversation

@slouken
Copy link
Copy Markdown
Contributor

@slouken slouken commented May 26, 2023

The get_usb_string call is rather expensive on some USB devices, so we cache the vendor/product strings for future lookups (e.g. when hid_enumerate is invoked again later).

This way, we only need to ask libusb for strings for devices we haven't seen since before we started.

This is an important performance improvement when frequently iterating devices using libusb.

The get_usb_string call is rather expensive on some USB devices, so we
cache the vendor/product strings for future lookups (e.g. when
hid_enumerate is invoked again later).

This way, we only need to ask libusb for strings for devices we haven't
seen since before we started.

Signed-off-by: Steven Noonan <steven@valvesoftware.com>
Signed-off-by: Sam Lantinga <slouken@libsdl.org>
@mcuee mcuee added the libusb Related to libusb backend label May 27, 2023
@Youw
Copy link
Copy Markdown
Member

Youw commented Jun 1, 2023

What about serial_number? Shouldn't it be cached as well?

@Youw
Copy link
Copy Markdown
Member

Youw commented Jun 1, 2023

And the caching probably needs to be device-specific, not vid/pid-specific. I have two USB/HID devices on my table right now: both have the same vid/pid but different Manufacturer and Product Name (FW is localized for different countries).

@Youw
Copy link
Copy Markdown
Member

Youw commented Jun 1, 2023

I also think none of it would be required when libusb/libusb#1258 is completed.

@slouken
Copy link
Copy Markdown
Contributor Author

slouken commented Jun 1, 2023

And the caching probably needs to be device-specific, not vid/pid-specific. I have two USB/HID devices on my table right now: both have the same vid/pid but different Manufacturer and Product Name (FW is localized for different countries).

Good point.

@mcuee
Copy link
Copy Markdown
Member

mcuee commented Mar 6, 2024

Unfortunately libusb/libusb#1258 is closed without being merged, due to some API discussions.

@Youw
Copy link
Copy Markdown
Member

Youw commented Mar 6, 2024

But the idea to get is done is still alive.
It is just a matter of getting it done.
I'd rather spend time making that than improving the change in this PR.

@mcuee
Copy link
Copy Markdown
Member

mcuee commented Mar 6, 2024

But the idea to get is done is still alive. It is just a matter of getting it done. I'd rather spend time making that than improving the change in this PR.

Good point. I agree with you here.

In that case, do you want to close this PR as not planned?

@Youw Youw added the don't_merge Don't merge this PR as is label Mar 6, 2024
@Youw
Copy link
Copy Markdown
Member

Youw commented Mar 6, 2024

I don't have a strong oppinion.
It doesn't bother me having it open.

@mcuee
Copy link
Copy Markdown
Member

mcuee commented May 21, 2026

@slouken
Copy link
Copy Markdown
Contributor Author

slouken commented May 21, 2026

I was looking at this patch again, and you're right, it should be device specific. We can add the path to the hash key, but that isn't 100% guaranteed to be unique if you unplug and plug a different device with the same vid/pid to the same port. Is there another way to guarantee that it's device specific?

@Youw
Copy link
Copy Markdown
Member

Youw commented May 22, 2026

I believe it could be at most platform-specific guarantee. On Windows devices with different serial numbers would have different paths even when connected to the same port, but on linux/libusb I believe it will be the same (by default) unless we add explicit additiona logic to distinguish devices (e.g. by serial number).

But I don't believe any of that is needed. I suggest abandining this PR/approach, and instead of caching anything on HIDAPI side - instead rely on OS caching, as implemented by libusb: libusb_get_device_string.

That implementation should probably be optional (in case HIDAPI is attempted to compile/link/run against older version of libusb, where this is not available), but totally covers what this PR suggests (at least logically).

@slouken
Copy link
Copy Markdown
Contributor Author

slouken commented May 22, 2026

I believe it could be at most platform-specific guarantee. On Windows devices with different serial numbers would have different paths even when connected to the same port, but on linux/libusb I believe it will be the same (by default) unless we add explicit additiona logic to distinguish devices (e.g. by serial number).

But I don't believe any of that is needed. I suggest abandining this PR/approach, and instead of caching anything on HIDAPI side - instead rely on OS caching, as implemented by libusb: libusb_get_device_string.

That implementation should probably be optional (in case HIDAPI is attempted to compile/link/run against older version of libusb, where this is not available), but totally covers what this PR suggests (at least logically).

I don't believe the OS implements caching, which is the problem. Also note this comment on the PR: "While the read can be fast, it can also take seconds."

@Youw
Copy link
Copy Markdown
Member

Youw commented May 22, 2026

Also note this comment on the PR: "While the read can be fast, it can also take seconds."

On one Ubuntu Linux machine, reading the /sys/bus/usb/ files to get the strings takes a variable amount of time **on one device, an xHCI root hub**. While the read can be fast, it can also take seconds.

HIDAPI doesn't try to open/read HUBs, that is not an issue.

I don't believe the OS implements caching

That's the thing - it does. That's one of the reasons why this API is allowed and works w/o actually opening the device (which requires elevated permissions sometime) - it only reads the information available from USB descriptor which automatically opened and parsed by the OS once, when the device just been attached (and even before it appeared as an available USB device thru OS API).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

don't_merge Don't merge this PR as is libusb Related to libusb backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants