[RFC] Flip ns call scope lookup order #14529
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Inspired by #13632, the aim of this RFC is to improve the performance of global, internal function calls inside namespaced code without the need of prefixing all calls with
\
, or explicitlyuse
-ing them. If this is proposed, it will likely be aimed at 9.0, which some migration path in 8.4, if it makes it in time.Currently, a non-fq function call name is looked up in local scope, and then in global scope. For example, a call to
var_dump()
inside theApp
namespace will first look for\App\var_dump()
, and only then for\var_dump()
. It is a well-known optimization to prefix the call with\
to avoid the local lookup.However, for internal calls this goes a few steps further. Particularly:
strlen()
).All of these optimizations are limited to functions known at compile-time. Because we don't know if
var_dump()
refers to\App\var_dump()
or\var_dump()
, we cannot apply any of these optimizations.However, if we were to tweak the lookup to first check in global scope, and only then in local scope, we can assume that any function found in global scope is the function that will be executed at run-time. There are a couple of downsides:
Valgrind shows a 0.91% reduction in instructions for Symfony Demo. I'm not sure yet how this translates to real world performance.
Here's a small impact analysis: https://gist.github.com/iluuu1994/4b83481baac563f8f0d3204c697c5551 There are 484 namespaced functions shadowing global, internal functions in the top 1000 composer packages. However, most of these come from
thecodingmachine/safe
, whose entire purpose is offering safer wrappers around internal functions. Excluding these, there are only 20 such functions, which is surprisingly little.