# High performance function autoloading

- Thread: https://externals.io/message/105757
- Original archive: https://news-web.php.net/php.internals/105757
- Started by Theodore Brown on 2019-05-20T17:17:49+00:00
- Messages: 12
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #105757 — Theodore Brown — 2019-05-20T17:17:49+00:00

URL: https://externals.io/message/105757#105757

Every time function autoloading been brought up in the past, there
have been concerns about performance issues when calling a global
function from inside a namespace. E.g. calling `strlen` in a loop
would become far slower if every call has to trigger the autoloader.

But isn't it extremely rare to override global functions in a
namespace? And even if they are overridden, how common is it to call
an overridden function in the same namespace that exists in a
different file?

Would it be possible to handle this rare case with a per-file option
like `declare(root_fallback=0);`?

Consider the following code examples:

# example_1.php
```php
namespace Foo;

echo strlen('test');
// PHP would first check if Foo\strlen exists. If not, it would check
// whether \strlen exists. Since it does, it would call the built-in
// function as expected without triggering the autoloader.
```

# example_2.php
```php
namespace Foo;

function strlen(string $str) {...}

echo strlen('test');
// PHP would first check if Foo\strlen exists. Since it is defined in
// the same file, it would be called without falling back to the
// global function or triggering the autoloader.
```

# example_3.php
```php
namespace Foo;

use function Bar\strlen;

echo strlen('test');
// PHP would first check if Bar\strlen exists. If not, it would
// trigger the autoloader rather than falling back to the global
// function. The same thing would occur for any qualified function
// call. E.g. without "use function" the following could be written:
echo \Bar\strlen('test');
```

# example_4.php
```php
namespace Foo;

echo my_function('test');
// PHP would first check if Foo\my_function exists. If not, it would
// check whether \my_function exists. If not, the autoloader would be
// triggered.
```

# example_5.php
```php
declare(root_fallback=0);

namespace Foo;

echo strlen('test');
// PHP would first check if Foo\strlen exists. If not, it would
// trigger the autoloader rather than falling back to the global
// function, since root fallback is disabled in the file.
```

What are your thoughts about this approach?

Personally I would rarely have to disable root fallback, since I like
to group functions in a single file per namespace. But it seems like
this would be a good way to enable function autoloading that works
with namespaces spread across multiple files and doesn't slow down
performance.

---

## #105758 — Marco Pivetta — 2019-05-20T17:56:05+00:00

URL: https://externals.io/message/105757#105758

Hey Theodore,

On Mon, May 20, 2019 at 7:17 PM Theodore Brown <theodorejb@outlook.com>
wrote:

> Every time function autoloading been brought up in the past, there
> have been concerns about performance issues when calling a global
> function from inside a namespace. E.g. calling `strlen` in a loop
> would become far slower if every call has to trigger the autoloader.
>
> But isn't it extremely rare to override global functions in a
> namespace? And even if they are overridden, how common is it to call
> an overridden function in the same namespace that exists in a
> different file?
>
> Would it be possible to handle this rare case with a per-file option
> like `declare(root_fallback=0);`?
>
> Consider the following code examples:
>
> # example_1.php
> ```php
> namespace Foo;
>
> echo strlen('test');
> // PHP would first check if Foo\strlen exists. If not, it would check
> // whether \strlen exists. Since it does, it would call the built-in
> // function as expected without triggering the autoloader.
> ```
>
> # example_2.php
> ```php
> namespace Foo;
>
> function strlen(string $str) {...}
>
> echo strlen('test');
> // PHP would first check if Foo\strlen exists. Since it is defined in
> // the same file, it would be called without falling back to the
> // global function or triggering the autoloader.
> ```
>
> # example_3.php
> ```php
> namespace Foo;
>
> use function Bar\strlen;
>
> echo strlen('test');
> // PHP would first check if Bar\strlen exists. If not, it would
> // trigger the autoloader rather than falling back to the global
> // function. The same thing would occur for any qualified function
> // call. E.g. without "use function" the following could be written:
> echo \Bar\strlen('test');
> ```
>
> # example_4.php
> ```php
> namespace Foo;
>
> echo my_function('test');
> // PHP would first check if Foo\my_function exists. If not, it would
> // check whether \my_function exists. If not, the autoloader would be
> // triggered.
> ```
>
> # example_5.php
> ```php
> declare(root_fallback=0);
>
> namespace Foo;
>
> echo strlen('test');
> // PHP would first check if Foo\strlen exists. If not, it would
> // trigger the autoloader rather than falling back to the global
> // function, since root fallback is disabled in the file.
> ```
>
> What are your thoughts about this approach?
>
> Personally I would rarely have to disable root fallback, since I like
> to group functions in a single file per namespace. But it seems like
> this would be a good way to enable function autoloading that works
> with namespaces spread across multiple files and doesn't slow down
> performance.
>

I do like the approach, as it makes the (better) fallback-less function
usage opt-in, which is currently only fixed by coding-standards tooling.

To be clear, would this lead to a "Fatal error: Uncaught Error: Call to
undefined function" in case of autoloader failure?

Greets,

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

---

## #105759 — Gabriel O — 2019-05-20T18:00:59+00:00

URL: https://externals.io/message/105757#105759

On 20 May 2019 7:17:58 PM Theodore Brown <theodorejb@outlook.com> wrote:

> Every time function autoloading been brought up in the past, there
> have been concerns about performance issues when calling a global
> function from inside a namespace. E.g. calling `strlen` in a loop
> would become far slower if every call has to trigger the autoloader.

This trick for perf improvement is overblown. It's misconception that it 
does provide speed advantage for most functions and reasons behind it. It 
does so only for those implemented as opcodes. People started to abuse it 
by importing ALL functions. Such overzealous approach completely prevents 
useful things like function mocking.

---

## #105760 — Theodore Brown — 2019-05-20T18:42:55+00:00

URL: https://externals.io/message/105757#105760

> > […]
> 
> This trick for perf improvement is overblown. It's misconception that it
> does provide speed advantage for most functions and reasons behind it. It
> does so only for those implemented as opcodes. People started to abuse it
> by importing ALL functions. Such overzealous approach completely prevents
> useful things like function mocking.

Can you clarify? I'm not sure what performance improvement trick you
are referring to, or what you mean by "importing all functions". Are
you saying it's a misconception that triggering an autoloader on
unqualified function calls in a namespace will slow down performance?
As I understand it, this is the roadblock for function autoloading.

Best regards,
Theodore

---

## #105761 — Marco Pivetta — 2019-05-20T18:45:16+00:00

URL: https://externals.io/message/105757#105761

>
> On 20 May 2019 7:17:58 PM Theodore Brown <theodorejb@outlook.com> wrote:
>
> > […]
>
> This trick for perf improvement is overblown. It's misconception that it
> does provide speed advantage for most functions and reasons behind it. It
> does so only for those implemented as opcodes. People started to abuse it
> by importing ALL functions. Such overzealous approach completely prevents
> useful things like function mocking.
>

Disabling function mocking is good ©️

It was a terrible practice in first place, and it is usually done for
impure functions that should be wrapped in integration-tested adapters.

---

## #105762 — M. W. Moe — 2019-05-20T19:01:14+00:00

URL: https://externals.io/message/105757#105762

Hello,

"Disabling function mocking is good", in my life I read many idioties; this
one gets the "Palme d'Or";
This majesty; crowned Emperor.



> 
>
> > […]
>
> Disabling function mocking is good ©️
>
> It was a terrible practice in first place, and it is usually done for
> impure functions that should be wrapped in integration-tested adapters.

---

## #105763 — Sebastian Bergmann — 2019-05-20T19:06:10+00:00

URL: https://externals.io/message/105757#105763

> Disabling function mocking is good ©️
> 
> It was a terrible practice in first place, and it is usually done for
> impure functions that should be wrapped in integration-tested adapters.

Hear, hear :)

---

## #105764 — Theodore Brown — 2019-05-20T19:11:18+00:00

URL: https://externals.io/message/105757#105764

> I do like the approach, as it makes the (better) fallback-less function
> usage opt-in, which is currently only fixed by coding-standards tooling.

> To be clear, would this lead to a "Fatal error: Uncaught Error: Call
> to undefined function" in case of autoloader failure?

Yes, that would be my expectation. Though I didn't mention it in my first
email, the same approach should work for autoloading constants as well as
functions.

If others are in favor, I'd be happy to collaborate on writing an RFC.
I'd need someone more experienced with PHP's internals to help with the
implementation, though.

Regards,
Theodore

---

## #105765 — Gabriel O — 2019-05-20T19:36:33+00:00

URL: https://externals.io/message/105757#105765

> 
> Can you clarify? I'm not sure what performance improvement trick you
> are referring to, or what you mean by "importing all functions". Are
> you saying it's a misconception that triggering an autoloader on
> unqualified function calls in a namespace will slow down performance?
> As I understand it, this is the roadblock for function autoloading.
> 
> Best regards,
> Theodore

Yes, this is misconception. Namespace resolutions are cached. First fixer tool bringing support for this was correct: PHP-CS-Fixer auto imports only optimized functions for this very same reason.


> 
> 
> Disabling function mocking is good ©️
> 
> It was a terrible practice in first place, and it is usually done for
> impure functions that should be wrapped in integration-tested adapters.


…which dramatically affects design of code. Nobody builds injectable replacements for these calls. As always, you are speaking from POV of library maintainer. As a user of your library, you don’t have perfect design. Nobody has. 

As a user of the library, I want to have ability to replace every aspect of it without copying it, while preserving simplicity - this is impossible to do, because these goals are in opposition. 

As a contributor of the library, I have found it impossible to write tests for some parts of very same library, because its code style guide dictates importing all the functions which makes it impossible to mock function call. And replacing them with injectable adapters was deemed overkill (and is subject of BC concerns).

Anyways, it doesn’t matter. Disabling function fallback for all functions has very little benefit like I mentioned.

---

## #105766 — Theodore Brown — 2019-05-20T20:30:21+00:00

URL: https://externals.io/message/105757#105766

> > […]
> 
> Yes, this is misconception. Namespace resolutions are cached. First
> fixer tool bringing support for this was correct: PHP-CS-Fixer auto
> imports only optimized functions for this very same reason.

I'm confused, since this doesn't match what I've read elsewhere. For
example, Nikita wrote the following last time function autoloading was
discussed: [1]

> Calling the autoloader if a global function with the name exists will
> totally kill performance. This means that every call to `strpos()` or
> any of the other functions in the PHP standard library will have to
> go through the autoloader first, unless people use fully qualified
> names (which, currently, they don't). This is completely out of the
> question.

In the same thread, it was suggested that a cache could be used
to avoid repeatedly triggering the autoloader, but concerns were
also pointed out with this: [2]

> > […]
> 
> I think triggering it even once for every internal function in the
> code may be too much.
> 
> > […]
> 
> Exactly. Also, caching stuff assumes static environment. What if it
> changes, e.g. autoloader gets different configuration?

This is why I suggested the alternative of by default falling back
to the global namespace for unqualified functions before triggering
the autoloader, and allowing this fallback to be disabled for the rare
case where functions in a namespace are spread across files and need
to override a global function.

I don't think this proposal changes anything related to function
mocking. As you pointed out, libraries can already prevent mocking
by importing or qualifying global function calls.

Sincerely,
Theodore

[1]: https://externals.io/message/94895#94897
[2]: https://externals.io/message/94895#94956

---

## #105767 — Dan Ackroyd — 2019-05-20T20:54:36+00:00

URL: https://externals.io/message/105757#105767

>
> *abusive stuff that shouldn't be on this list*

Please don't troll.

You might be aiming for being amusing, but you're just being disruptive.

cheers
Dan
Ack

---

## #105769 — Gabriel O — 2019-05-21T08:16:26+00:00

URL: https://externals.io/message/105757#105769

> 
> 
> 
> > […]
> 
> I'm confused, since this doesn't match what I've read elsewhere. For
> example, Nikita wrote the following last time function autoloading was
> discussed: [1]
> 
> > […]

Check https://www.reddit.com/r/PHP/comments/b107c5/optimizing_your_php_app_speed/eim2lti <https://www.reddit.com/r/PHP/comments/b107c5/optimizing_your_php_app_speed/eim2lti> which is also comment from nikic.
I guess there is some confusion core maintainers should clear up. Anyway, my point was if it improves perf of only some functions, this config should be named differently and not have impact on non-optimized ones.
