Skip to content

Commit 0b036b5

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

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-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

+16-2
Original file line numberDiff line numberDiff line change
@@ -2341,7 +2341,14 @@ 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+
GC_ADDREF(s);
2349+
} else {
2350+
s = zend_string_init(ZSTR_VAL(str), ZSTR_LEN(str), false);
2351+
}
23452352
ZSTR_VAL(s)[0] = r;
23462353
return s;
23472354
}
@@ -2373,7 +2380,14 @@ static zend_string* php_lcfirst(zend_string *str)
23732380
if (r == ZSTR_VAL(str)[0]) {
23742381
return zend_string_copy(str);
23752382
} else {
2376-
zend_string *s = zend_string_init(ZSTR_VAL(str), ZSTR_LEN(str), 0);
2383+
zend_string *s;
2384+
if (zend_may_modify_string_in_place(str)) {
2385+
s = str;
2386+
zend_string_forget_hash_val(s);
2387+
GC_ADDREF(s);
2388+
} else {
2389+
s = zend_string_init(ZSTR_VAL(str), ZSTR_LEN(str), false);
2390+
}
23772391
ZSTR_VAL(s)[0] = r;
23782392
return s;
23792393
}

0 commit comments

Comments
 (0)