Blocklib and block objects instantiated twice...

Blocklib and block objects instantiated twice...

Martín Langhoff -
回帖数:4
I am reading the Blocks dev notes (1) and the code for existing blocks. I have to confess, I've never been too deep in the blocks code so far, and writing a custom block for 1.5 I found something very strange. The get_content() method is meant to be called twice: once to check that there is content to print, and a 2nd one to actually display it. Strangely, the blocklib framework currently doesn't do any caching or memoizing of the results. It is up to the block developer to do it.

This would require, however, that the instantiated blocks are kept around (cached in memory indeed) between the first invocation of get_content() and the second one... and apparently they aren't. Each block is being instantiated twice, and running all its db queries twice too 糊涂

The $pageblocks global that could (should) be holding the block instances is holding something else instead... it seems that it only has block data from the DB.

I am looking deeper into it, but if anyone has been working on the blocks code and can shed some light, it'll be much appreciated. Where is Jon when we need him!?

1 - Entry for get_content() method here
http://docs.moodle.org/en/Blocks_Howto#Methods_you_can_freely_use_and_override
回复Martín Langhoff

Re: Blocklib and block objects instantiated twice...

Martín Langhoff -
Ok -- I have a fix that I think I will be landing in MOODLE_16_STABLE and HEAD. Testing indicates that it is halving the 10~20 DB queries in the homepage are related to blocks. It is here:
http://git.catalyst.net.nz/gitweb?p=moodle.git;a=commitdiff;h=3c569323a95a059900f1df7fdc5c42d62c112bf6;hp=9e2e45c6ef7cf8cab6b0efe8cbe173db8479f6fb

For HEAD I would like to change the API slightly, to remove the 'get_content() may be called twice' oddity. But won't do that until I hear a 2nd opinion on the matter...

Blocklib can just cache the output which means simpler code, less memory, less processing. But if any block is counting on the side-effects if get_content() sometimes being called twice... 伤心
回复Martín Langhoff

Re: Blocklib and block objects instantiated twice...

Martin Dougiamas -
Core developers的头像 Documentation writers的头像 Moodle HQ的头像 Particularly helpful Moodlers的头像 Plugin developers的头像 Testers的头像
Awesome, yes please, check the first fix in.

Good catch!

As for changing the Blocks API in 1.7, let's make sure that's ABSOLUTELY NECESSARY first.
回复Martin Dougiamas

Re: Blocklib and block objects instantiated twice...

Martín Langhoff -
Merged in both HEAD and MOODLE_16_STABLE. In terms of API changes... well, it's not absolutely necessary, but current documentation seems to imply that it's something that the modules have to cope with, rather than something to take advantage of... and I agree with that documentation! 眨眼
回复Martín Langhoff

Re: Blocklib and block objects instantiated twice...

John Papaioannou -
Where is Jon when we need him!?

Greece has conscription, so Jon is currently (and unless I come upon an unexpected mountain of luck, until Jan 2007) out of the loop for all practical purposes, helping run a military post office of all things.

It's possible that the objects were being instantiated only once in the past and this behavior "broke" at some point without anyone noticing. I really can't help more than that, and for that I 'm more sorry than you can imagine Martin... sad

Oh well.