2

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,

2

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.

  1. 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.

  1. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.