Skip to content

Commit 88b255f

Browse files
committed
ZPP: Return zend_string directly from zend_parse_arg_str_weak()
This idea is based on @ndossche previous PR to reduce codebloat: #18436
1 parent b34d906 commit 88b255f

File tree

6 files changed

+44
-38
lines changed

6 files changed

+44
-38
lines changed

Zend/zend_API.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -728,58 +728,58 @@ ZEND_API bool ZEND_FASTCALL zend_parse_arg_number_or_str_slow(zval *arg, zval **
728728
return true;
729729
}
730730

731-
ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_weak(zval *arg, zend_string **dest, uint32_t arg_num) /* {{{ */
731+
ZEND_API zend_string* ZEND_FASTCALL zend_parse_arg_str_weak(zval *arg, uint32_t arg_num) /* {{{ */
732732
{
733733
if (EXPECTED(Z_TYPE_P(arg) < IS_STRING)) {
734734
if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("string", arg_num)) {
735-
return 0;
735+
return NULL;
736736
}
737737
convert_to_string(arg);
738-
*dest = Z_STR_P(arg);
738+
return Z_STR_P(arg);
739739
} else if (UNEXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
740740
zend_object *zobj = Z_OBJ_P(arg);
741741
zval obj;
742742
if (zobj->handlers->cast_object(zobj, &obj, IS_STRING) == SUCCESS) {
743743
OBJ_RELEASE(zobj);
744744
ZVAL_COPY_VALUE(arg, &obj);
745-
*dest = Z_STR_P(arg);
746-
return 1;
745+
return Z_STR_P(arg);
747746
}
748-
return 0;
747+
return NULL;
749748
} else {
750-
return 0;
749+
return NULL;
751750
}
752-
return 1;
753751
}
754752
/* }}} */
755753

756-
ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_slow(zval *arg, zend_string **dest, uint32_t arg_num) /* {{{ */
754+
ZEND_API zend_string* ZEND_FASTCALL zend_parse_arg_str_slow(zval *arg, uint32_t arg_num) /* {{{ */
757755
{
758756
if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
759-
return 0;
757+
return NULL;
760758
}
761-
return zend_parse_arg_str_weak(arg, dest, arg_num);
759+
return zend_parse_arg_str_weak(arg, arg_num);
762760
}
763761
/* }}} */
764762

765-
ZEND_API bool ZEND_FASTCALL zend_flf_parse_arg_str_slow(zval *arg, zend_string **dest, uint32_t arg_num)
763+
ZEND_API zend_string* ZEND_FASTCALL zend_flf_parse_arg_str_slow(zval *arg, uint32_t arg_num)
766764
{
767765
if (UNEXPECTED(ZEND_FLF_ARG_USES_STRICT_TYPES())) {
768-
return 0;
766+
return NULL;
769767
}
770-
return zend_parse_arg_str_weak(arg, dest, arg_num);
768+
return zend_parse_arg_str_weak(arg, arg_num);
771769
}
772770

773771
ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_or_long_slow(zval *arg, zend_string **dest_str, zend_long *dest_long, uint32_t arg_num) /* {{{ */
774772
{
773+
zend_string *str;
775774
if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
776775
return 0;
777776
}
778777
if (zend_parse_arg_long_weak(arg, dest_long, arg_num)) {
779778
*dest_str = NULL;
780779
return 1;
781-
} else if (zend_parse_arg_str_weak(arg, dest_str, arg_num)) {
780+
} else if ((str = zend_parse_arg_str_weak(arg, arg_num)) != NULL) {
782781
*dest_long = 0;
782+
*dest_str = str;
783783
return 1;
784784
} else {
785785
return 0;

Zend/zend_API.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2180,14 +2180,14 @@ ZEND_API bool ZEND_FASTCALL zend_parse_arg_long_slow(const zval *arg, zend_long
21802180
ZEND_API bool ZEND_FASTCALL zend_parse_arg_long_weak(const zval *arg, zend_long *dest, uint32_t arg_num);
21812181
ZEND_API bool ZEND_FASTCALL zend_parse_arg_double_slow(const zval *arg, double *dest, uint32_t arg_num);
21822182
ZEND_API bool ZEND_FASTCALL zend_parse_arg_double_weak(const zval *arg, double *dest, uint32_t arg_num);
2183-
ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_slow(zval *arg, zend_string **dest, uint32_t arg_num);
2184-
ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_weak(zval *arg, zend_string **dest, uint32_t arg_num);
2183+
ZEND_API zend_string* ZEND_FASTCALL zend_parse_arg_str_slow(zval *arg, uint32_t arg_num);
2184+
ZEND_API zend_string* ZEND_FASTCALL zend_parse_arg_str_weak(zval *arg, uint32_t arg_num);
21852185
ZEND_API bool ZEND_FASTCALL zend_parse_arg_number_slow(zval *arg, zval **dest, uint32_t arg_num);
21862186
ZEND_API bool ZEND_FASTCALL zend_parse_arg_number_or_str_slow(zval *arg, zval **dest, uint32_t arg_num);
21872187
ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_or_long_slow(zval *arg, zend_string **dest_str, zend_long *dest_long, uint32_t arg_num);
21882188

21892189
ZEND_API bool ZEND_FASTCALL zend_flf_parse_arg_bool_slow(const zval *arg, bool *dest, uint32_t arg_num);
2190-
ZEND_API bool ZEND_FASTCALL zend_flf_parse_arg_str_slow(zval *arg, zend_string **dest, uint32_t arg_num);
2190+
ZEND_API zend_string* ZEND_FASTCALL zend_flf_parse_arg_str_slow(zval *arg, uint32_t arg_num);
21912191
ZEND_API bool ZEND_FASTCALL zend_flf_parse_arg_long_slow(const zval *arg, zend_long *dest, uint32_t arg_num);
21922192

21932193
static zend_always_inline bool zend_parse_arg_bool_ex(const zval *arg, bool *dest, bool *is_null, bool check_null, uint32_t arg_num, bool frameless)
@@ -2289,10 +2289,16 @@ static zend_always_inline bool zend_parse_arg_str_ex(zval *arg, zend_string **de
22892289
} else if (check_null && Z_TYPE_P(arg) == IS_NULL) {
22902290
*dest = NULL;
22912291
} else {
2292+
zend_string *str;
22922293
if (frameless) {
2293-
return zend_flf_parse_arg_str_slow(arg, dest, arg_num);
2294+
str = zend_flf_parse_arg_str_slow(arg, arg_num);
22942295
} else {
2295-
return zend_parse_arg_str_slow(arg, dest, arg_num);
2296+
str = zend_parse_arg_str_slow(arg, arg_num);
2297+
}
2298+
if (str) {
2299+
*dest = str;
2300+
} else {
2301+
return 0;
22962302
}
22972303
}
22982304
return 1;
@@ -2526,7 +2532,8 @@ static zend_always_inline bool zend_parse_arg_array_ht_or_str(
25262532
*dest_str = NULL;
25272533
} else {
25282534
*dest_ht = NULL;
2529-
return zend_parse_arg_str_slow(arg, dest_str, arg_num);
2535+
*dest_str = zend_parse_arg_str_slow(arg, arg_num);
2536+
return *dest_str != NULL;
25302537
}
25312538
return 1;
25322539
}

Zend/zend_execute.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,6 @@ static bool zend_verify_weak_scalar_type_hint(uint32_t type_mask, zval *arg)
733733
{
734734
zend_long lval;
735735
double dval;
736-
zend_string *str;
737736
bool bval;
738737

739738
/* Type preference order: int -> float -> string -> bool */
@@ -765,7 +764,7 @@ static bool zend_verify_weak_scalar_type_hint(uint32_t type_mask, zval *arg)
765764
ZVAL_DOUBLE(arg, dval);
766765
return true;
767766
}
768-
if ((type_mask & MAY_BE_STRING) && zend_parse_arg_str_weak(arg, &str, 0)) {
767+
if ((type_mask & MAY_BE_STRING) && zend_parse_arg_str_weak(arg, 0)) {
769768
/* on success "arg" is converted to IS_STRING */
770769
return true;
771770
}

Zend/zend_frameless_function.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
dest_ht = NULL; \
6565
ZVAL_COPY(&str_tmp, arg ## arg_num); \
6666
arg ## arg_num = &str_tmp; \
67-
if (!zend_flf_parse_arg_str_slow(arg ## arg_num, &dest_str, arg_num)) { \
67+
if (!(dest_str = zend_flf_parse_arg_str_slow(arg ## arg_num, arg_num))) { \
6868
zend_wrong_parameter_type_error(arg_num, Z_EXPECTED_ARRAY_OR_STRING, arg ## arg_num); \
6969
goto flf_clean; \
7070
} \

Zend/zend_vm_def.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8823,8 +8823,8 @@ ZEND_VM_COLD_CONST_HANDLER(121, ZEND_STRLEN, CONST|TMP|CV, ANY)
88238823
strict = EX_USES_STRICT_TYPES();
88248824
do {
88258825
if (EXPECTED(!strict)) {
8826-
zend_string *str;
88278826
zval tmp;
8827+
zend_string *str;
88288828

88298829
if (UNEXPECTED(Z_TYPE_P(value) == IS_NULL)) {
88308830
zend_error(E_DEPRECATED,
@@ -8837,7 +8837,7 @@ ZEND_VM_COLD_CONST_HANDLER(121, ZEND_STRLEN, CONST|TMP|CV, ANY)
88378837
}
88388838

88398839
ZVAL_COPY(&tmp, value);
8840-
if (zend_parse_arg_str_weak(&tmp, &str, 1)) {
8840+
if ((str = zend_parse_arg_str_weak(&tmp, 1)) != NULL) {
88418841
ZVAL_LONG(EX_VAR(opline->result.var), ZSTR_LEN(str));
88428842
zval_ptr_dtor(&tmp);
88438843
break;

Zend/zend_vm_execute.h

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)