com php-src: Avoid repeatable strlen() calls: Zend/zend_execu te.h Zend/zend_execute_API.c ext/opcache/ZendAccele rator.c main/fopen_wrappers.c
main/streams/plain_wr apper.c
| From: | Dmitry Stogov | Date: | Thu, 05 Mar 2015 15:18:39 +0000 |
| Subject: | com php-src: Avoid repeatable strlen() calls: Zend/zend_execu te.h Zend/zend_execute_API.c ext/opcache/ZendAccele rator.c main/fopen_wrappers.c main/streams/plain_wr apper.c |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-85262@lists.php.net to get a copy of this message | ||
Commit: 6d065a8545291a8048ef307476c4358922ca3a61
Author: Dmitry Stogov <dmitry@zend.com> Thu, 5 Mar 2015 18:18:39 +0300
Parents: bb91bf83f9fc60cbd6cb30e6b2a7858efe1aa4c5
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=6d065a8545291a8048ef307476c4358922ca3a61
Log:
Avoid repeatable strlen() calls
Changed paths:
M Zend/zend_execute.h
M Zend/zend_execute_API.c
M ext/opcache/ZendAccelerator.c
M main/fopen_wrappers.c
M main/streams/plain_wrapper.c
Diff:
diff --git a/Zend/zend_execute.h b/Zend/zend_execute.h
index 4298f6b..d337352 100644
--- a/Zend/zend_execute..h
+++ b/Zend/zend_execute.h
@@ -221,6 +221,7 @@ static zend_always_inline void zend_vm_stack_free_call_frame(zend_execute_data *
ZEND_API const char *get_active_class_name(const char **space);
ZEND_API const char *get_active_function_name(void);
ZEND_API const char *zend_get_executed_filename(void);
+ZEND_API zend_string *zend_get_executed_filename_ex(void);
ZEND_API uint zend_get_executed_lineno(void);
ZEND_API zend_bool zend_is_executing(void);
diff --git a/Zend/zend_execute_API.c b/Zend/zend_execute_API.c
index f52d9f3..3c1bd87 100644
--- a/Zend/zend_execute_API.c
+++ b/Zend/zend_execute_API.c
@@ -461,6 +461,21 @@ ZEND_API const char *zend_get_executed_filename(void) /* {{{ */
}
/* }}} */
+ZEND_API zend_string *zend_get_executed_filename_ex(void) /* {{{ */
+{
+ zend_execute_data *ex = EG(current_execute_data);
+
+ while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
+ ex = ex->prev_execute_data;
+ }
+ if (ex) {
+ return ex->func->op_array.filename;
+ } else {
+ return NULL;
+ }
+}
+/* }}} */
+
ZEND_API uint zend_get_executed_lineno(void) /* {{{ */
{
zend_execute_data *ex = EG(current_execute_data);
diff --git a/ext/opcache/ZendAccelerator.c b/ext/opcache/ZendAccelerator.c
index a1e7d9c..8b56c86 100644
--- a/ext/opcache/ZendAccelerator.c
+++ b/ext/opcache/ZendAccelerator.c
@@ -927,8 +927,8 @@ char *accel_make_persistent_key_ex(zend_file_handle *file_handle, int path_lengt
!is_stream_path(file_handle->filename)) {
char *include_path = NULL;
int include_path_len = 0;
- const char *parent_script = NULL;
- int parent_script_len = 0;
+ zend_string *parent_script = NULL;
+ size_t parent_script_len = 0;
int cur_len = 0;
int cwd_len;
char *cwd;
@@ -990,11 +990,10 @@ char *accel_make_persistent_key_ex(zend_file_handle *file_handle, int
path_lengt
in include path too.
*/
if (EG(current_execute_data) &&
- (parent_script = zend_get_executed_filename()) != NULL &&
- parent_script[0] != '[') {
+ (parent_script = zend_get_executed_filename_ex()) != NULL) {
- parent_script_len = strlen(parent_script);
- while ((--parent_script_len > 0) &&
!IS_SLASH(parent_script[parent_script_len]));
+ parent_script_len = parent_script->len;
+ while ((--parent_script_len > 0) &&
!IS_SLASH(parent_script->val[parent_script_len]));
}
/* Calculate key length */
@@ -1022,7 +1021,7 @@ char *accel_make_persistent_key_ex(zend_file_handle *file_handle, int
path_lengt
cur_len = cwd_len + 1 + path_length + 1;
if (parent_script_len) {
- memcpy(ZCG(key) + cur_len, parent_script, parent_script_len);
+ memcpy(ZCG(key) + cur_len, parent_script->val, parent_script_len);
cur_len += parent_script_len;
ZCG(key)[cur_len] = ':';
cur_len++;
diff --git a/main/fopen_wrappers.c b/main/fopen_wrappers.c
index c516481..396b0c5 100644
--- a/main/fopen_wrappers.c
+++ b/main/fopen_wrappers.c
@@ -493,6 +493,7 @@ PHPAPI zend_string *php_resolve_path(const char *filename, int filename_length,
const char *ptr, *end, *p;
const char *actual_path;
php_stream_wrapper *wrapper;
+ zend_string *exec_filename;
if (!filename || CHECK_NULL_PATH(filename, filename_length)) {
return NULL;
@@ -580,13 +581,13 @@ PHPAPI zend_string *php_resolve_path(const char *filename, int
filename_length,
/* check in calling scripts' current working directory as a fall back case
*/
- if (zend_is_executing()) {
- const char *exec_fname = zend_get_executed_filename();
- int exec_fname_length = (int)strlen(exec_fname);
+ if (zend_is_executing() &&
+ (exec_filename = zend_get_executed_filename_ex()) != NULL) {
+ const char *exec_fname = exec_filename->val;
+ size_t exec_fname_length = exec_filename->len;
while ((--exec_fname_length >= 0) && !IS_SLASH(exec_fname[exec_fname_length]));
- if (exec_fname && exec_fname[0] != '[' &&
- exec_fname_length > 0 &&
+ if (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);
@@ -627,12 +628,10 @@ PHPAPI zend_string *php_resolve_path(const char *filename, int
filename_length,
PHPAPI FILE *php_fopen_with_path(const char *filename, const char *mode, const char *path,
zend_string **opened_path)
{
char *pathbuf, *ptr, *end;
- const char *exec_fname;
char trypath[MAXPATHLEN];
FILE *fp;
- int path_length;
int filename_length;
- int exec_fname_length;
+ zend_string *exec_filename;
if (opened_path) {
*opened_path = NULL;
@@ -660,16 +659,18 @@ PHPAPI FILE *php_fopen_with_path(const char *filename, const char *mode, const
c
/* append the calling scripts' current working directory
* as a fall back case
*/
- if (zend_is_executing()) {
- exec_fname = zend_get_executed_filename();
- exec_fname_length = (int)strlen(exec_fname);
- path_length = (int)strlen(path);
+ if (zend_is_executing() &&
+ (exec_filename = zend_get_executed_filename_ex()) != NULL) {
+ const char *exec_fname = exec_filename->val;
+ size_t exec_fname_length = exec_filename->len;
while ((--exec_fname_length >= 0) && !IS_SLASH(exec_fname[exec_fname_length]));
if ((exec_fname && exec_fname[0] == '[') || exec_fname_length <= 0) {
/* [no active file] or no path */
pathbuf = estrdup(path);
} else {
+ size_t path_length = strlen(path);
+
pathbuf = (char *) emalloc(exec_fname_length + path_length + 1 + 1);
memcpy(pathbuf, path, path_length);
pathbuf[path_length] = DEFAULT_DIR_SEPARATOR;
diff --git a/main/streams/plain_wrapper.c b/main/streams/plain_wrapper.c
index 83bd059..6af8ffa 100644
--- a/main/streams/plain_wrapper.c
+++ b/main/streams/plain_wrapper.c
@@ -1390,12 +1390,10 @@ PHPAPI php_stream *_php_stream_fopen_with_path(const char *filename, const
char
/* code ripped off from fopen_wrappers.c */
char *pathbuf, *end;
const char *ptr;
- const char *exec_fname;
char trypath[MAXPATHLEN];
php_stream *stream;
- int path_length;
int filename_length;
- int exec_fname_length;
+ zend_string *exec_filename;
if (opened_path) {
*opened_path = NULL;
@@ -1471,17 +1469,18 @@ not_relative_path:
/* append the calling scripts' current working directory
* as a fall back case
*/
- if (zend_is_executing()) {
- exec_fname = zend_get_executed_filename();
- exec_fname_length = (int)strlen(exec_fname);
- path_length = (int)strlen(path);
+ if (zend_is_executing() &&
+ (exec_filename = zend_get_executed_filename_ex()) != NULL) {
+ const char *exec_fname = exec_filename->val;
+ size_t exec_fname_length = exec_filename->len;
while ((--exec_fname_length >= 0) && !IS_SLASH(exec_fname[exec_fname_length]));
- if ((exec_fname && exec_fname[0] == '[')
- || exec_fname_length<=0) {
- /* [no active file] or no path */
+ if (exec_fname_length<=0) {
+ /* no path */
pathbuf = estrdup(path);
} else {
+ size_t path_length = strlen(path);
+
pathbuf = (char *) emalloc(exec_fname_length + path_length +1 +1);
memcpy(pathbuf, path, path_length);
pathbuf[path_length] = DEFAULT_DIR_SEPARATOR;