phpinternalsbook / PHP-Internals-Book

PHP Internals Book
http://www.phpinternalsbook.com/
Other
1.39k stars 177 forks source link

smart_str: mention smart_str_extract() #127

Closed arnaud-lb closed 2 years ago

arnaud-lb commented 2 years ago

This mentions smart_str_extract() in the smart_str page.

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.

The overhead can be up to 231 bytes for strings smaller than that, and 4095 for other strings. This can be avoided for strings smaller than 4096 - zend_string header size - 1 by reallocating the string.

https://github.com/php/php-src/pull/8902 introduces smart_str_trim_to_size(), and calls it in smart_str_extract().

There are other benefits in using smart_str_extract() over accessing .s directly: