TheApproach / Approach

GNU General Public License v2.0
5 stars 2 forks source link

Resource Autoloading is O(n^2) #6

Open Brandon-Sawyer opened 6 months ago

Brandon-Sawyer commented 6 months ago

In Resource/Resource.php:850, the implementation of the callback function passed to spl_autoload_register will only work for the one resource class that is passed to the __update_composer_autoloader static method. Since each registered function will be called each time a class is auto loaded, this means the function calls will scale exponentially, in O(n^2) time.

To test this, I added 2 global variables to track the total number of times the autoload functions are called as well as the number of times that the provided class name actually matched the resource class file that was being autoloaded. My results found that out of 25,992 total function calls, only 114 function calls were actually for the right file. 114 114 2 is 25,992 (the 2 comes from an unimportant implementation detail in the part of the code i was working on).

To fix this, the $resource_class in the callback function should actually be derived from the provided $classname rather than passed in via use when the function is defined. I also found that the top level of the namespace for $classname did not match the directory in the project root, so the namespace and directory name should probably be changed to match.

TheApproach commented 3 months ago

turns out this is a feature to allow discovery process to continue, which is pre-processing and not invoked during normal runtime

original purpose was to cascade folder / namespace alignment to allow projects with simple stacks (1 type of database, 1 server/pool)

eg to mint classes directly into /Resource/MyTable instead of /Resource/MariaDB/MyServer/MyDatabase/MyTable