Skip to content

Commit a08723d

Browse files
committed
Use interned empty and "one char" strings.
1 parent b1a07d4 commit a08723d

File tree

8 files changed

+614
-566
lines changed

8 files changed

+614
-566
lines changed

Zend/zend_operators.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ ZEND_API void ZEND_FASTCALL _convert_to_string(zval *op ZEND_FILE_LINE_DC) /* {{
537537
break;
538538
}
539539
case IS_LONG: {
540-
ZVAL_NEW_STR(op, zend_long_to_str(Z_LVAL_P(op)));
540+
ZVAL_STR(op, zend_long_to_str(Z_LVAL_P(op)));
541541
break;
542542
}
543543
case IS_DOUBLE: {
@@ -2856,9 +2856,13 @@ ZEND_API void ZEND_FASTCALL zend_locale_sprintf_double(zval *op ZEND_FILE_LINE_D
28562856

28572857
ZEND_API zend_string* ZEND_FASTCALL zend_long_to_str(zend_long num) /* {{{ */
28582858
{
2859-
char buf[MAX_LENGTH_OF_LONG + 1];
2860-
char *res = zend_print_long_to_buf(buf + sizeof(buf) - 1, num);
2861-
return zend_string_init(res, buf + sizeof(buf) - 1 - res, 0);
2859+
if ((zend_ulong)num <= 9) {
2860+
return ZSTR_CHAR((zend_uchar)'0' + (zend_uchar)num);
2861+
} else {
2862+
char buf[MAX_LENGTH_OF_LONG + 1];
2863+
char *res = zend_print_long_to_buf(buf + sizeof(buf) - 1, num);
2864+
return zend_string_init(res, buf + sizeof(buf) - 1 - res, 0);
2865+
}
28622866
}
28632867
/* }}} */
28642868

ext/mysqlnd/mysqlnd_wireprotocol.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,6 +1766,10 @@ php_mysqlnd_rowp_read_text_protocol_aux(MYSQLND_MEMORY_POOL_CHUNK * row_buffer,
17661766
} else if (Z_TYPE_P(current_field) == IS_STRING) {
17671767
/* nothing to do here, as we want a string and ps_fetch_from_1_to_8_bytes() has given us one */
17681768
}
1769+
} else if (len == 0) {
1770+
ZVAL_EMPTY_STRING(current_field);
1771+
} else if (len == 1) {
1772+
ZVAL_INTERNED_STR(current_field, ZSTR_CHAR((zend_uchar)*(char *)p));
17691773
} else {
17701774
ZVAL_STRINGL(current_field, (char *)p, len);
17711775
}

ext/standard/array.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3807,19 +3807,19 @@ static inline void php_array_merge_or_replace_wrapper(INTERNAL_FUNCTION_PARAMETE
38073807
Z_PARAM_VARIADIC('+', args, argc)
38083808
ZEND_PARSE_PARAMETERS_END();
38093809

3810-
for (i = 0; i < argc; i++) {
3811-
zval *arg = args + i;
3812-
3813-
if (Z_TYPE_P(arg) != IS_ARRAY) {
3814-
php_error_docref(NULL, E_WARNING, "Argument #%d is not an array", i + 1);
3815-
RETURN_NULL();
3816-
}
3817-
}
3818-
38193810

38203811
if (replace) {
38213812
HashTable *dest;
38223813

3814+
for (i = 0; i < argc; i++) {
3815+
zval *arg = args + i;
3816+
3817+
if (Z_TYPE_P(arg) != IS_ARRAY) {
3818+
php_error_docref(NULL, E_WARNING, "Argument #%d is not an array", i + 1);
3819+
RETURN_NULL();
3820+
}
3821+
}
3822+
38233823
/* copy first array */
38243824
arg = args;
38253825
dest = zend_array_dup(Z_ARRVAL_P(arg));
@@ -3838,11 +3838,22 @@ static inline void php_array_merge_or_replace_wrapper(INTERNAL_FUNCTION_PARAMETE
38383838
} else {
38393839
zval *src_entry;
38403840
HashTable *src, *dest;
3841+
uint32_t count = 0;
3842+
3843+
for (i = 0; i < argc; i++) {
3844+
zval *arg = args + i;
3845+
3846+
if (Z_TYPE_P(arg) != IS_ARRAY) {
3847+
php_error_docref(NULL, E_WARNING, "Argument #%d is not an array", i + 1);
3848+
RETURN_NULL();
3849+
}
3850+
count += zend_hash_num_elements(Z_ARRVAL_P(arg));
3851+
}
38413852

38423853
arg = args;
38433854
src = Z_ARRVAL_P(arg);
38443855
/* copy first array */
3845-
array_init_size(return_value, zend_hash_num_elements(src));
3856+
array_init_size(return_value, count);
38463857
dest = Z_ARRVAL_P(return_value);
38473858
if (src->u.flags & HASH_FLAG_PACKED) {
38483859
zend_hash_real_init(dest, 1);

ext/standard/basic_functions.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5320,7 +5320,7 @@ PHP_FUNCTION(highlight_string)
53205320
PHP_FUNCTION(ini_get)
53215321
{
53225322
char *varname, *str;
5323-
size_t varname_len;
5323+
size_t varname_len, len;
53245324

53255325
ZEND_PARSE_PARAMETERS_START(1, 1)
53265326
Z_PARAM_STRING(varname, varname_len)
@@ -5332,7 +5332,13 @@ PHP_FUNCTION(ini_get)
53325332
RETURN_FALSE;
53335333
}
53345334

5335-
RETURN_STRING(str);
5335+
len = strlen(str);
5336+
if (len == 0) {
5337+
RETURN_EMPTY_STRING();
5338+
} else if (len == 1) {
5339+
RETURN_INTERNED_STR(ZSTR_CHAR((zend_uchar)str[0]));
5340+
}
5341+
RETURN_STRINGL(str, len);
53365342
}
53375343
/* }}} */
53385344

@@ -5443,7 +5449,15 @@ PHP_FUNCTION(ini_set)
54435449

54445450
/* copy to return here, because alter might free it! */
54455451
if (old_value) {
5446-
RETVAL_STRING(old_value);
5452+
size_t len = strlen(old_value);
5453+
5454+
if (len == 0) {
5455+
RETVAL_EMPTY_STRING();
5456+
} else if (len == 1) {
5457+
RETVAL_INTERNED_STR(ZSTR_CHAR((zend_uchar)old_value[0]));
5458+
} else {
5459+
RETVAL_STRINGL(old_value, len);
5460+
}
54475461
} else {
54485462
RETVAL_FALSE;
54495463
}

ext/standard/string.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,15 @@ PHPAPI void php_explode(const zend_string *delim, zend_string *str, zval *return
10941094
zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &tmp);
10951095
} else {
10961096
do {
1097-
ZVAL_STRINGL(&tmp, p1, p2 - p1);
1097+
size_t l = p2 - p1;
1098+
1099+
if (l == 0) {
1100+
ZVAL_EMPTY_STRING(&tmp);
1101+
} else if (l == 1) {
1102+
ZVAL_INTERNED_STR(&tmp, ZSTR_CHAR((zend_uchar)(*p1)));
1103+
} else {
1104+
ZVAL_STRINGL(&tmp, p1, p2 - p1);
1105+
}
10981106
zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &tmp);
10991107
p1 = p2 + ZSTR_LEN(delim);
11001108
p2 = (char *) php_memnstr(p1, ZSTR_VAL(delim), ZSTR_LEN(delim), endp);
@@ -2420,6 +2428,14 @@ PHP_FUNCTION(substr)
24202428
l = ZSTR_LEN(str) - f;
24212429
}
24222430

2431+
if (l == 0) {
2432+
RETURN_EMPTY_STRING();
2433+
} else if (l == 1) {
2434+
RETURN_INTERNED_STR(ZSTR_CHAR((zend_uchar)(ZSTR_VAL(str)[f])));
2435+
} else if (l == ZSTR_LEN(str)) {
2436+
RETURN_STR_COPY(str);
2437+
}
2438+
24232439
RETURN_STRINGL(ZSTR_VAL(str) + f, l);
24242440
}
24252441
/* }}} */
@@ -3227,7 +3243,14 @@ static zend_string *php_str_to_str_ex(zend_string *haystack,
32273243
nothing_todo:
32283244
return zend_string_copy(haystack);
32293245
} else {
3230-
new_str = zend_string_init(str, str_len, 0);
3246+
if (str_len == 0) {
3247+
new_str = ZSTR_EMPTY_ALLOC();
3248+
} else if (str_len == 1) {
3249+
new_str = ZSTR_CHAR((zend_uchar)(*str));
3250+
} else {
3251+
new_str = zend_string_init(str, str_len, 0);
3252+
}
3253+
32313254
(*replace_count)++;
32323255
return new_str;
32333256
}

0 commit comments

Comments
 (0)