Problem

Having the Drupal\Core and Drupal\Component namespaces registered as fallbacks results on a strpos call on every class called in the Drupal namespace. Namespace fallbacks means it supports multiple paths within one namespace, hence the requirement to check strpos() to check each fallback directory. This can REALLY slow things down.

Solution

Register the namespaces individually, we don't need to have them as fallbacks. This'll save us lots of calls to strpos().

Comments

msonnabaum’s picture

Title: Stop calling file_exists() on every class load » Explicitly register \Drupal\Component and \Drupal\Core to avoid file_exists calls
Status: Needs review » Needs work

Drupal\Core and Drupal\Component are not registered as fallbacks, Drupal is.

I think it's a fine idea though to register them explicitly. I don't think it's necessary to remove the fallback though. Separate issue.

msonnabaum’s picture

Status: Needs work » Reviewed & tested by the community

Actually, damnit, Rob's right. We can get rid of the fallback. It just means we can't have classes in the \Drupal namespace, which seems reasonable. Anything we might put there should go in \Drupal\Core or \Drupal\Component.

msonnabaum’s picture

Status: Reviewed & tested by the community » Needs review

Oops, got ahead of testbot.

cweagans’s picture

Status: Needs review » Reviewed & tested by the community

Looks like testbot is happy with it, and it looks pretty straightforward. I say yes, and it looks like msonnabaum has voiced his approval as well.

tstoeckler’s picture

Status: Reviewed & tested by the community » Needs review
+++ b/core/includes/bootstrap.inc
@@ -2950,21 +2950,14 @@ function drupal_classloader() {
+      'Drupal\\Core' => DRUPAL_ROOT . '/core/lib',
+      'Drupal\\Component' => DRUPAL_ROOT . '/core/lib',

Can we drop the escaping in single-quoted strings, pretty plz? :-)

tstoeckler’s picture

Status: Needs review » Reviewed & tested by the community

That was a crosspost. Didn't want to go to needs work.

robloach’s picture

StatusFileSize
new1.33 KB

Was following UniversalClassLoader->findFile(), but I'm indifferent :-) . Same patch, without the escaped backspaces.

dries’s picture

Did you happen to measure how much cost savings this provides? Regardless of any benchmark results, this makes a lot of sense to me.

msonnabaum’s picture

I haven't benchmarked it, but it's not hard to predict what it will do. The initial description of what this does isn't correct though. The is_file() for the fallback only gets called if the the class isn't found in the registered namespaces. The idea being that you register all the module namespaces, so the only thing that gets through to the fallback is core. I don't believe this patch will save us any is_file calls.

What this patch *does* save us however is a bunch of calls to strpos when we're loading core classes. This comes at the cost of being able to register a class in the \Drupal namespace, outside of \Core and \Component, which doesn't seem like a big loss. We could actually keep the fallback if we thought we wanted that, and the rest of this patch would have the same benefit.

catch’s picture

StatusFileSize
new51.32 KB
new101.94 KB

Here's before/after xhprof screenshots, results in 1/6th the number of calls to strpos(), which at the moment means 1,000 less calls (it's likely to be a lot more than that by the time we get to release).

catch’s picture

Issue summary: View changes

Updated issue summary.

chx’s picture

I am severely tempted to mark this as duplicate in favor of #1658720: Use ClassLoader instead of UniversalClassLoader but of course first getting this won't hurt. But, it will be ripped out soon anyways :)

robloach’s picture

Status: Reviewed & tested by the community » Closed (duplicate)

Agreed.

sun’s picture

Title: Explicitly register \Drupal\Component and \Drupal\Core to avoid file_exists calls » Explicitly register \Drupal\Component and \Drupal\Core to increase class lookup performance
Status: Closed (duplicate) » Needs review
StatusFileSize
new1.65 KB

This patch was ready, the other one is far from being there.

I don't understand why this was marked as duplicate instead of simply committing it.

Re-rolled against HEAD. This looks RTBC to me.

robloach’s picture

Status: Needs review » Reviewed & tested by the community
cweagans’s picture

Yeah, definitely RTBC.

cweagans’s picture

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Ok, cool. Looks like the benchmarks in #10 show a nice improvement on # of function calls here.

Committed and pushed to 8.x.

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

Updated issue summary.