public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	Jiri Kosina <jikos@kernel.org>
Cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] HID: debug: improve hid_debug_event()
Date: Wed, 25 Nov 2015 12:31:56 -0800	[thread overview]
Message-ID: <1448483516.20113.61.camel@perches.com> (raw)
In-Reply-To: <1448368427-1669-1-git-send-email-linux@rasmusvillemoes.dk>

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;


  reply	other threads:[~2015-11-25 20:32 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-24 12:33 [PATCH] HID: debug: improve hid_debug_event() Rasmus Villemoes
2015-11-25 20:31 ` Joe Perches [this message]
2015-11-26 21:03   ` Rasmus Villemoes
2015-11-26 23:05 ` Jiri Kosina

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=1448483516.20113.61.camel@perches.com \
    --to=joe@perches.com \
    --cc=jikos@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    /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