nextcloud / server

☁️ Nextcloud server, a safe home for all your data
https://nextcloud.com
GNU Affero General Public License v3.0
27.38k stars 4.07k forks source link

Performance: autoloader is executed too many times and is slow #40236

Open bohwaz opened 1 year ago

bohwaz commented 1 year ago

⚠️ This issue respects the following points: ⚠️

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?

Configuration report

No response

List of activated Apps

No response

Nextcloud Signing status

No response

Nextcloud Logs

No response

Additional info

No response

joshtrichards commented 3 months ago

Hi @bohwaz - Looks like this was based on NC25, which reached end-of-life a few weeks after your note/work.

A lot of things have changed since then. This is not my area of expertise, but since no one else has popped in yet... I thought you might find these links of interest for some additional context:

38835

https://docs.nextcloud.com/server/latest/developer_manual/search.html?q=autoloader&check_keywords=yes&area=default

https://github.com/nextcloud/server/pulls?q=is%3Apr+autoloader+is%3Aclosed+label%3A%22performance+%F0%9F%9A%80%22

https://github.com/nextcloud/server/pulls?q=is%3Apr+autoloader+is%3Aclosed+in%3Atitle