Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:105757 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 62115 invoked from network); 20 May 2019 20:10:38 -0000 Received: from unknown (HELO NAM02-BL2-obe.outbound.protection.outlook.com) (40.92.3.107) by pb1.pair.com with SMTP; 20 May 2019 20:10:38 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=DD3dhYBylM2Kum2tKzGaUxRAtH1+r68EWCABzBgfChw=; b=AUY1D00/wTCfsQxiWEyQJiBiAAYPnVYoBcNsfbXlj8Xi+5tAa8U2KIzPeRo0WH8d+J7GghnnYHQjt4gGIn9kQEarCM2MRjO2MiiLGGkug3nNiSlLasHb5G10F/88fc4uvK/JThzH2Pr2ypAPGwjtX86qiTSq2G2Z4wi2rEkfIAnjNKwyxh/22NynSA7TlIhwC9GoEaD4JshnG5Vlx76HQXsQqwElxEHGNgyCa3UgeYFMuhOit1GKdL8c3qjOci74v7diwpb5pH1wVIekogXwCIP07MkhzCxqaribaiN05/itpb1QLN7ambnDYu5dEh2vCQnGTAYJJbonpzvTBQuqmQ== Received: from BL2NAM02FT040.eop-nam02.prod.protection.outlook.com (10.152.76.58) by BL2NAM02HT192.eop-nam02.prod.protection.outlook.com (10.152.77.142) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.1900.16; Mon, 20 May 2019 17:17:49 +0000 Received: from DM5PR06MB2857.namprd06.prod.outlook.com (10.152.76.54) by BL2NAM02FT040.mail.protection.outlook.com (10.152.77.193) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.1900.16 via Frontend Transport; Mon, 20 May 2019 17:17:49 +0000 Received: from DM5PR06MB2857.namprd06.prod.outlook.com ([fe80::8c9a:8f50:c23:76a5]) by DM5PR06MB2857.namprd06.prod.outlook.com ([fe80::8c9a:8f50:c23:76a5%5]) with mapi id 15.20.1900.020; Mon, 20 May 2019 17:17:49 +0000 To: PHP internals Thread-Topic: High performance function autoloading Thread-Index: AQHVDy+ySrd8q1/GuEiFhoiHIw5ZMQ== Date: Mon, 20 May 2019 17:17:49 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-incomingtopheadermarker: OriginalChecksum:D606822D12A1F05B70686FDEEF2C29304066C3ED744F145175EB9FEC45D17B2F;UpperCasedChecksum:0BACB92B4F398CFAE48AD34646EC89722A3471E1C9617B9CC4E3A89316138A9A;SizeAsReceived:6510;Count:40 x-tmn: [AYvebAWRnEKn0I+yqXcad1NQ9XiSOfkD] x-ms-publictraffictype: Email x-incomingheadercount: 40 x-eopattributedmessage: 0 x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(5050001)(7020095)(20181119110)(201702061078)(5061506573)(5061507331)(1603103135)(2017031320274)(2017031323274)(2017031324274)(2017031322404)(1601125500)(1603101475)(1701031045);SRVR:BL2NAM02HT192; x-ms-traffictypediagnostic: BL2NAM02HT192: x-microsoft-antispam-message-info: h/cwsSZLBF7W/cWEUwDsmtoV+3dvCTbE+aELG4yVg3NXgaQBgrhvzIZrAMsekA97h4hvYLiosY1gfA82//9Efz4fZZYf/G0HD615RUXoSGNuXy9q14RZ4gkzjXKVKOc4mrtOZqbgH8mXCxEpKuCMiRA4NI9wqclvvM/eXa9gSdOsa6u4xcu62Z1up5N501t8 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 80d068fc-7fd0-4076-32ba-08d6dd471585 X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-originalarrivaltime: 20 May 2019 17:17:49.1980 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL2NAM02HT192 Subject: High performance function autoloading From: theodorejb@outlook.com (Theodore Brown) Every time function autoloading been brought up in the past, there=0A= have been concerns about performance issues when calling a global=0A= function from inside a namespace. E.g. calling `strlen` in a loop=0A= would become far slower if every call has to trigger the autoloader.=0A= =0A= But isn't it extremely rare to override global functions in a=0A= namespace? And even if they are overridden, how common is it to call=0A= an overridden function in the same namespace that exists in a=0A= different file?=0A= =0A= Would it be possible to handle this rare case with a per-file option=0A= like `declare(root_fallback=3D0);`?=0A= =0A= Consider the following code examples:=0A= =0A= # example_1.php=0A= ```php=0A= namespace Foo;=0A= =0A= echo strlen('test');=0A= // PHP would first check if Foo\strlen exists. If not, it would check=0A= // whether \strlen exists. Since it does, it would call the built-in=0A= // function as expected without triggering the autoloader.=0A= ```=0A= =0A= # example_2.php=0A= ```php=0A= namespace Foo;=0A= =0A= function strlen(string $str) {...}=0A= =0A= echo strlen('test');=0A= // PHP would first check if Foo\strlen exists. Since it is defined in=0A= // the same file, it would be called without falling back to the=0A= // global function or triggering the autoloader.=0A= ```=0A= =0A= # example_3.php=0A= ```php=0A= namespace Foo;=0A= =0A= use function Bar\strlen;=0A= =0A= echo strlen('test');=0A= // PHP would first check if Bar\strlen exists. If not, it would=0A= // trigger the autoloader rather than falling back to the global=0A= // function. The same thing would occur for any qualified function=0A= // call. E.g. without "use function" the following could be written:=0A= echo \Bar\strlen('test');=0A= ```=0A= =0A= # example_4.php=0A= ```php=0A= namespace Foo;=0A= =0A= echo my_function('test');=0A= // PHP would first check if Foo\my_function exists. If not, it would=0A= // check whether \my_function exists. If not, the autoloader would be=0A= // triggered.=0A= ```=0A= =0A= # example_5.php=0A= ```php=0A= declare(root_fallback=3D0);=0A= =0A= namespace Foo;=0A= =0A= echo strlen('test');=0A= // PHP would first check if Foo\strlen exists. If not, it would=0A= // trigger the autoloader rather than falling back to the global=0A= // function, since root fallback is disabled in the file.=0A= ```=0A= =0A= What are your thoughts about this approach?=0A= =0A= Personally I would rarely have to disable root fallback, since I like=0A= to group functions in a single file per namespace. But it seems like=0A= this would be a good way to enable function autoloading that works=0A= with namespaces spread across multiple files and doesn't slow down=0A= performance.=