Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

HID: debug: improve hid_debug_event()

The code in hid_debug_event() causes horrible code generation. First,
we do a strlen() call for every byte we copy (we're doing a store to
global memory, so gcc has no way of proving that strlen(buf) doesn't
change). Second, since both i, list->tail and HID_DEBUG_BUFSIZE have
signed type, the modulo computation has to take into account the
possibility that list->tail+i is negative, so it's not just a simple
and.

Fix the former by simply not doing strlen() at all (we have to load
buf[i] anyway, so testing it is almost free) and the latter by
changing i to unsigned. This cuts 29% (69 bytes) of the size of the
function.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>

authored by

Rasmus Villemoes and committed by
Jiri Kosina
92529623 cc8a9d79

+2 -2
+2 -2
drivers/hid/hid-debug.c
··· 659 659 /* enqueue string to 'events' ring buffer */ 660 660 void hid_debug_event(struct hid_device *hdev, char *buf) 661 661 { 662 - int i; 662 + unsigned i; 663 663 struct hid_debug_list *list; 664 664 unsigned long flags; 665 665 666 666 spin_lock_irqsave(&hdev->debug_list_lock, flags); 667 667 list_for_each_entry(list, &hdev->debug_list, node) { 668 - for (i = 0; i < strlen(buf); i++) 668 + for (i = 0; buf[i]; i++) 669 669 list->hid_debug_buf[(list->tail + i) % HID_DEBUG_BUFSIZE] = 670 670 buf[i]; 671 671 list->tail = (list->tail + i) % HID_DEBUG_BUFSIZE;