public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Clemens Ladisch <clemens@ladisch.de>
To: Aaro Koskinen <aaro.koskinen@iki.fi>,
	Eric Curtin <ericcurtin17@gmail.com>
Cc: valentina.manea.m@gmail.com, shuah.kh@samsung.com,
	linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: tools: usbip: detach: avoid calling strlen() at each iteration
Date: Wed, 16 Sep 2015 09:25:56 +0200	[thread overview]
Message-ID: <55F91984.4000905@ladisch.de> (raw)
In-Reply-To: <20150915205555.GA13203@blackmetal.musicnaut.iki.fi>

Aaro Koskinen wrote:
> On Tue, Sep 15, 2015 at 09:27:20PM +0100, Eric Curtin wrote:
>> -	for (unsigned int i = 0; i < strlen(port); i++)
>> +	unsigned int port_len = strlen(port);
>> +
>> +	for (unsigned int i = 0; i < port_len; i++)
>
> port is read only in this function, so maybe just use "const" and the
> compiler should know to do the same without adding a new variable?

If the compiler knows the implementation of strlen() (because it's
a built-in function), then it sees that nobody modifies port[] in the
loop.  If the compiler does not know the implementation of strlen()
(because -fno-builtins is used), then it is possible that some other
function has a valid non-const pointer and modifies the data through it.

(Anyway, the detach_port() function is not time critical, so I don't
think that optimizing it is worthwhile if it reduces readability.  But
seeing the strlen() call at that place grates on me; I'm not against
moving it out of the loop.)

The loop goes through the string one character at a time, so it might
be possible to drop strlen() altogether and just stop the loop when the
end of the string is reached.

But the actual purpose of the loop is to check whether there is a valid
number.  This could be done more easily by replacing the loop and the
following atoi() call with strtol().


Regards,
Clemens

  reply	other threads:[~2015-09-16  7:34 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-15 20:27 tools: usbip: detach: avoid calling strlen() at each iteration Eric Curtin
2015-09-15 20:55 ` Aaro Koskinen
2015-09-16  7:25   ` Clemens Ladisch [this message]
2015-09-16 13:44   ` David Laight

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=55F91984.4000905@ladisch.de \
    --to=clemens@ladisch.de \
    --cc=aaro.koskinen@iki.fi \
    --cc=ericcurtin17@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=shuah.kh@samsung.com \
    --cc=valentina.manea.m@gmail.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