Skip to content

Is there a reason OPcache can't be statically compiled? #16887

@dktapps

Description

@dktapps

Description

I maintain a threading extension ext-pmmpthread.

One of the biggest maintenance issues is the need to copy classes, functions & constants from thread A to thread B when a new thread is started.

If OPcache is enabled, this becomes vastly easier & faster, since the extension doesn't need to copy classes & op_arrays with ZEND_ACC_IMMUTABLE at all. Constants are still an issue right now because OPcache doesn't persist those as far as I'm aware.

However, we still have to account for the case when OPcache is not used (e.g. on platforms where dynamic loading isn't available since OPcache can't be statically compiled, or disabled due to bugs). This means that:

  • A large amount of code is needed to copy the classes, functions & constants in question. This makes up a significant chunk of the extension's code & is also the source of most of the bugs. Many parts of it are adapted from zend_persist.c which does a pretty similar thing.
  • Maintenance on the extension is needed on basically every PHP version to copy-paste & adapt bits of code from zend_persist.c to fix various new issues that come up. Often these issues are not detectable during initial testing.
  • Some things can't be copied in certain situations, such as object constants. This is because the AST for these constants is deleted when the constant is evaluated if OPcache is not used. It's easy to copy AST but difficult to copy objects (particularly internal objects). This leads to weird limitations like threadable code not being able to use certain language features, or needing hacks like serialize (a la ext-pthreads) which don't always work either.
  • Threads take a big performance hit if they need to copy a lot of classes from thread A to thread B.

For completeness, I'll mention that I have considered implementing copying at the point of loading a script (exactly like OPcache does) to avoid some of these issues. However this would involve basically identical logic to zend_persist.c being copied to the extension, which I don't feel makes a lot of sense.

For PHP's own sake it doesn't make a ton of sense to me to have two different modes of class & function existence either. I feel like it would simplify a bunch of things for everyone if the OPcache way was the default.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions