Performance: autoloader is executed too many times and is slow
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 36.9k
- Forks
- 5.2k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 713
Description
⚠️ This issue respects the following points: ⚠️
- This is a bug, not a question or a configuration/webserver/proxy issue.
- This issue is not already reported on Github OR Nextcloud Community Forum (I've searched it).
- Nextcloud Server is up to date. See Maintenance and Release Schedule for supported versions.
- I agree to follow Nextcloud's Code of Conduct.
Bug description
When benchmarking the login page of NextCloud, I found out it was extremely slow, about 12 requests / second on my laptop, compared to around 380 requests / second on the login page of another PHP app I'm developing.
The login page is making 29 SQL requests, and is loading 768 files of PHP code (!).
When watching what's going on with Xdebug profiler I can see that classLoader::loadClass from Composer is executed more than 14.000 times and is taking 13% of the execution time. That's a lot. Most of the time it doesn't do much as the required file is not part of this composer instance (as all projects have their own composer loader). It seems that there are dozens of composer loaders registered, and every one is called until the correct project loader is found. For each of the 700 included files.
I tried writing a short autoloader that will just store a cache of all files and have only one instance (see attached patch). It's just a proof of concept, not a ready-to-implement patch (hence why the bug report and not a PR).
0001-Alternative-autoloader.patch.gz
This is a very simple and nave approach, just to see the impact it has.
With it I get only 752 calls, and it is now only using 3.5% of the execution time.
It's likely that there are better options out there, but this was just a quick test and a pointer on a place where NextCloud performance could be made better. Even though it should be questioned why a simple login page is doing so many requests and loading so many unrelated classes.
Steps to reproduce
--
Expected behavior
--
Installation method
None
Nextcloud Server version
25
Operating system
None
PHP engine version
None
Web server
None
Database engine version
None
Is this bug present after an update or on a fresh install?
None
Are you using the Nextcloud Server Encryption module?
None
What user-backends are you using?
- Default user-backend (database)
- LDAP/ Active Directory
- SSO - SAML
- Other
Configuration report
No response
List of activated Apps
No response
Nextcloud Signing status
No response
Nextcloud Logs
No response
Additional info
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by profiling the login page and examining Composer's classLoader::loadClass calls, using the attached alternative-autoloader patch as a performance reference. Compare the loader-call count and execution time with the current behavior, then define a solution that reduces redundant autoloader work without changing class loading behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- backend, performance
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100