On Fri, 2024-08-02 at 18:51 +0200, Ilija Tovilo wrote:
> Hi everyone
>
> As you probably know, a common performance optimization in PHP is to
> prefix global function calls in namespaced code with a \
. In
> namespaced code, relative function calls (meaning, not prefixed with
> \
, not imported and not containing multiple namespace components)
> will be looked up in the current namespace before falling back to the
> global namespace. Prefixing the function name with \
disambiguates
> the called function by always picking the global function.
>
> Not knowing exactly which function is called at compile time has a
> couple of downsides to this:
>
> * It leads to the aforementioned double-lookup.
> * It prevents compile-time-evaluation of pure internal functions.
> * It prevents compiling to specialized opcodes for specialized
> internal functions (e.g. strlen()).
> * It requires branching for frameless functions [1].
> * It prevents an optimization that looks up internal functions by
> offset rather than by name [2].
> * It prevents compiling to more specialized argument sending opcodes
> because of unknown by-value/by-reference passing.
>
> All of these are enabled by disambiguating the call. Unfortunately,
> prefixing all calls with \
, or adding a use function
at the top
> of
> every file is annoying and noisy. We recently got a feature request
> to
> change how functions are looked up [3].
I think there should be some way to use globals first at compile time.
I had suggested a per-file directive in a post to this list a while
back. Something like:
namespace foo;
use global functions;
class MyClass {
// do stuff.
}
Where use global functions
would be a special token that the compiler
uses to skip the ns lookup and use dedicated opcodes when available.