perf(autoloader): optimize package list accumulation in loop#10341
perf(autoloader): optimize package list accumulation in loop#10341gr8man wants to merge 1 commit into
Conversation
|
Thanks for the PR. I have to say I respect the ambition here. Not every The implementation itself looks fine to me, but I think the performance framing is doing more work than the code change needs. I'd trim the PR body quite a bit and frame this as a small internal cleanup. I'd also consider dropping the new test. It reads a bit like |
|
Thanks, but I don't think this optimization is justified.
A benchmark showing a real-world improvement would be needed to justify this change. |
|
Thanks for the thorough review and valid points. You're absolutely right – after deeper analysis, the practical gain would be negligible. In standard scenarios, the PHP loop overhead might even slightly degrade performance compared to the native array_merge. Closing the PR. |
Description
In
Autoloader::loadComposerNamespaces(),array_merge()was being used inside aforeachloop to accumulate composer packages into$packageList:Complexity Analysis:
Before ($i$ , $i \times S$ , where $S$ is the size of each package version list) plus the new ones.
array_mergeinside the loop):On each iteration
array_mergeallocates a completely new array and copies all existing elements from$packageList(which has sizeAfter (Nested
foreachloop with direct assignment):The nested loop directly assigns elements to
$packageListone by one. This preserves the exact behavior ofarray_merge()where duplicate string keys (package names) in subsequent array elements overwrite previous ones, while completely avoiding intermediate array copying.This PR shifts the package list building process from quadratic time$O(N^2)$ to linear time $O(N)$ while maintaining 100% behavioral compatibility with the original
array_mergelogic.Additionally, a unit test was added to verify the package list auto-discovery flow inside
loadComposerNamespaces.Checklist: