
On 09/24/2012 01:53 PM, Richard W.M. Jones wrote:
+ /* Search for the name in the existing environment. */ + namelen = strcspn(env, "=");
Would 'strchr(env, '=') - env' be any more efficient? But that's a micro-optimization, probably not worth worrying about.
I guess I trust glibc or gcc to have these string primitives optimized better than I could.
Ah, but glibc is open source, so we can check for ourselves: The naive C fallback when no .S is present is highly unoptimized. From glibcc/string/strcspn.c: size_t strcspn (s, reject) const char *s; const char *reject; { size_t count = 0; while (*s != '\0') if (strchr (reject, *s++) == NULL) ++count; else return count; return count; } and even in the .S optimized versions, there's still no shortcuts taken for a one-character reject (possibly worth filing a BZ about the missed optimization, though). From glibc/sysdeps/x86_64/strcspn.S: /* First we create a table with flags for all possible characters. For the ASCII (7bit/8bit) or ISO-8859-X character sets which are supported by the C string functions we have 256 characters. Before inserting marks for the stop characters we clear the whole table. */ movq %rdi, %r8 /* Save value. */ subq $256, %rsp /* Make space for 256 bytes. */ cfi_adjust_cfa_offset(256) movl $32, %ecx /* 32*8 bytes = 256 bytes. */ movq %rsp, %rdi xorl %eax, %eax /* We store 0s. */ ... That is, you are definitely wasting time pre-computing the reject table, compared to doing a strchr() for the one rejection. -- Eric Blake eblake@redhat.com +1-919-301-3266 Libvirt virtualization library http://libvirt.org