Skip to content

smart_str: mention smart_str_extract() #127

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 3 commits into from
Sep 2, 2022

Conversation

arnaud-lb
Copy link
Contributor

@arnaud-lb arnaud-lb commented Jul 8, 2022

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.

php/php-src#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:

  • It will return an empty string if .s is null
  • It takes car of the trailing NUL byte
  • It resets .s to NULL

Copy link
Collaborator

@cmb69 cmb69 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Looks good to me.

@cmb69 cmb69 merged commit de00e63 into phpinternalsbook:master Sep 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants