# Syscall Optimization

- Thread: https://externals.io/message/39115
- Original archive: https://news-web.php.net/php.internals/39115
- Started by Rasmus Lerdorf on 2008-07-20T14:58:40+00:00
- Messages: 1
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #39115 — Rasmus Lerdorf — 2008-07-20T14:58:40+00:00

URL: https://externals.io/message/39115#39115

On most requests we end up with a `getcwd()` followed by a `chdir()` to the 
same directory and then a second `chdir()` to the same directory at the 
end of the request:

getcwd("/var/www", 4095)           = 14
chdir("/var/www")                  = 0
... request is processed ...
chdir("/var/www")                  = 0

which comes from this code in main.c:

         if ((primary_file->type == ZEND_HANDLE_FILENAME || 
primary_file->type == ZEND_HANDLE_STREAM) && primary_file->filename) {
             VCWD_GETCWD(old_cwd, OLD_CWD_SIZE-1);
             VCWD_CHDIR_FILE(primary_file->filename);
         }

...

     if (old_cwd[0] != '\0') {
         VCWD_CHDIR(old_cwd);
     }
     free_alloca(old_cwd, use_heap);

We probably can't get rid of the second `chdir()` since we should preserve 
our sandbox and leave the process in the same state that we started in 
and all sorts of things along the way could theoretically have changed 
the cwd.  But we should be able to get rid of that first `chdir()` when 
the directory we want to change to is the same as old_cwd.

Perhaps a new macro to not break other code using it?

VCWD_GETCWD_CHDIR_FILE(primary_file->filename, old_cwd, OLD_CWD_SIZE-1)

And have this macro only do the chdir if the directories are different.

-Rasmus
