Skip to content

Commit a92dedc

Browse files
committed
RC1 optimisation for ucfirst and lcfirst
1 parent 4749e5b commit a92dedc

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

Zend/zend_types.h

+5
Original file line numberDiff line numberDiff line change
@@ -1512,4 +1512,9 @@ static zend_always_inline bool zend_may_modify_arg_in_place(const zval *arg)
15121512
return Z_REFCOUNTED_P(arg) && !(GC_FLAGS(Z_COUNTED_P(arg)) & (GC_IMMUTABLE | GC_PERSISTENT)) && Z_REFCOUNT_P(arg) == 1;
15131513
}
15141514

1515+
static zend_always_inline bool zend_may_modify_string_in_place(const zend_string *arg)
1516+
{
1517+
return !(GC_FLAGS(arg) & (GC_IMMUTABLE | GC_PERSISTENT)) && GC_REFCOUNT(arg) == 1;
1518+
}
1519+
15151520
#endif /* ZEND_TYPES_H */

ext/standard/string.c

+14-2
Original file line numberDiff line numberDiff line change
@@ -2341,7 +2341,13 @@ static zend_string* php_ucfirst(zend_string *str)
23412341
if (r == ch) {
23422342
return zend_string_copy(str);
23432343
} else {
2344-
zend_string *s = zend_string_init(ZSTR_VAL(str), ZSTR_LEN(str), 0);
2344+
zend_string *s;
2345+
if (zend_may_modify_string_in_place(str)) {
2346+
s = str;
2347+
zend_string_forget_hash_val(s);
2348+
} else {
2349+
s = zend_string_init(ZSTR_VAL(str), ZSTR_LEN(str), false);
2350+
}
23452351
ZSTR_VAL(s)[0] = r;
23462352
return s;
23472353
}
@@ -2373,7 +2379,13 @@ static zend_string* php_lcfirst(zend_string *str)
23732379
if (r == ZSTR_VAL(str)[0]) {
23742380
return zend_string_copy(str);
23752381
} else {
2376-
zend_string *s = zend_string_init(ZSTR_VAL(str), ZSTR_LEN(str), 0);
2382+
zend_string *s;
2383+
if (zend_may_modify_string_in_place(str)) {
2384+
s = str;
2385+
zend_string_forget_hash_val(s);
2386+
} else {
2387+
s = zend_string_init(ZSTR_VAL(str), ZSTR_LEN(str), false);
2388+
}
23772389
ZSTR_VAL(s)[0] = r;
23782390
return s;
23792391
}

0 commit comments

Comments
 (0)