Smuuf
Smuuf

Reputation: 6534

What userland strings get automatically interned by PHP?

I was trying to get a better understanding of PHP's internal mechanisms of string interning - more specifically:

What are the rules PHP uses to determine whether (or not) the string created in userland will be interned...?

I see two possibilities:

  1. Only some strings are interned by PHP.
  2. PHP interns all strings.

For example - in Python (cpython) some, but not all strings are interned automatically (according to the Internet: http://guilload.com/python-string-interning/ or https://python-reference.readthedocs.io/en/latest/docs/functions/intern.html, to give some examples of sources I found).

I was trying to find some info about how PHP determines if the string from userland will be interned or not (I somehow expect that strings several megabytes in size are probably excluded from interning - but are they really...?) - but I couldn't find any.

I found this page http://www.phpinternalsbook.com/php7/internal_types/strings/zend_strings.html, but I don't see any mention about specific rules about automatic interning.

Upvotes: 4

Views: 397

Answers (1)

Sannis
Sannis

Reputation: 275

The documentation you mentioned have a part describing internal strings and the rules applied:

This process is in fact a little bit more complex than this. If you make use of an interned string out of a request processing, that string will be interned for sure. However, if you make use of an interned string as PHP is treating a request, then this string will only get interned for the current request, and will get cleared after that. All this is valid if you don’t use the opcache extension, something you shouldn’t do : use it.

When using the opcache extension, if you make use of an interned string out of a request processing, that string will be interned for sure and will also be shared to every PHP process or thread that will be spawned by you parallelism layer. Also, if you make use of an interned string as PHP is treating a request, this string will also get interned by opcache itself, and shared to every PHP process or thread that will be spawned by you parallelism layer.

Upvotes: 0

Related Questions