<blockquote>
  <p>I often read the first user request is slowest to Craft Sites point mentioned in articles/posts, what exactly is that referring to?</p>
</blockquote>

<p>Probably missing some context surrounding where you originally read that, but if it was talking about caching, then maybe it was referring to the first user that triggers the request to generate the necessary cached content.</p>

<blockquote>
  <ol>
  <li>Is this loop that creates these URL's running every single time a page is requested? Is this bad practice?</li>
  </ol>
</blockquote>

<p>Craft will process <code>registerSiteRoutes</code> on every request, so if you have a lot of business logic going on in your plugins around that method, it might be worth caching the results for a time to speed things up.</p>

<blockquote>
  <ol start="2">
  <li>Or do these files get loaded up regardless of being called, and therefore should be re-written into functions so that they don't load the data unless asked?</li>
  </ol>
</blockquote>

<p>You assume correctly, but generally I'd recommend against haven't expensive operations in class constructors/init() methods.  My personal preference would be to explicitly load the data.</p>

<blockquote>
  <ol start="3">
  <li>Plugin count -- does having many small, niche plugins affect load speed versus less but larger plugins that cover more functionality? Is plugin count something that needs to be carefully considered, a common caution when working with a plugin-based CMS like Wordpress?</li>
  </ol>
</blockquote>

<p>The more plugins, the more file system access Craft has to do to find and load them. That part is usually negligible performance wise, but there's nothing stopping a plugin from doing something crazy slow in its constructor/init() methods.</p>

<p>This has been alleviated in Craft 3 to a large extent, because Craft doesn't have to instantiate a plugin just to figure out information about it (version number, author, etc.).</p>

<blockquote>
  <p>Basically, I guess the question is, I would love to be able to read a detailed explanation of the scheme/process in how plugins and their respective plugin file, config, services, controllers, templates, variables etc are loaded, and if there is a guideline on what is and isn't good practice when designing the flow of plugins for optimization?</p>
</blockquote>

<p>Generally speaking, Craft will lazy-load a plugin's <code>Controller</code>, <code>Enum</code>, <code>Helper</code>, <code>Model</code>, <code>Record</code>, <code>Service</code>, <code>Variable</code>, and <code>Validator</code> classes as they are requested, just like Craft's own files are lazy loaded.</p>

CC BY-SA 3.0
2016-02-02T00:47:07.937Z
2016-02-02T00:47:07.937Z
Brad Bell
<p>This is a fairly generalized question, maybe I completely missed where to learn about this, but I've always been curious about it as it'd help me be a little more aware of what would and would not be good practice in plugin development.</p>

<p>I often read the <strong>first user request is slowest</strong> to Craft Sites point mentioned in articles/posts, what exactly is that referring to?</p>

<p>With each page request are all plugins and their files opened and loaded up? Or are these loaded and stored <em>somewhere</em> for X period of time, decreasing loading time per request?</p>

<p>Some examples of plugin scenarios/questions</p>

<p><strong>1:</strong></p>

<p>I have a fairly decent-sized loop in two plugins via <code>public function registerSiteRoutes() {}</code> that generate 200-1000+ custom routes, as those URL's will not have any content from Craft's entries (and thus don't have URL's,) or are completely dynamic (for forum topics with unpredictable topic urls.) </p>

<p>Is this loop that creates these URL's running every single time a page is requested? Is this bad practice?</p>

<p><strong>2:</strong></p>

<p>I can have some <strong>plugin services</strong> load large files of data in <code>__construct()</code>, or as <code>var $data;</code> for use in specific scenarios. Under the assumption that these service files are only loaded when a plugin calls to it, it wouldn't be slowing requests down that don't use the data because only the pages calling for them would load the data, such as through <code>craft()-&gt;myPluginName_serviceName-&gt;getVarWithLotsOfData;</code> requests.</p>

<p>Or do these files get loaded up regardless of being called, and therefore should be re-written into functions so that they don't load the data unless asked?</p>

<p><strong>3:</strong></p>

<p>Plugin count -- does having many small, niche plugins affect load speed versus less but larger plugins that cover more functionality? Is plugin count something that needs to be carefully considered, a common caution when working with a plugin-based CMS like Wordpress?</p>

<p>--</p>

<p>Basically, I guess the question is, I would love to be able to read a detailed explanation of the scheme/process in how plugins and their respective <strong>plugin file, config, services, controllers, templates, variables etc</strong> are loaded, and if there is a guideline on what is and isn't good practice when designing the flow of plugins for optimization?</p>

<p>If there is documentation that I missed that greatly covers and explains all of this, feel free to link it and put me in a corner for shame. D:</p>

<p>Thanks,</p>

<plugin-development><performance><optimization>
Plugin loading scheme
# Plugin loading scheme

(Asked by: username_0 on 2016-01-27)

This is a fairly generalized question, maybe I completely missed where to learn about this, but I've always been curious about it as it'd help me be a little more aware of what would and would not be good practice in plugin development.

I often read the **first user request is slowest** to Craft Sites point mentioned in articles/posts, what exactly is that referring to?

With each page request are all plugins and their files opened and loaded up? Or are these loaded and stored *somewhere* for X period of time, decreasing loading time per request?

Some examples of plugin scenarios/questions

**1:**

I have a fairly decent-sized loop in two plugins via `public function registerSiteRoutes() {}` that generate 200-1000+ custom routes, as those URL's will not have any content from Craft's entries (and thus don't have URL's,) or are completely dynamic (for forum topics with unpredictable topic urls.)

Is this loop that creates these URL's running every single time a page is requested? Is this bad practice?

**2:**

I can have some **plugin services** load large files of data in `__construct()`, or as `var $data;` for use in specific scenarios. Under the assumption that these service files are only loaded when a plugin calls to it, it wouldn't be slowing requests down that don't use the data because only the pages calling for them would load the data, such as through `craft()->myPluginName_serviceName->getVarWithLotsOfData;` requests.

Or do these files get loaded up regardless of being called, and therefore should be re-written into functions so that they don't load the data unless asked?

**3:**

Plugin count -- does having many small, niche plugins affect load speed versus less but larger plugins that cover more functionality? Is plugin count something that needs to be carefully considered, a common caution when working with a plugin-based CMS like Wordpress?

--

Basically, I guess the question is, I would love to be able to read a detailed explanation of the scheme/process in how plugins and their respective **plugin file, config, services, controllers, templates, variables etc** are loaded, and if there is a guideline on what is and isn't good practice when designing the flow of plugins for optimization?

If there is documentation that I missed that greatly covers and explains all of this, feel free to link it and put me in a corner for shame. D:

Thanks,

**Tags:** plugin-development, performance, optimization

---

## 1 answers

### Answer by username_1 on 2016-02-02. Score: 2 | Accepted answer

> I often read the first user request is slowest to Craft Sites point mentioned in articles/posts, what exactly is that referring to?

Probably missing some context surrounding where you originally read that, but if it was talking about caching, then maybe it was referring to the first user that triggers the request to generate the necessary cached content.

> 1. Is this loop that creates these URL's running every single time a page is requested? Is this bad practice?

Craft will process `registerSiteRoutes` on every request, so if you have a lot of business logic going on in your plugins around that method, it might be worth caching the results for a time to speed things up.

> 2. Or do these files get loaded up regardless of being called, and therefore should be re-written into functions so that they don't load the data unless asked?

You assume correctly, but generally I'd recommend against haven't expensive operations in class constructors/init() methods. My personal preference would be to explicitly load the data.

> 3. Plugin count -- does having many small, niche plugins affect load speed versus less but larger plugins that cover more functionality? Is plugin count something that needs to be carefully considered, a common caution when working with a plugin-based CMS like Wordpress?

The more plugins, the more file system access Craft has to do to find and load them. That part is usually negligible performance wise, but there's nothing stopping a plugin from doing something crazy slow in its constructor/init() methods.

This has been alleviated in Craft 3 to a large extent, because Craft doesn't have to instantiate a plugin just to figure out information about it (version number, author, etc.).

> Basically, I guess the question is, I would love to be able to read a detailed explanation of the scheme/process in how plugins and their respective plugin file, config, services, controllers, templates, variables etc are loaded, and if there is a guideline on what is and isn't good practice when designing the flow of plugins for optimization?

Generally speaking, Craft will lazy-load a plugin's `Controller`, `Enum`, `Helper`, `Model`, `Record`, `Service`, `Variable`, and `Validator` classes as they are requested, just like Craft's own files are lazy loaded.


---

