Skip to content

perf: fast-path for default Composer namespaces loading in Autoloader - #10706

Open
gr8man wants to merge 1 commit into
codeigniter4:developfrom
gr8man:perf/autoloader-composer-namespaces
Open

gr8man wants to merge 1 commit into
codeigniter4:developfrom
gr8man:perf/autoloader-composer-namespaces

Conversation

@gr8man

@gr8man gr8man commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

Description
In Autoloader::loadComposerNamespaces(), whenever Composer namespaces are loaded, the autoloader was unconditionally calling InstalledVersions::getAllRawData(), performing an array_merge across all installed package versions, and executing a triple-nested loop testing str_starts_with($path, $installPath) for every single PSR-4 namespace.

In almost all applications, Config\Modules::$composerPackages is empty (default configuration with neither only nor exclude specified). In this default state, all registered PSR-4 prefixes from Composer already belong to installed packages (or the root package) and can be registered directly into $this->prefixes without scanning InstalledVersions.

This PR adds a fast-path for the default configuration:

  • Checks if $only === [] && $exclude === [] upfront.
  • Directly registers the trimmed PSR-4 namespaces into $this->prefixes, skipping InstalledVersions::getAllRawData(), array_merge, and the triple-nested str_starts_with() matching loop entirely.
  • When package filtering is configured (only or exclude), the original full validation logic is preserved with zero BC breaks.

Benchmark (10,000 iterations)

  • Original loadComposerNamespaces(): 1496.44 ms (~0.150 ms per request)
  • Fast-path loadComposerNamespaces(): 144.24 ms (~0.014 ms per request)
  • Speedup: 10.4x (90.4% latency reduction during autoloader bootstrap)

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@carson-codeigniter4 carson-codeigniter4 Bot added the enhancement PRs that improve existing functionalities label Nov 1, 2026
When Config\Modules::$composerPackages has no filters defined (the default
behavior in almost all applications), all PSR-4 prefixes from Composer
belong to installed packages.

Skip the expensive InstalledVersions::getAllRawData() scanning, versions
array_merge, and triple-nested str_starts_with matching loop when neither
'only' nor 'exclude' is configured. When package filtering is configured,
the original full validation logic is preserved with zero BC breaks.
@gr8man
gr8man force-pushed the perf/autoloader-composer-namespaces branch from 8863a59 to 26612d5 Compare November 1, 2026 22:41

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement PRs that improve existing functionalities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant