Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:105758 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 73397 invoked from network); 20 May 2019 20:49:06 -0000 Received: from unknown (HELO mail-it1-f176.google.com) (209.85.166.176) by pb1.pair.com with SMTP; 20 May 2019 20:49:06 -0000 Received: by mail-it1-f176.google.com with SMTP id a190so450150ite.4 for ; Mon, 20 May 2019 10:56:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Z/66yejnNLh2Kxjk/+DqD3Jdxh3cjSYUGMB7B2m0Tf0=; b=WyVx85Eu9a8wNeZGDLX9aNU8xsYQHKoJZx7gOw0dFszeqSKyi2uwAKSCmyCh9Va85F w+dWMXagdYGh4bNMKovWR3LdCOaUNUgLXdorwiQRVrLBSa2ANsMYIP//Pqh+Htn7sDEU VDHSG2tVYLXCdLnX45knXBIQpjNnruoP1mkmtHsgy9UCmVY9B0GgE4vHnK+f4zwdhTEL 0U55n+HMtZ0VjTgG6Ljk5vAzhd4GvbknkOmooaOHYmrEbhyvY0KjTmcuWAUIhoEaHo2B x5u1JBiWgth5Lfa6ICksimwLzozEq8e4ASroEyMsY7PkoTp5WrnahGayg/EoaibtbaG9 ujFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Z/66yejnNLh2Kxjk/+DqD3Jdxh3cjSYUGMB7B2m0Tf0=; b=SO54cKUSTCpzEI72GOLh6rwTPiRVzhxxwvPMn85ycxiMdz/bmKcJpJCbmYQ4f/Br+x +YFWKlNlr9NBGj00wf9kWZoOpDXFxD3KCtcKzw9k1hWLtemH8K5hS7SmSlfXly5Pi7KM HmBhmRb/H/FxWNpljA+lxBIfl+DaZcPxs4vzJ98LmVoZm6gXjAYuU5rLLYaMUoLkJzxy 7FiEQE44Ankd6HPS0/9qRlw6JtgP5VNFHGJ1buudBwsWuNlc1Fa4F9HR/pGPA8RbiVwx UEwcQ1ghZfHxMOODlkd6Wo2ZMiDIVlh1zQW1bLG2L3WjWYAK5i12gHXpCTcpYq6/XbcK 47XA== X-Gm-Message-State: APjAAAVLfiTUw63L06DGeJDbAUeEbZuYRAGMsC3cvlwlZCFyJPxuJjtk Z61tJTW7/CE4KlzvLmSI/Ox2mmxUX63zdOlahto= X-Google-Smtp-Source: APXvYqwjnx38kWm/32r3qQwqW1AOVxp4IjmbeftujICHlJpTfCEc8xlMVdwpY/Mu7jO2pNs3brM7+nw8gnEQyNqPnnE= X-Received: by 2002:a24:3242:: with SMTP id j63mr261983ita.116.1558374977735; Mon, 20 May 2019 10:56:17 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 20 May 2019 19:56:05 +0200 Message-ID: To: Theodore Brown Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000f990f305895572b7" Subject: Re: [PHP-DEV] High performance function autoloading From: ocramius@gmail.com (Marco Pivetta) --000000000000f990f305895572b7 Content-Type: text/plain; charset="UTF-8" Hey Theodore, On Mon, May 20, 2019 at 7:17 PM Theodore Brown 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/ --000000000000f990f305895572b7--