]> granicus.if.org Git - php/commitdiff
Avoid temporary string creation and destruction.
authorDmitry Stogov <dmitry@zend.com>
Mon, 11 Dec 2017 15:18:30 +0000 (18:18 +0300)
committerDmitry Stogov <dmitry@zend.com>
Mon, 11 Dec 2017 15:18:30 +0000 (18:18 +0300)
Zend/zend_ini.c
Zend/zend_ini.h
ext/standard/basic_functions.c

index 129c7824995fe40fffda1d116436321037b15fb3..cb711cbaebd295b44dd0ff6dbc96f91addf39754 100644 (file)
@@ -496,6 +496,19 @@ ZEND_API char *zend_ini_string(char *name, size_t name_length, int orig) /* {{{
 }
 /* }}} */
 
+ZEND_API zend_string *zend_ini_get_value(zend_string *name) /* {{{ */
+{
+       zend_ini_entry *ini_entry;
+
+       ini_entry = zend_hash_find_ptr(EG(ini_directives), name);
+       if (ini_entry) {
+               return ini_entry->value ? ini_entry->value : ZSTR_EMPTY_ALLOC();
+       } else {
+               return NULL;
+       }
+}
+/* }}} */
+
 #if TONY_20070307
 static void zend_ini_displayer_cb(zend_ini_entry *ini_entry, int type) /* {{{ */
 {
index 8f91ef0d150a0e0cd437240fa38c8db61ab65dc6..cfea8da3fb7346b1e34abf8acc17992fabafbe03 100644 (file)
@@ -87,6 +87,7 @@ ZEND_API zend_long zend_ini_long(char *name, size_t name_length, int orig);
 ZEND_API double zend_ini_double(char *name, size_t name_length, int orig);
 ZEND_API char *zend_ini_string(char *name, size_t name_length, int orig);
 ZEND_API char *zend_ini_string_ex(char *name, size_t name_length, int orig, zend_bool *exists);
+ZEND_API zend_string *zend_ini_get_value(zend_string *name);
 
 ZEND_API int zend_ini_register_displayer(char *name, uint32_t name_length, void (*displayer)(zend_ini_entry *ini_entry, int type));
 
index 278ed08ff6d1464e54ae2a91953939f5048fe221..f904a2ad311e38f6f328073be9e08f1f0f14fd34 100644 (file)
@@ -5352,26 +5352,29 @@ PHP_FUNCTION(highlight_string)
    Get a configuration option */
 PHP_FUNCTION(ini_get)
 {
-       char *varname, *str;
-       size_t varname_len, len;
+       zend_string *varname, *val;
 
        ZEND_PARSE_PARAMETERS_START(1, 1)
-               Z_PARAM_STRING(varname, varname_len)
+               Z_PARAM_STR(varname)
        ZEND_PARSE_PARAMETERS_END();
 
-       str = zend_ini_string(varname, (uint32_t)varname_len, 0);
+       val = zend_ini_get_value(varname);
 
-       if (!str) {
+       if (!val) {
                RETURN_FALSE;
        }
 
-       len = strlen(str);
-       if (len == 0) {
-               RETURN_EMPTY_STRING();
-       } else if (len == 1) {
-               RETURN_INTERNED_STR(ZSTR_CHAR((zend_uchar)str[0]));
+       if (ZSTR_IS_INTERNED(val)) {
+               RETVAL_INTERNED_STR(val);
+       } else if (ZSTR_LEN(val) == 0) {
+               RETVAL_EMPTY_STRING();
+       } else if (ZSTR_LEN(val) == 1) {
+               RETVAL_INTERNED_STR(ZSTR_CHAR((zend_uchar)ZSTR_VAL(val)[0]));
+       } else if (!(GC_FLAGS(val) & GC_PERSISTENT)) {
+               ZVAL_NEW_STR(return_value, zend_string_copy(val));
+       } else {
+               ZVAL_NEW_STR(return_value, zend_string_init(ZSTR_VAL(val), ZSTR_LEN(val), 0));
        }
-       RETURN_STRINGL(str, len);
 }
 /* }}} */
 
@@ -5471,25 +5474,27 @@ PHP_FUNCTION(ini_set)
 {
        zend_string *varname;
        zend_string *new_value;
-       char *old_value;
+       zend_string *val;
 
        ZEND_PARSE_PARAMETERS_START(2, 2)
                Z_PARAM_STR(varname)
                Z_PARAM_STR(new_value)
        ZEND_PARSE_PARAMETERS_END();
 
-       old_value = zend_ini_string(ZSTR_VAL(varname), ZSTR_LEN(varname), 0);
+       val = zend_ini_get_value(varname);
 
        /* copy to return here, because alter might free it! */
-       if (old_value) {
-               size_t len = strlen(old_value);
-
-               if (len == 0) {
+       if (val) {
+               if (ZSTR_IS_INTERNED(val)) {
+                       RETVAL_INTERNED_STR(val);
+               } else if (ZSTR_LEN(val) == 0) {
                        RETVAL_EMPTY_STRING();
-               } else if (len == 1) {
-                       RETVAL_INTERNED_STR(ZSTR_CHAR((zend_uchar)old_value[0]));
+               } else if (ZSTR_LEN(val) == 1) {
+                       RETVAL_INTERNED_STR(ZSTR_CHAR((zend_uchar)ZSTR_VAL(val)[0]));
+               } else if (!(GC_FLAGS(val) & GC_PERSISTENT)) {
+                       ZVAL_NEW_STR(return_value, zend_string_copy(val));
                } else {
-                       RETVAL_STRINGL(old_value, len);
+                       ZVAL_NEW_STR(return_value, zend_string_init(ZSTR_VAL(val), ZSTR_LEN(val), 0));
                }
        } else {
                RETVAL_FALSE;