Skip to contentSkip to conversation

tools: usbip: detach: avoid calling strlen() at each iteration

PATCH Eric Curtin 4 participants 4 messages Sep 15, 2015 – Sep 16, 2015 USB over IP driver

lore
Share & tools
lore
#0 lore raw

Instead of calling strlen on every iteration of the for loop, just call it once and cache the result in a temporary local variable which will be used in the for loop instead.

tools/usb/usbip/src/usbip_detach.c +3 -1
index 05c6d15..9db9d21 100644
@@ -47,7 +47,9 @@ static int detach_port(char *port)
 	uint8_t portnum;
 	char path[PATH_MAX+1];
 
-	for (unsigned int i = 0; i < strlen(port); i++)
+	unsigned int port_len = strlen(port);
+
+	for (unsigned int i = 0; i < port_len; i++)
 		if (!isdigit(port[i])) {
 			err("invalid port %s", port);
 			return -1;
#1 lore raw

Hi,

Quoted 11 lines · Eric Curtin

Instead of calling strlen on every iteration of the for loop, just call it once and cache the result in a temporary local variable which will be used in the for loop instead.

tools/usb/usbip/src/usbip_detach.c +0 -0
index 05c6d15..9db9d21 100644

tools/usb/usbip/src/usbip_detach.c static int detach_port(char *port) · line 49

Quoted 3 lines
tools/usb/usbip/src/usbip_detach.c +0 -0
@@ -47,7 +47,9 @@ static int detach_port(char *port)
 	uint8_t portnum;
 	char path[PATH_MAX+1];
 

The above blank line should be deleted if you declare a new local variable below...

tools/usb/usbip/src/usbip_detach.c static int detach_port(char *port) · line 52

Quoted 4 lines
tools/usb/usbip/src/usbip_detach.c +3 -1
-	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?

A.

#2 lore raw
Quoted 8 lines · Aaro Koskinen

tools/usb/usbip/src/usbip_detach.c static int detach_port(char *port) · line 52

Quoted 4 lines · Eric Curtin
tools/usb/usbip/src/usbip_detach.c +3 -1
-	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

#3 lore raw
In reply to Aaro Koskinen

From: Aaro Koskinen

Quoted 1 line

Sent: 15 September 2015 21:56

...

Quoted 7 lines

tools/usb/usbip/src/usbip_detach.c static int detach_port(char *port) · line 52

Quoted 4 lines
tools/usb/usbip/src/usbip_detach.c +3 -1
-	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?

While I've seen the compiler make the assumption, I'm not sure it should assume that data that is 'const' in one function cannot be modified by a called function.
(Unless the compiler has some way of knowing that the called function cannot obtain a non-const pointer to the referenced data.) (This is also independent of whether the const pointer is passed to the function.)

David