Skip to content

Conversation

arnaud-lb
Copy link
Owner

@arnaud-lb arnaud-lb commented Sep 8, 2025

PFAs are created by generating the AST of a Closure at runtime and compiling it. The compiled op_array can be cached similarly to linked classes.

About caching

There are two levels of caching:

  • The global opcache (ZCG(hash))
  • The caller's run time cache

The cache key in ZCG(hash) is composed of the address of the declaring opline, the address of the function being called, and a prefix.

This scheme is not ideal because:

  • The key used in ZCG(hash) may be a valid file name
  • This takes a slot in ZCG(hash)
  • When opcache is not enabled, only the run time cache is used. This cache stores only one entry, so cache misses will occur when the function being called changes.

A better solution might be to cache PFA op_arrays in the declaring op_array (similarly, the inheritance cache is stored in classes). This cache could store multiple PFAs, and could be used regardless of opcache being enabled.

Alternatively, use a global two-level map: First level is indexed by the "signature" of the PFA, second level by the function being called. This would lead to a better cache hit rate when the same function is partialled in multiple locations.

TODO

  • Improve PFA caching when opcache is not enabled, and fix cases that are broken when opcache is not enabled
  • Reorganize tests
  • PFAs in constant expressions
  • bindTo() is a no-op for PFA of closures

@arnaud-lb arnaud-lb force-pushed the partials-v2-gen branch 2 times, most recently from eaf9c94 to 7062e66 Compare September 13, 2025 10:35
Co-authored-by: Joe Watkins <[email protected]>
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.

2 participants