lkml.org 
[lkml]   [2015]   [Sep]   [16]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
SubjectRe: tools: usbip: detach: avoid calling strlen() at each iteration
From
Date
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


\
 
 \ /
  Last update: 2015-09-16 10:01    [W:0.040 / U:0.572 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site