Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1276402 > unrolled thread
| Started by | Rasmus Villemoes <linux@rasmusvillemoes.dk> |
|---|---|
| First post | 2015-11-24 13:40 +0100 |
| Last post | 2015-11-27 00:10 +0100 |
| Articles | 4 — 3 participants |
Back to article view | Back to linux.kernel
[PATCH] HID: debug: improve hid_debug_event() Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2015-11-24 13:40 +0100
Re: [PATCH] HID: debug: improve hid_debug_event() Joe Perches <joe@perches.com> - 2015-11-25 21:40 +0100
Re: [PATCH] HID: debug: improve hid_debug_event() Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2015-11-26 22:10 +0100
Re: [PATCH] HID: debug: improve hid_debug_event() Jiri Kosina <jikos@kernel.org> - 2015-11-27 00:10 +0100
| From | Rasmus Villemoes <linux@rasmusvillemoes.dk> |
|---|---|
| Date | 2015-11-24 13:40 +0100 |
| Subject | [PATCH] HID: debug: improve hid_debug_event() |
| Message-ID | <qyl35-86O-37@gated-at.bofh.it> |
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>
---
drivers/hid/hid-debug.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/hid/hid-debug.c b/drivers/hid/hid-debug.c
index 2886b645ced7..acfb522a432a 100644
--- a/drivers/hid/hid-debug.c
+++ b/drivers/hid/hid-debug.c
@@ -659,13 +659,13 @@ EXPORT_SYMBOL_GPL(hid_dump_device);
/* enqueue string to 'events' ring buffer */
void hid_debug_event(struct hid_device *hdev, char *buf)
{
- int i;
+ unsigned i;
struct hid_debug_list *list;
unsigned long flags;
spin_lock_irqsave(&hdev->debug_list_lock, flags);
list_for_each_entry(list, &hdev->debug_list, node) {
- for (i = 0; i < strlen(buf); i++)
+ for (i = 0; buf[i]; i++)
list->hid_debug_buf[(list->tail + i) % HID_DEBUG_BUFSIZE] =
buf[i];
list->tail = (list->tail + i) % HID_DEBUG_BUFSIZE;
--
2.6.1
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
[toc] | [next] | [standalone]
| From | Joe Perches <joe@perches.com> |
|---|---|
| Date | 2015-11-25 21:40 +0100 |
| Message-ID | <qyP19-2J1-31@gated-at.bofh.it> |
| In reply to | #1276402 |
On Tue, 2015-11-24 at 13:33 +0100, Rasmus Villemoes wrote:
> 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.
[]
> diff --git a/drivers/hid/hid-debug.c b/drivers/hid/hid-debug.c
[]
> @@ -659,13 +659,13 @@ EXPORT_SYMBOL_GPL(hid_dump_device);
> /* enqueue string to 'events' ring buffer */
> void hid_debug_event(struct hid_device *hdev, char *buf)
> {
> - int i;
> + unsigned i;
> struct hid_debug_list *list;
> unsigned long flags;
>
> spin_lock_irqsave(&hdev->debug_list_lock, flags);
> list_for_each_entry(list, &hdev->debug_list, node) {
> - for (i = 0; i < strlen(buf); i++)
> + for (i = 0; buf[i]; i++)
> list->hid_debug_buf[(list->tail + i) % HID_DEBUG_BUFSIZE] =
> buf[i];
> list->tail = (list->tail + i) % HID_DEBUG_BUFSIZE;
trivia:
The code might look nicer if (list->tail + i) % HID_DEBUG_BUFSIZE
was stored into a temporary.
Maybe use an if >= BUFSIZE to avoid a %
Something like:
int pos = list->tail;
for (i = 0; buf[i]; i++) {
list->hid_debug_buf[pos++] = buf[i];
if (pos >= HID_DEBUG_BUFSIZE)
pos = 0;
}
list->tail = pos;
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
[toc] | [prev] | [next] | [standalone]
| From | Rasmus Villemoes <linux@rasmusvillemoes.dk> |
|---|---|
| Date | 2015-11-26 22:10 +0100 |
| Message-ID | <qzbXI-22g-13@gated-at.bofh.it> |
| In reply to | #1277790 |
On Wed, Nov 25 2015, Joe Perches <joe@perches.com> wrote:
>> spin_lock_irqsave(&hdev->debug_list_lock, flags);
>> list_for_each_entry(list, &hdev->debug_list, node) {
>> - for (i = 0; i < strlen(buf); i++)
>> + for (i = 0; buf[i]; i++)
>> list->hid_debug_buf[(list->tail + i) % HID_DEBUG_BUFSIZE] =
>> buf[i];
>> list->tail = (list->tail + i) % HID_DEBUG_BUFSIZE;
>
> trivia:
>
> The code might look nicer if (list->tail + i) % HID_DEBUG_BUFSIZE
> was stored into a temporary.
Maybe.
> Maybe use an if >= BUFSIZE to avoid a %
Nah, that would likely be worse; both a cmov and a conditional jump are
probably more expensive than a simple '& 0x1ff' which the % should compile to
(provided the expression is unsigned).
Rasmus
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
[toc] | [prev] | [next] | [standalone]
| From | Jiri Kosina <jikos@kernel.org> |
|---|---|
| Date | 2015-11-27 00:10 +0100 |
| Message-ID | <qzdPP-3ct-1@gated-at.bofh.it> |
| In reply to | #1276402 |
On Tue, 24 Nov 2015, Rasmus Villemoes wrote: > 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> Agreed, this is much better. Applied to for-4.5/core, thanks Rasmus. -- Jiri Kosina SUSE Labs -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
[toc] | [prev] | [standalone]
Back to top | Article view | linux.kernel
csiph-web