-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Can't think of a better name for finalize
.
strpprintf functions also use smart strings. Especially when printing a small value as zend_string, it can be very noticeable. Can that one also be improved?
Yes, but this is not solved by a realloc. Allocations larger than 4 KiB are always on a page size multiple. |
Definitively not worth a long debate :-) that s a detail. |
I think so, but that can be done in a separate PR. :) |
I agree on this but worth doing nonetheless. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the PR!
I'm slightly concerned that the additional reallocation might decrease performance. It is okay, if we actually can save memory, but if not (see @bwoebi's comment), it might be better to avoid it.
At least if zend_alloc is used, and page_aligned(old_size) == page_aligned(new_size), nothing happens (if > 4 KiB). It's debatable whether it's worth short-circuiting this, as with big strings the relative overhead is minimal. |
Did some benchmarks to measure the impact on execution time: https://gist.github.com/arnaud-lb/ac223f3e7cf6e6d818adb77bbbd0a182 The first benchmark measures the execution time of var_export($string), with strings of various lengths.
There is no visible difference on other benchmarks (sub-1% difference, results vary in this range from multiple runs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't forget to add a note to UPGRADING.INTERNALS when merging. A PR for https://www.phpinternalsbook.com/php7/internal_types/strings/smart_str.html would also be nice.
… notes about this
smart_str uses an over-allocated string to optimize for append operations. Functions that use smart_str tend to return the over-allocated string directly. This results in unnecessary memory usage, especially for small strings.
See #8896
The overhead can be up to 231 bytes for strings smaller than that, and 4095 for other strings.
Here I change a few functions so that they trim the string before returning it.