rfc:autoload_classmap

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
rfc:autoload_classmap [2021/03/15 17:13] – created marandallrfc:autoload_classmap [2021/04/06 05:32] (current) marandall
Line 1: Line 1:
 ====== PHP RFC: Autoload Classmap ====== ====== PHP RFC: Autoload Classmap ======
-  * Version: 0.9+  * Version: 1
   * Date: 2021-03-15   * Date: 2021-03-15
-  * Author: Mark Randall, mrandall@php.net +  * Author: Mark Randall, marandall@php.net 
-  * Status: Draft+  * Status: Under Discussion
   * Target: 8.1   * Target: 8.1
  
Line 12: Line 12:
  
 ===== Proposal ===== ===== Proposal =====
-This RFC would introduce a new mechanism for autoloading classes via an internal classmap loader, that can be configured once per request by passing an associative array mapping the class name to their path on disk. +This RFC would introduce a new mechanism for autoloading classes via an internal classmap loader, that can be configured once per request by passing an associative array mapping each lowercase class name to its path on disk. 
  
-This proposal is written with the names autoload_xx rather than spl_autoload_ as an independent RFC will be proposed to alias the existing SPL autoload functions to simply autoload_xx. If that RFC fails, but this one passes, the implementation would use spl_autoload_xx names instead.+This proposal is written with the names autoload_xx rather than spl_autoload_ as an independent RFC will be proposed to alias the existing SPL autoload functions to simply autoload_xx (or whatever is eventually agreed upon). If that RFC fails, but this one passes, the implementation would use spl_autoload_xx names instead.
  
 +<code php>
 /* Sets the internal autoloader to use the given classmap */ /* Sets the internal autoloader to use the given classmap */
-autoload_set_classmap(array $mapping);+autoload_set_classmap(array $mapping): void {}
  
-Setting the classmap can be performed once per request.  +/Returns the existing classmap (or an empty array) */ 
-* The associative array must contain lowercase class names as the keys, and the location on disk for the value.+autoload_get_classmap(): array {} 
 +</code>
  
-/Returns the existing classmap */ +Rules: 
-autoload_get_classmap(): array;+  Setting the classmap can be performed once per request.  
 +  * The associative array must contain lowercase class names as the keys, and the location on disk for the value.
  
 The autoloader will: The autoloader will:
-Check the classmap first if it is set.  +  Always check the classmap first if it is set.  
-* (If Found) Trigger a TypeError if the relevant key's value is not a string. +  * (If Found) Trigger a TypeError if the key'associated value is not a string. 
-* (If Found) Trigger an Error if the relevant class is not defined after including the file specified in the key value+  * (If Found) Use the REQUIRE mechanism.  
-* (If FoundUse the REQUIRE_ONCE mechanism+  * (If Found) Trigger an Error if the relevant class is not defined after including the file specified in the classmap
 + 
 +If the classmap is not set, or the class name cannot be found within the classmap, the autoloader will continue to perform its traditional behaviour of iterating over the functions specified by spl_autoload_register.  
 + 
 +===== Performance ===== 
 + 
 +Testing suggests that autoloading through an internal classmap delivers around 5% performance increase vs a userland function call (e.g. composer).  
 + 
 +This is 5% of the cost of the autoloading, and not the execution as a whole. Testing was performed by creating 50,000 empty classes each in an individual file, and then autoloading every one of them in a loop. Amount of classes was purely to help reduce noise. 
 + 
 +Average for internal classmap was 0.295 vs 0.313 for spl_autoload_register representing 5.9% difference (in autoload performance only).
  
-If the classmap is not set, or the class name cannot be found within the classmap, the autoloader will continue to perform its traditional behaviour of iterating over the functions specified by spl_autoload_register. 
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
rfc/autoload_classmap.1615828406.txt.gz · Last modified: 2021/03/15 17:13 by marandall