dmitry Wed Mar 5 13:35:02 2008 UTC Modified files: /ZendEngine2 zend.c zend.h zend_vm_def.h zend_vm_execute.h /php-src/main fopen_wrappers.c fopen_wrappers.h main.c Log: Optimized require_once() and include_once() by eliminationg open() syscall on se cond usage.
http://cvs.php.net/viewvc.cgi/ZendEngine2/zend.c?r1=1.407&r2=1.408&diff_format=u Index: ZendEngine2/zend.c diff -u ZendEngine2/zend.c:1.407 ZendEngine2/zend.c:1.408 --- ZendEngine2/zend.c:1.407 Sat Feb 23 17:03:51 2008 +++ ZendEngine2/zend.c Wed Mar 5 13:35:01 2008 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: zend.c,v 1.407 2008/02/23 17:03:51 helly Exp $ */ +/* $Id: zend.c,v 1.408 2008/03/05 13:35:01 dmitry Exp $ */ #include "zend.h" #include "zend_extensions.h" @@ -65,6 +65,7 @@ ZEND_API void (*zend_error_cb)(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args); int (*zend_vspprintf)(char **pbuf, size_t max_len, const char *format, va_list ap); ZEND_API char *(*zend_getenv)(char *name, size_t name_len TSRMLS_DC); +ZEND_API char *(*zend_resolve_path)(const char *filename, int filename_len TSRMLS_DC); void (*zend_on_timeout)(int seconds TSRMLS_DC); @@ -1067,6 +1068,7 @@ zend_on_timeout = utility_functions->on_timeout; zend_vspprintf = utility_functions->vspprintf_function; zend_getenv = utility_functions->getenv_function; + zend_resolve_path = utility_functions->resolve_path_function; zend_compile_file = compile_file; zend_compile_string = compile_string; http://cvs.php.net/viewvc.cgi/ZendEngine2/zend.h?r1=1.349&r2=1.350&diff_format=u Index: ZendEngine2/zend.h diff -u ZendEngine2/zend.h:1.349 ZendEngine2/zend.h:1.350 --- ZendEngine2/zend.h:1.349 Tue Jan 22 09:29:29 2008 +++ ZendEngine2/zend.h Wed Mar 5 13:35:01 2008 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: zend.h,v 1.349 2008/01/22 09:29:29 dmitry Exp $ */ +/* $Id: zend.h,v 1.350 2008/03/05 13:35:01 dmitry Exp $ */ #ifndef ZEND_H #define ZEND_H @@ -503,6 +503,7 @@ int (*stream_open_function)(const char *filename, zend_file_handle *handle TSRMLS_DC); int (*vspprintf_function)(char **pbuf, size_t max_len, const char *format, va_list ap); char *(*getenv_function)(char *name, size_t name_len TSRMLS_DC); + char *(*resolve_path_function)(const char *filename, int filename_len TSRMLS_DC); } zend_utility_functions; typedef struct _zend_utility_values { @@ -640,6 +641,7 @@ extern ZEND_API int (*zend_stream_open_function)(const char *filename, zend_file_handle *handle TSRMLS_DC); extern int (*zend_vspprintf)(char **pbuf, size_t max_len, const char *format, va_list ap); extern ZEND_API char *(*zend_getenv)(char *name, size_t name_len TSRMLS_DC); +extern ZEND_API char *(*zend_resolve_path)(const char *filename, int filename_len TSRMLS_DC); ZEND_API void zend_error(int type, const char *format, ...); http://cvs.php.net/viewvc.cgi/ZendEngine2/zend_vm_def.h?r1=1.217&r2=1.218&diff_format=u Index: ZendEngine2/zend_vm_def.h diff -u ZendEngine2/zend_vm_def.h:1.217 ZendEngine2/zend_vm_def.h:1.218 --- ZendEngine2/zend_vm_def.h:1.217 Tue Mar 4 11:44:15 2008 +++ ZendEngine2/zend_vm_def.h Wed Mar 5 13:35:01 2008 @@ -18,7 +18,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: zend_vm_def.h,v 1.217 2008/03/04 11:44:15 dmitry Exp $ */ +/* $Id: zend_vm_def.h,v 1.218 2008/03/05 13:35:01 dmitry Exp $ */ /* If you change this file, please regenerate the zend_vm_execute.h and * zend_vm_opcodes.h files by running: @@ -3155,27 +3155,23 @@ case ZEND_INCLUDE_ONCE: case ZEND_REQUIRE_ONCE: { zend_file_handle file_handle; - - if (IS_ABSOLUTE_PATH(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename))) { - cwd_state state; - - state.cwd_length = 0; - state.cwd = malloc(1); - state.cwd[0] = 0; - - failure_retval = (!virtual_file_ex(&state, Z_STRVAL_P(inc_filename), NULL, 1) && - zend_hash_exists(&EG(included_files), state.cwd, state.cwd_length+1)); - - free(state.cwd); + char *resolved_path; + + resolved_path = zend_resolve_path(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename) TSRMLS_CC); + if (resolved_path) { + failure_retval = zend_hash_exists(&EG(included_files), resolved_path, strlen(resolved_path)+1); + } else { + resolved_path = Z_STRVAL_P(inc_filename); } + + if (failure_retval) { + /* do nothing, file already included */ + } else if (SUCCESS == zend_stream_open(resolved_path, &file_handle TSRMLS_CC)) { - if (failure_retval) { - /* do nothing */ - } else if (SUCCESS == zend_stream_open(Z_STRVAL_P(inc_filename), &file_handle TSRMLS_CC)) { if (!file_handle.opened_path) { - file_handle.opened_path = estrndup(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename)); + file_handle.opened_path = estrdup(resolved_path); } - + if (zend_hash_add_empty_element(&EG(included_files), file_handle.opened_path, strlen(file_handle.opened_path)+1)==SUCCESS) { new_op_array = zend_compile_file(&file_handle, (Z_LVAL(opline->op2.u.constant)==ZEND_INCLUDE_ONCE?ZEND_INCLUDE:ZEND_REQUIRE) TSRMLS_CC); zend_destroy_file_handle(&file_handle TSRMLS_CC); @@ -3190,6 +3186,9 @@ zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, Z_STRVAL_P(inc_filename)); } } + if (resolved_path != Z_STRVAL_P(inc_filename)) { + efree(resolved_path); + } } break; case ZEND_INCLUDE: http://cvs.php.net/viewvc.cgi/ZendEngine2/zend_vm_execute.h?r1=1.221&r2=1.222&diff_format=u Index: ZendEngine2/zend_vm_execute.h diff -u ZendEngine2/zend_vm_execute.h:1.221 ZendEngine2/zend_vm_execute.h:1.222 --- ZendEngine2/zend_vm_execute.h:1.221 Tue Mar 4 11:44:15 2008 +++ ZendEngine2/zend_vm_execute.h Wed Mar 5 13:35:01 2008 @@ -1696,25 +1696,21 @@ case ZEND_INCLUDE_ONCE: case ZEND_REQUIRE_ONCE: { zend_file_handle file_handle; + char *resolved_path; - if (IS_ABSOLUTE_PATH(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename))) { - cwd_state state; - - state.cwd_length = 0; - state.cwd = malloc(1); - state.cwd[0] = 0; - - failure_retval = (!virtual_file_ex(&state, Z_STRVAL_P(inc_filename), NULL, 1) && - zend_hash_exists(&EG(included_files), state.cwd, state.cwd_length+1)); - - free(state.cwd); + resolved_path = zend_resolve_path(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename) TSRMLS_CC); + if (resolved_path) { + failure_retval = zend_hash_exists(&EG(included_files), resolved_path, strlen(resolved_path)+1); + } else { + resolved_path = Z_STRVAL_P(inc_filename); } - if (failure_retval) { - /* do nothing */ - } else if (SUCCESS == zend_stream_open(Z_STRVAL_P(inc_filename), &file_handle TSRMLS_CC)) { + if (failure_retval) { + /* do nothing, file already included */ + } else if (SUCCESS == zend_stream_open(resolved_path, &file_handle TSRMLS_CC)) { + if (!file_handle.opened_path) { - file_handle.opened_path = estrndup(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename)); + file_handle.opened_path = estrdup(resolved_path); } if (zend_hash_add_empty_element(&EG(included_files), file_handle.opened_path, strlen(file_handle.opened_path)+1)==SUCCESS) { @@ -1731,6 +1727,9 @@ zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, Z_STRVAL_P(inc_filename)); } } + if (resolved_path != Z_STRVAL_P(inc_filename)) { + efree(resolved_path); + } } break; case ZEND_INCLUDE: @@ -5004,25 +5003,21 @@ case ZEND_INCLUDE_ONCE: case ZEND_REQUIRE_ONCE: { zend_file_handle file_handle; + char *resolved_path; - if (IS_ABSOLUTE_PATH(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename))) { - cwd_state state; - - state.cwd_length = 0; - state.cwd = malloc(1); - state.cwd[0] = 0; - - failure_retval = (!virtual_file_ex(&state, Z_STRVAL_P(inc_filename), NULL, 1) && - zend_hash_exists(&EG(included_files), state.cwd, state.cwd_length+1)); - - free(state.cwd); + resolved_path = zend_resolve_path(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename) TSRMLS_CC); + if (resolved_path) { + failure_retval = zend_hash_exists(&EG(included_files), resolved_path, strlen(resolved_path)+1); + } else { + resolved_path = Z_STRVAL_P(inc_filename); } - if (failure_retval) { - /* do nothing */ - } else if (SUCCESS == zend_stream_open(Z_STRVAL_P(inc_filename), &file_handle TSRMLS_CC)) { + if (failure_retval) { + /* do nothing, file already included */ + } else if (SUCCESS == zend_stream_open(resolved_path, &file_handle TSRMLS_CC)) { + if (!file_handle.opened_path) { - file_handle.opened_path = estrndup(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename)); + file_handle.opened_path = estrdup(resolved_path); } if (zend_hash_add_empty_element(&EG(included_files), file_handle.opened_path, strlen(file_handle.opened_path)+1)==SUCCESS) { @@ -5039,6 +5034,9 @@ zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, Z_STRVAL_P(inc_filename)); } } + if (resolved_path != Z_STRVAL_P(inc_filename)) { + efree(resolved_path); + } } break; case ZEND_INCLUDE: @@ -8346,25 +8344,21 @@ case ZEND_INCLUDE_ONCE: case ZEND_REQUIRE_ONCE: { zend_file_handle file_handle; + char *resolved_path; - if (IS_ABSOLUTE_PATH(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename))) { - cwd_state state; - - state.cwd_length = 0; - state.cwd = malloc(1); - state.cwd[0] = 0; - - failure_retval = (!virtual_file_ex(&state, Z_STRVAL_P(inc_filename), NULL, 1) && - zend_hash_exists(&EG(included_files), state.cwd, state.cwd_length+1)); - - free(state.cwd); + resolved_path = zend_resolve_path(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename) TSRMLS_CC); + if (resolved_path) { + failure_retval = zend_hash_exists(&EG(included_files), resolved_path, strlen(resolved_path)+1); + } else { + resolved_path = Z_STRVAL_P(inc_filename); } - if (failure_retval) { - /* do nothing */ - } else if (SUCCESS == zend_stream_open(Z_STRVAL_P(inc_filename), &file_handle TSRMLS_CC)) { + if (failure_retval) { + /* do nothing, file already included */ + } else if (SUCCESS == zend_stream_open(resolved_path, &file_handle TSRMLS_CC)) { + if (!file_handle.opened_path) { - file_handle.opened_path = estrndup(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename)); + file_handle.opened_path = estrdup(resolved_path); } if (zend_hash_add_empty_element(&EG(included_files), file_handle.opened_path, strlen(file_handle.opened_path)+1)==SUCCESS) { @@ -8381,6 +8375,9 @@ zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, Z_STRVAL_P(inc_filename)); } } + if (resolved_path != Z_STRVAL_P(inc_filename)) { + efree(resolved_path); + } } break; case ZEND_INCLUDE: @@ -22603,25 +22600,21 @@ case ZEND_INCLUDE_ONCE: case ZEND_REQUIRE_ONCE: { zend_file_handle file_handle; + char *resolved_path; - if (IS_ABSOLUTE_PATH(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename))) { - cwd_state state; - - state.cwd_length = 0; - state.cwd = malloc(1); - state.cwd[0] = 0; - - failure_retval = (!virtual_file_ex(&state, Z_STRVAL_P(inc_filename), NULL, 1) && - zend_hash_exists(&EG(included_files), state.cwd, state.cwd_length+1)); - - free(state.cwd); + resolved_path = zend_resolve_path(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename) TSRMLS_CC); + if (resolved_path) { + failure_retval = zend_hash_exists(&EG(included_files), resolved_path, strlen(resolved_path)+1); + } else { + resolved_path = Z_STRVAL_P(inc_filename); } - if (failure_retval) { - /* do nothing */ - } else if (SUCCESS == zend_stream_open(Z_STRVAL_P(inc_filename), &file_handle TSRMLS_CC)) { + if (failure_retval) { + /* do nothing, file already included */ + } else if (SUCCESS == zend_stream_open(resolved_path, &file_handle TSRMLS_CC)) { + if (!file_handle.opened_path) { - file_handle.opened_path = estrndup(Z_STRVAL_P(inc_filename), Z_STRLEN_P(inc_filename)); + file_handle.opened_path = estrdup(resolved_path); } if (zend_hash_add_empty_element(&EG(included_files), file_handle.opened_path, strlen(file_handle.opened_path)+1)==SUCCESS) { @@ -22638,6 +22631,9 @@ zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, Z_STRVAL_P(inc_filename)); } } + if (resolved_path != Z_STRVAL_P(inc_filename)) { + efree(resolved_path); + } } break; case ZEND_INCLUDE: http://cvs.php.net/viewvc.cgi/php-src/main/fopen_wrappers.c?r1=1.202&r2=1.203&diff_format=u Index: php-src/main/fopen_wrappers.c diff -u php-src/main/fopen_wrappers.c:1.202 php-src/main/fopen_wrappers.c:1.203 --- php-src/main/fopen_wrappers.c:1.202 Tue Jan 29 14:25:07 2008 +++ php-src/main/fopen_wrappers.c Wed Mar 5 13:35:01 2008 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: fopen_wrappers.c,v 1.202 2008/01/29 14:25:07 dmitry Exp $ */ +/* $Id: fopen_wrappers.c,v 1.203 2008/03/05 13:35:01 dmitry Exp $ */ /* {{{ includes */ @@ -447,6 +447,80 @@ } /* }}} */ +/* {{{ php_resolve_path + * Returns the realpath for given filename according to include path + */ +PHPAPI char *php_resolve_path(const char *filename, int filename_length, const char *path TSRMLS_DC) +{ + char resolved_path[MAXPATHLEN]; + char trypath[MAXPATHLEN]; + char *ptr, *end; + + if (!filename) { + return NULL; + } + + if (*filename == '.' || + IS_ABSOLUTE_PATH(filename, filename_length) || + !path || + !*path) { + if (tsrm_realpath(filename, resolved_path TSRMLS_CC)) { + return estrdup(resolved_path); + } else { + return NULL; + } + } + + ptr = path; + while (ptr && *ptr) { + end = strchr(ptr, DEFAULT_DIR_SEPARATOR); + if (end) { + if ((end-ptr) + 1 + filename_length + 1 >= MAXPATHLEN) { + ptr = end + 1; + continue; + } + memcpy(trypath, ptr, end-ptr); + trypath[end-ptr] = '/'; + memcpy(trypath+(end-ptr)+1, filename, filename_length+1); + ptr = end+1; + } else { + int len = strlen(ptr); + + if (len + 1 + filename_length + 1 >= MAXPATHLEN) { + break; + } + memcpy(trypath, ptr, len); + trypath[len] = '/'; + memcpy(trypath+len+1, filename, filename_length+1); + ptr = NULL; + } + if (tsrm_realpath(trypath, resolved_path TSRMLS_CC)) { + return estrdup(resolved_path); + } + } /* end provided path */ + + /* check in calling scripts' current working directory as a fall back case + */ + if (zend_is_executing(TSRMLS_C)) { + char *exec_fname = zend_get_executed_filename(TSRMLS_C); + int exec_fname_length = strlen(exec_fname); + + while ((--exec_fname_length >= 0) && !IS_SLASH(exec_fname[exec_fname_length])); + if (exec_fname && exec_fname[0] != '[' && + exec_fname_length > 0 && + exec_fname_length + 1 + filename_length + 1 < MAXPATHLEN) { + memcpy(trypath, exec_fname, exec_fname_length + 1); + memcpy(trypath+exec_fname_length + 1, filename, filename_length+1); + if (tsrm_realpath(trypath, resolved_path TSRMLS_CC)) { + return estrdup(resolved_path); + } + } + } + + return NULL; +} +/* }}} */ + /* {{{ php_fopen_with_path * Tries to open a file with a PATH-style list of directories. * If the filename starts with "." or "/", the path is ignored. http://cvs.php.net/viewvc.cgi/php-src/main/fopen_wrappers.h?r1=1.51&r2=1.52&diff_format=u Index: php-src/main/fopen_wrappers.h diff -u php-src/main/fopen_wrappers.h:1.51 php-src/main/fopen_wrappers.h:1.52 --- php-src/main/fopen_wrappers.h:1.51 Mon Dec 31 07:12:18 2007 +++ php-src/main/fopen_wrappers.h Wed Mar 5 13:35:02 2008 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: fopen_wrappers.h,v 1.51 2007/12/31 07:12:18 sebastian Exp $ */ +/* $Id: fopen_wrappers.h,v 1.52 2008/03/05 13:35:02 dmitry Exp $ */ #ifndef FOPEN_WRAPPERS_H #define FOPEN_WRAPPERS_H @@ -32,6 +32,8 @@ PHPAPI int php_check_open_basedir_ex(const char *path, int warn TSRMLS_DC); PHPAPI int php_check_specific_open_basedir(const char *basedir, const char *path TSRMLS_DC); +PHPAPI char *php_resolve_path(const char *filename, int filename_len, const char *path TSRMLS_DC); + PHPAPI FILE *php_fopen_with_path(const char *filename, const char *mode, const char *path, char **opened_path TSRMLS_DC); PHPAPI char *php_strip_url_passwd(char *path); http://cvs.php.net/viewvc.cgi/php-src/main/main.c?r1=1.760&r2=1.761&diff_format=u Index: php-src/main/main.c diff -u php-src/main/main.c:1.760 php-src/main/main.c:1.761 --- php-src/main/main.c:1.760 Sat Feb 23 17:03:53 2008 +++ php-src/main/main.c Wed Mar 5 13:35:02 2008 @@ -18,7 +18,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: main.c,v 1.760 2008/02/23 17:03:53 helly Exp $ */ +/* $Id: main.c,v 1.761 2008/03/05 13:35:02 dmitry Exp $ */ /* {{{ includes */ @@ -1202,6 +1202,12 @@ } /* }}} */ +static char *php_resolve_path_for_zend(const char *filename, int filename_len TSRMLS_DC) /* {{{ */ +{ + return php_resolve_path(filename, filename_len, PG(include_path) TSRMLS_CC); +} +/* }}} */ + /* {{{ php_get_configuration_directive_for_zend */ static int php_get_configuration_directive_for_zend(char *name, uint name_length, zval *contents) @@ -1816,6 +1822,7 @@ zuf.stream_open_function = php_stream_open_for_zend; zuf.vspprintf_function = vspprintf; zuf.getenv_function = sapi_getenv; + zuf.resolve_path_function = php_resolve_path_for_zend; zend_startup(&zuf, NULL, 1); #ifdef ZTS
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php