Optimize prev. commit for speed.

1) Don't iterate the loop from the environment array beginning each time,
iterate it under the last place we deactivate instead.
2) Call __rebuild_environ() not on each iteration but once, only at the end
of whole loop (of course, only in case if something is changed).

MFC after:      1 week
This commit is contained in:
Andrey A. Chernov 2012-10-03 04:46:58 +00:00
parent e9f7ad1fa4
commit 542e13498f

View File

@ -662,6 +662,7 @@ unsetenv(const char *name)
{
int envNdx;
size_t nameLen;
int newEnvActive;
/* Check for malformed name. */
if (name == NULL || (nameLen = __strleneq(name)) == 0) {
@ -674,15 +675,18 @@ unsetenv(const char *name)
return (-1);
/* Deactivate specified variable. */
envNdx = envVarsTotal - 1;
/* Remove all occurrences. */
envNdx = envVarsTotal - 1;
newEnvActive = envActive;
while (__findenv(name, nameLen, &envNdx, true) != NULL) {
envVars[envNdx].active = false;
if (envVars[envNdx].putenv)
__remove_putenv(envNdx);
__rebuild_environ(envActive - 1);
envNdx = envVarsTotal - 1;
envNdx--;
newEnvActive--;
}
if (newEnvActive != envActive)
__rebuild_environ(newEnvActive);
return (0);
}