Every request a lot of code is interpreted but also a lot of this code isn't actually used for the current request. The most ideal situation would be that only necessary code is interpreted. D7 already has something to achieve this: registry.

Another improvement would be if functions that are only called in a combination (like hooks), also are combined into one file (and when thy have the same arguments, maybe even into one function) using code generation (only needs to be called when a module is enabled/disabled).

To illustrate this an example for hook-implementations:

modules/
  example.module
  example.hooks (move all hook-implementations of this module into this file)

Another option would be to move all non-hook-implementations to .inc files and leave hook-implementations in the .module file

When a module is enabled/disabled all hook-implementations are combined in files per hook (generated).

files/
  code/
    hooks/
      menu.inc (containing all hook-implementations of all modules of hook_menu())
      block.inc (containing all hook-implementations of all modules of hook_block())
      etc...

Maybe it also possible to combine all hook-implementations into one function per hook; would be tricky because hook-implementations use the same scope then. On the other hand it would mean a very big performance improvement since function calls are time-consuming.

module_invoke($hook) then should include "file/code/hooks/$hook.inc" every time a new hook is used.

Since the original files aren't modified it is still possible to use the .hook file instead of the generated ones (useful for debug purposes).

Overall I think code generation could bring drupal a big performance improvement. Besides code generation for hook-implementations there are several other possibilities.

CommentFileSizeAuthor
#8 dcc.zip7.46 KBcasey
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

alanburke’s picture

Issue tags: +Performance
catch’s picture

casey - have you seen #345118: Performance: Split .module files by moving hooks and #340723: Make modules and installation profiles only require .info.yml files? I think much of what you're talking about other than the actual code generation is discussed there.

Being able to group all $module_nodeapi_load() and $module_form_alter() into a single file per hook sounds interesting, but I don't see how you'd get round the security issue of allowing Drupal write access to itself, nor the issue of duplicate function declarations between the original file and the hook.

Additionally, many modules implement upwards of 10 hooks each (not to mention using their API functions within hooks, which Drupal can't know much about, requiring the .module to be loaded too anyway. So at a certain point, the number of aggregated hook files which would need to be loaded might well end up equivalent to the number of .module files (or whichever files we end up loading when things have been split up properly for the registry) - and we wouldn't end up gaining anything. I think this issue is better handled in the two existing patches - where we can set up good conventions for contrib modules to follow and make improvements pretty much just by moving some code around. But leaving this open for more discussion.

casey’s picture

Ah, no I didn't see these issues. But that is a good start for implementing code generation :)

1. security
Why is writing access a security issue? Code is only generated when modules are enabled/disabled (when drupal starts/stops using more/less code).

2. duplicate declarations
The original files shouldn't be loaded when the optimized ones are generated; that's why hook implementations have to be in a seperate file. You also could rename the generated functions by giving it a prefix to ensure to duplicate declarations.

You could also add an extra function to these files: module_invoke_$hook() with fixed calls to hook-implementations; no need for registry lookups.

3. loaded files
Most hooks aren't used every request (hook_menu() only for menu rebuild, hook_nodeapi() only for pages containing nodes, etc). So I guess it does bring down the number of included files.

4. API functions within hooks
Maybe hook-implementations should include their needed code, or use the registry.

catch’s picture

Any solution which allows Drupal to write code to it's own directories which will subsequently be executed is going to be won't fixed - once apache has write access to those directories, then there are many potential ways code can be written to them, either from inside or outside Drupal itself depending on configuration. See the massive effort which went in to making http://drupal.org/project/plugin_manager into a secure solution for some of the arguments around this.

casey’s picture

Other possibilities:
- page callbacks could also be moved into seperate files (during code generation);
- theme() function can be hugely optimized using fixed calls in a generated version per theme.

casey’s picture

I see...

Maybe then Drupal should generate code when a module is enabled/disabed, provide it to the user to download and ask the user to upload it. Nah... don't think so :)

symfony-project uses code generation, for database models if I am right (called Propel I believe); that's where I got the idea from.

killes@www.drop.org’s picture

Status: Active » Closed (duplicate)

There is no code in here and it doesn't look as if we'd get any. There rest can be dealt with in #345118: Performance: Split .module files by moving hooks.

casey’s picture

Status: Active » Closed (duplicate)
FileSize
7.46 KB

Well, I've been testing some code generation techniques for Drupal 6; I created a module that copies the code of your drupal installation to a temporary directory. First it splits all functions and class into seperate files. Then it modifies the function calls so that functions gets loaded when needed.

It will decrease memory usage a lot! Especially when a great number of modules are installed.

WARNING Please don't try this module on your production website; it'll probably destroy it!

To try the module:

  • Make sure you use Drupal 6, database mysqli and theme garland (hardcoded)
  • download and install attached dcc.module
  • Move index2.php from dcc directory to your drupal root
  • Goto Admin > Drupal Compiler > Build and run it
  • Call yoursite.com/index2.php?q={path}
  • Or rename index.php to something else, and index2.php to index.php

    Note that this module is more of a proof of concept than an actual module! What do you think? Could be an alternative/addition to the registry in D7?

  • killes@www.drop.org’s picture

    Status: Closed (duplicate) » Active

    I think it should be a contrib module.

    CorniI’s picture

    Status: Closed (duplicate) » Active

    subscribe

    sun’s picture

    wuf31’s picture

    Mm..
    Sounds interesting. Curious on how this is gonna turn up.
    @casey, any progress on this one ?

    catch’s picture

    Version: 7.x-dev » 8.x-dev

    This is almost certainly a contrib project, but leaving open.

    ClearXS’s picture

    Sorry for my PHP/Apache etc. technical lackings, but in general:

    Normally C programming (assembly language compiling) is much faster than basic; its important to compile everything when the program/module is ready to the assembly language form.

    So how about Drupal, PHP in general? To me leaving a program in human language, sounds amateuristic. But those were ideas in the beginning of programming, for me some lblue monday long time ago.

    Then indeed, Drupal is amazingly slow, considering the speed of a computer/server and other webware. Ofcourse only installing the basic core functions will be much faster. But I loaded my install with a lot of mods (in the lower hundreds, but many mods should be combined) to make some supersite. Not even that many, considering that there are thousands of Drupel mods. It took me years (by other reasons too) to get over this barrier of Drupal being nasty to accept installations, because it is slow. I had to put my memory up to 96M (now 500M), script time to 300. But that didn't work untill I added a seemingly undocumented rule in de settings.php for that script time too. Yes, something like cgi-php, I selected on Bluehost and I'm not sure if thats working now. For the normal php I did:

    .htaccess:
    php_value memory_limit 500M
    php_value max_execution_time = 300
    php_value max_input_time = 600

    #php.ini:
    max_execution_time = 300
    memory_limit = 500M
    max_input_time = 600

    settings.php:
    ini_set('memory_limit', '500M');
    ini_set('max_input_time', '600'); (own idea; not documented?)
    ini_set('max_execution_time', '300'); (own idea; not documented?)

    Now most modules are active someway, my Drupal is very, very slow. It surpises me that my browser accepts these long times, but other users might have that problem indeed, or just walk out of this not wanting to wait that long.
    (yeah, I have Bluehost, a dedicated probably is much faster)
    I'm really amazed that experienced programmers -what I'm not- let this severe problem going on for years. Or did I miss something like nice theory, but in practice?

    Anyway, just googling on this, first page:
    * Compiling PHP and Apache 2 from source on Linux OS http://www.web-tech-india.com/articles/php/compiling_php_apache/
    * Roadsend PHP compiler http://en.wikipedia.org/wiki/Roadsend_PHP
    * phc -- the open source PHP compiler http://www.phpcompiler.org/
    * http://en.wikipedia.org/wiki/PHP#Speed_optimization
    Speed optimization
    As with any interpreted language, PHP scripts are stored as human-readable source code and are compiled on-the-fly by the PHP engine. In order to speed up execution time and not have to compile the PHP source code everytime the webpage is accessed, PHP scripts can also be stored in binary format using PHP compilers such as phc and roadsend.
    Code optimizers aim to reduce the runtime of the compiled code by reducing its size and making other changes that can reduce the execution time with the goal of improving performance. The nature of the PHP compiler is such that there are often opportunities for code optimization, and an example of a code optimizer is the eAccelerator PHP extension.
    Another approach for reducing overhead for high load PHP servers is using an Opcode cache. Opcode caches work by caching the compiled form of a PHP script (opcodes) in shared memory to avoid the overhead of parsing and compiling the code every time the script runs. An opcode cache, APC, will be built into PHP 6. Opcode caching is also available in Zend Server Community Edition.

    Sorry for my long reply, but I like to make the picture as complete as possible. So I hope for the feedback and OFCOURSE THIS NEW PROJECT/MODULE.

    Is this an official project already? If not, how?

    Thansk & good luck!

    scroogie’s picture

    ClearXSClearXS, this is not the place to discuss this, but I can understand how you got mislead by the title. Those links you pasted are about technologies that need to be configured on the server site. If you know what you're doing, Drupal can be very fast. Of course you need decent hardware and some knowledge, but the same holds for every other major software product. Of course, you don't get a Porsche if you pay for a Citroën 2CV, but c'est la vie.

    The core developers spend a LOT of time on performance optimizations. Plus, there are a lot of contrib projects that deal with performance. Have a look at some of the performance projects: http://drupal.org/taxonomy/term/123
    Especially the following ones

    http://drupal.org/project/Cacherouter
    http://drupal.org/project/apc
    http://drupal.org/project/memcache
    http://drupal.org/project/fastpath_fscache

    First, what you should do is identify your problem. First step should be to find out if it's the time PHP spends on the code or the database connections and querying thats pulling your site down. Second step is to invest some time to learn about possible optimizations.

    ClearXS’s picture

    Many thanks! Indeed, it wasn't the proper place, but now this page is one of the most complete; so many others and I will easily get to this info again by a Google or Drupal search. However, think I just found the proper page:
    Drupal caching, speed and performance http://drupal.org/node/326504
    If so; that is the place where all this sort of info should be centralized and edited into (anyone can edit there).

    markus_petrux’s picture

    Interesting news, I think: Facebook has recently open sourced a new PHP compiler:

    http://developers.facebook.com/news.php?blog=1&story=358

    PS: Sorry, if this is Off Topic here. :-/ I haven't found any better Drupal core issue to post this.

    alanburke’s picture

    sun’s picture

    Version: 8.0.x-dev » 8.1.x-dev

    Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

    Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

    Version: 8.1.x-dev » 8.2.x-dev

    Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

    Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

    Version: 8.2.x-dev » 8.3.x-dev

    Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

    Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

    Version: 8.3.x-dev » 8.4.x-dev

    Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

    Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

    Version: 8.4.x-dev » 8.5.x-dev

    Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

    Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

    Version: 8.5.x-dev » 8.6.x-dev

    Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

    Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

    Version: 8.6.x-dev » 8.8.x-dev

    Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

    Version: 8.8.x-dev » 8.9.x-dev

    Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

    Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

    Version: 8.9.x-dev » 9.2.x-dev

    Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

    Version: 9.2.x-dev » 9.3.x-dev

    Version: 9.3.x-dev » 9.4.x-dev

    Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

    Version: 9.4.x-dev » 9.5.x-dev

    Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

    smustgrave’s picture

    Status: Active » Postponed (maintainer needs more info)

    Wonder if this is still needed with D10?

    catch’s picture

    Status: Postponed (maintainer needs more info) » Closed (outdated)

    We're on the way towards deprecating procedural hooks, and everything else these days is OOP, so I think this can just be marked outdated. #3366083: [META] Hooks via attributes on service methods (hux style).