Skip to main content

How can plugin systems be designed so they don"t waste so many resources?


I am trying to build a basic plugin system like the kind you often find in a CMS like WordPress. You have a folder of plugins which tie into the main system's operation through event notifications using an Observer or Event design pattern.



The problem is it's impossible for the system to know which events the plugin wants to act upon - so the system has to load each plugin for every page request just to find out if that plugin is actually needed at some point. Needless to say, that's a lot of wasted resources right there-- in the case of WordPress, that adds up to several extra MB of memory for each request!



Are there alternative ways to do this?



For example, is there a way to load all this once and then cache the results so that your system knows how to lazy-load plugins? In other words, the system loads a configuration file that specifies all the events that plugin wishes to tie into and then saves it in APC or something for future requests?



If that also performs poorly, then perhaps there is a special file-structure that could be used to make educated guesses about when certain plugins are unneeded to fulfill the request.


Source: Tips4all

Comments

  1. I do have a plugin management tool, but I only every used it with predominantly procedural plugins, and with all includes usually loaded at once. But for an event-based and lazy-loading API I could imagine using shallow wrappers for the plugin management, and resorting to autoloading for the actual extensions.

    <?php
    /**
    * api: whatever
    * version: 0.1
    * title: plugin example
    * description: ...
    * config: <var name="cfg[pretty]" type="boolean" ...>
    * depends: otherplugin
    */

    $plugins["title_event"] = "TitleEventClass";
    $plugins["secondary"] = array("Class2", "callback");
    ?>


    In this example I'd assume the plugin API is a plain list. This example feature-plugin-123.php script would do nothing but add to an array when loaded. So even if you have a dozen feature plugins, it would only incur an extra include_once each.

    But the main application / or plugin API could instead just instantiate the mentioned classes (either new $eventcb; for the raw classnames or call_user_func_array for the callbacks). Where in turn it would offload the actual task to an autoloader. Thus you have a dual system, where one part manages the list, the other locates the real code.

    I'm thereby still imaganing a simple config.php which just lists plugins and settings like this:

    <?php
    include_once("user/feature-plugin-123.php");
    include_once("user/otherplugin2.php");
    include_once("user/wrapper-for-htmlpurifier.php");
    $cfg["pretty"] = 1;


    Again taking in mind that these are just wrappers / data scripts, with the plugin description for managability. One could as well use an actual register_even() API and define an additional wrapper function in each. But listing classnames seems the simplest option.

    The aforementioned management tool is kind of rusty and ugly: http://milki.include-once.org/genericplugins/
    But it's uneeded if you just need a list (sql table) and no settings management. That overhead is only for prettyprinting the plugin meta data and keeping a human-readable config.php.

    In conclusion:

    spl_autoload() on the include_path, and a simple event->classname registry, one wrapper script each, simply included all at once.

    ReplyDelete
  2. Wordpress and other CMS systems are very bad examples.

    What we have to understand is that modular, almost always means heavier.

    The best scheme that I ever worked with to solve this situation is a class based plugin, with a strict naming convention using an auto loader.

    So, before using the plugin, you´ll need to create an instance, or use static functions.

    You can even call the plugin like:

    <?php $thePlugin::method(); ?>


    eg:

    <?php

    spl_autoload_register('systemAutoload');

    function systemAutoload($class)
    {
    $parts = explode('_',$class);


    switch($parts[1])
    {
    case "Plugin":
    include("/plugins/{$parts[2]}/{$parts[2]}.php");
    break;
    }

    // ...

    }

    ?>


    Regarding Events:

    You have to register this events statically to avoid bringing it in a dynamic manner.

    The database would be the right place to do it. You can have an events table, and install() and uninstall() methods on the plugin class to add specific events or bind methods to other events. It´s one database query, and if you want more from it, add it to memcached, or to a flat ini file.

    Works well for me. This way I was able to get a heavy system that was consuming 8mb per request to drop to 1mb, with the exactly same list of features, without advanced caching. Now we can add more features and mantain the system "clean"

    Hope that helps

    ReplyDelete
  3. I would save the plugin class name along with it's subscribed events in a configuration file and then save the parsed config file in APC, for example. Then when an event is triggered the system can lazy load the appropriate plugin classes as needed.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex