Skip to content

Reduce memory allocated by var_export, json_encode, serialize, and other #8902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ PHP NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? ????, PHP 8.2.0beta1

- Core:
. Reduced the memory footprint of strings returned by var_export(),
json_encode(), serialize(), iconv_*(), mb_ereg*(), session_create_id(),
http_build_query(), strstr(), Reflection*::__toString(). (Arnaud)

- CLI:
- Updated the mime-type table for the builtin-server. (Ayesh Karunaratne)
. Updated the mime-type table for the builtin-server. (Ayesh Karunaratne)

- FPM:
- Added listen.setfib pool option to set route FIB on FreeBSD. (David Carlier)
. Added listen.setfib pool option to set route FIB on FreeBSD. (David Carlier)

07 Jul 2022, PHP 8.2.0alpha3

Expand Down
7 changes: 7 additions & 0 deletions UPGRADING.INTERNALS
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ PHP 8.2 INTERNALS UPGRADE NOTES
- zend_object_do_operation_t
* Added a new zero_position argument to php_stream_fopen_from_fd_rel to reflect
if this a newly created file so the current file offset needs not to be checked.
* Added smart_str_trim_to_size(). The function trims the memory allocated for the
string. This can considerably reduce the memory footprint of strings smaller
than approximately 4096 bytes.
* smart_str_extract() and the spprintf family of functions now use
smart_str_trim_to_size() before returning the string.
* It is recommended to use smart_str_extract() or smart_str_trim_to_size() when
using the smart_str API.

========================
2. Build system changes
Expand Down
3 changes: 1 addition & 2 deletions Zend/zend.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,7 @@ ZEND_API zend_string *zend_vstrpprintf(size_t max_len, const char *format, va_li
ZSTR_LEN(buf.s) = max_len;
}

smart_str_0(&buf);
return buf.s;
return smart_str_extract(&buf);
}
/* }}} */

Expand Down
15 changes: 14 additions & 1 deletion Zend/zend_smart_str.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
smart_str_appendl_ex((dest), (src), strlen(src), (what))
#define smart_str_appends(dest, src) \
smart_str_appendl((dest), (src), strlen(src))
#define smart_str_extract(dest) \
smart_str_extract_ex((dest), 0)
#define smart_str_trim_to_size(dest) \
smart_str_trim_to_size_ex((dest), 0)
#define smart_str_extend(dest, len) \
smart_str_extend_ex((dest), (len), 0)
#define smart_str_appendc(dest, c) \
Expand Down Expand Up @@ -101,10 +105,19 @@ static zend_always_inline size_t smart_str_get_len(smart_str *str) {
return str->s ? ZSTR_LEN(str->s) : 0;
}

static zend_always_inline zend_string *smart_str_extract(smart_str *str) {
static zend_always_inline void smart_str_trim_to_size_ex(smart_str *str, bool persistent)
{
if (str->s && str->a > ZSTR_LEN(str->s)) {
str->s = zend_string_realloc(str->s, ZSTR_LEN(str->s), persistent);
str->a = ZSTR_LEN(str->s);
}
}

static zend_always_inline zend_string *smart_str_extract_ex(smart_str *str, bool persistent) {
if (str->s) {
zend_string *res;
smart_str_0(str);
smart_str_trim_to_size_ex(str, persistent);
res = str->s;
str->s = NULL;
return res;
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_smart_str_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define ZEND_SMART_STR_PUBLIC_H

typedef struct {
/** See smart_str_extract() */
zend_string *s;
size_t a;
} smart_str;
Expand Down
3 changes: 1 addition & 2 deletions ext/dom/documenttype.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ int dom_documenttype_internal_subset_read(dom_object *obj, zval *retval)
}

if (ret_buf.s) {
smart_str_0(&ret_buf);
ZVAL_NEW_STR(retval, ret_buf.s);
ZVAL_STR(retval, smart_str_extract(&ret_buf));
return SUCCESS;
}
}
Expand Down
3 changes: 1 addition & 2 deletions ext/filter/sanitizing_filters.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ static void php_filter_encode_html(zval *value, const unsigned char *chars)
s++;
}

smart_str_0(&str);
zval_ptr_dtor(value);
ZVAL_NEW_STR(value, str.s);
ZVAL_STR(value, smart_str_extract(&str));
}

static const unsigned char hexchars[] = "0123456789ABCDEF";
Expand Down
14 changes: 3 additions & 11 deletions ext/iconv/iconv.c
Original file line number Diff line number Diff line change
Expand Up @@ -1845,7 +1845,7 @@ PHP_FUNCTION(iconv_substr)
_php_iconv_show_error(err, GENERIC_SUPERSET_NAME, charset);

if (err == PHP_ICONV_ERR_SUCCESS && retval.s != NULL) {
RETURN_NEW_STR(retval.s);
RETURN_STR(smart_str_extract(&retval));
}
smart_str_free(&retval);
RETURN_FALSE;
Expand Down Expand Up @@ -2038,11 +2038,7 @@ PHP_FUNCTION(iconv_mime_encode)
_php_iconv_show_error(err, out_charset, in_charset);

if (err == PHP_ICONV_ERR_SUCCESS) {
if (retval.s != NULL) {
RETVAL_STR(retval.s);
} else {
RETVAL_EMPTY_STRING();
}
RETVAL_STR(smart_str_extract(&retval));
} else {
smart_str_free(&retval);
RETVAL_FALSE;
Expand Down Expand Up @@ -2083,11 +2079,7 @@ PHP_FUNCTION(iconv_mime_decode)
_php_iconv_show_error(err, charset, "???");

if (err == PHP_ICONV_ERR_SUCCESS) {
if (retval.s != NULL) {
RETVAL_STR(retval.s);
} else {
RETVAL_EMPTY_STRING();
}
RETVAL_STR(smart_str_extract(&retval));
} else {
smart_str_free(&retval);
RETVAL_FALSE;
Expand Down
6 changes: 1 addition & 5 deletions ext/json/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,7 @@ PHP_FUNCTION(json_encode)
}
}

smart_str_0(&buf); /* copy? */
if (buf.s) {
RETURN_NEW_STR(buf.s);
}
RETURN_EMPTY_STRING();
RETURN_STR(smart_str_extract(&buf));
}
/* }}} */

Expand Down
9 changes: 3 additions & 6 deletions ext/mbstring/php_mbregex.c
Original file line number Diff line number Diff line change
Expand Up @@ -1144,13 +1144,10 @@ static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOp

if (err <= -2) {
smart_str_free(&out_buf);
RETVAL_FALSE;
} else if (out_buf.s) {
smart_str_0(&out_buf);
RETVAL_STR(out_buf.s);
} else {
RETVAL_EMPTY_STRING();
RETURN_FALSE;
}

RETURN_STR(smart_str_extract(&out_buf));
}
/* }}} */

Expand Down
5 changes: 2 additions & 3 deletions ext/session/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -1464,7 +1464,7 @@ PHPAPI zend_result php_session_reset_id(void) /* {{{ */
smart_str_0(&var);
if (sid) {
zval_ptr_dtor_str(sid);
ZVAL_NEW_STR(sid, var.s);
ZVAL_STR(sid, smart_str_extract(&var));
} else {
REGISTER_STRINGL_CONSTANT("SID", ZSTR_VAL(var.s), ZSTR_LEN(var.s), 0);
smart_str_free(&var);
Expand Down Expand Up @@ -2362,8 +2362,7 @@ PHP_FUNCTION(session_create_id)
php_error_docref(NULL, E_WARNING, "Failed to create new ID");
RETURN_FALSE;
}
smart_str_0(&id);
RETVAL_NEW_STR(id.s);
RETVAL_STR(smart_str_extract(&id));
}
/* }}} */

Expand Down
6 changes: 2 additions & 4 deletions ext/soap/php_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3261,8 +3261,7 @@ sdlPtr get_sdl(zval *this_ptr, char *uri, zend_long cache_wsdl)
smart_str_appends(&proxy,Z_STRVAL_P(proxy_host));
smart_str_appends(&proxy,":");
smart_str_append_long(&proxy,Z_LVAL_P(proxy_port));
smart_str_0(&proxy);
ZVAL_NEW_STR(&str_proxy, proxy.s);
ZVAL_STR(&str_proxy, smart_str_extract(&proxy));

if (!context) {
context = php_stream_context_alloc();
Expand Down Expand Up @@ -3304,8 +3303,7 @@ sdlPtr get_sdl(zval *this_ptr, char *uri, zend_long cache_wsdl)
http_context_headers(context, has_authorization, has_proxy_authorization, 0, &headers);
}

smart_str_0(&headers);
ZVAL_NEW_STR(&str_headers, headers.s);
ZVAL_STR(&str_headers, smart_str_extract(&headers));
php_stream_context_set_option(context, "http", "header", &str_headers);
zval_ptr_dtor(&str_headers);
}
Expand Down
2 changes: 1 addition & 1 deletion ext/spl/spl_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -1554,7 +1554,7 @@ PHP_METHOD(ArrayObject, serialize)
/* done */
PHP_VAR_SERIALIZE_DESTROY(var_hash);

RETURN_NEW_STR(buf.s);
RETURN_STR(smart_str_extract(&buf));
} /* }}} */

/* {{{ unserialize the object */
Expand Down
4 changes: 1 addition & 3 deletions ext/spl/spl_dllist.c
Original file line number Diff line number Diff line change
Expand Up @@ -1032,12 +1032,10 @@ PHP_METHOD(SplDoublyLinkedList, serialize)
current = next;
}

smart_str_0(&buf);

/* done */
PHP_VAR_SERIALIZE_DESTROY(var_hash);

RETURN_NEW_STR(buf.s);
RETURN_STR(smart_str_extract(&buf));
} /* }}} */

/* {{{ Unserializes storage */
Expand Down
2 changes: 1 addition & 1 deletion ext/spl/spl_observer.c
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ PHP_METHOD(SplObjectStorage, serialize)
/* done */
PHP_VAR_SERIALIZE_DESTROY(var_hash);

RETURN_NEW_STR(buf.s);
RETURN_STR(smart_str_extract(&buf));
} /* }}} */

/* {{{ Unserializes storage */
Expand Down
8 changes: 1 addition & 7 deletions ext/standard/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,6 @@ PHP_FUNCTION(http_build_query)

php_url_encode_hash_ex(HASH_OF(formdata), &formstr, prefix, prefix_len, NULL, 0, NULL, 0, (Z_TYPE_P(formdata) == IS_OBJECT ? formdata : NULL), arg_sep, (int)enc_type);

if (!formstr.s) {
RETURN_EMPTY_STRING();
}

smart_str_0(&formstr);

RETURN_NEW_STR(formstr.s);
RETURN_STR(smart_str_extract(&formstr));
}
/* }}} */
3 changes: 1 addition & 2 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -2891,8 +2891,7 @@ static void php_strtr_array(zval *return_value, zend_string *input, HashTable *p

if (result.s) {
smart_str_appendl(&result, str + old_pos, slen - old_pos);
smart_str_0(&result);
RETVAL_NEW_STR(result.s);
RETVAL_STR(smart_str_extract(&result));
} else {
smart_str_free(&result);
RETVAL_STR_COPY(input);
Expand Down
8 changes: 2 additions & 6 deletions ext/standard/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ PHP_FUNCTION(var_export)
smart_str_0 (&buf);

if (return_output) {
RETURN_NEW_STR(buf.s);
RETURN_STR(smart_str_extract(&buf));
} else {
PHPWRITE(ZSTR_VAL(buf.s), ZSTR_LEN(buf.s));
smart_str_free(&buf);
Expand Down Expand Up @@ -1318,11 +1318,7 @@ PHP_FUNCTION(serialize)
RETURN_THROWS();
}

if (buf.s) {
RETURN_NEW_STR(buf.s);
} else {
RETURN_EMPTY_STRING();
}
RETURN_STR(smart_str_extract(&buf));
}
/* }}} */

Expand Down