follow-up to: #254491: Standardize static caching

see last patch examples: http://drupal.org/node/254491#comment-1430180 and also: http://drupal.org/node/224333#static_variable_api

Apply this conversion pattern to includes/theme.inc, unicode.inc, and xmlrpc.inc to convert all static variables there. Pay close attention to any place a reset parameter is provided and add a call to drupal_static_reset() where appropriate (e.g. in any calling function that uses the reset parameter)

Comments

pwolanin’s picture

Per discussion w/ catch - if you have a variable name more complex than just __FUNCTION__, use a ':' to separate the suffix (this will avoid colliding with any other valid function name). e.g. :

&drupal_static(__FUNCTION__ . ':second_var');
JamesAn’s picture

Status: Active » Needs review
StatusFileSize
new3.96 KB

Here we go.

Status: Needs review » Needs work

The last submitted patch failed testing.

JamesAn’s picture

Status: Needs work » Needs review
StatusFileSize
new4.59 KB

Oops. Missed a call to reset list_themes(). The tests should now pass.

dries’s picture

Status: Needs review » Needs work

Please do not remove $reset/$refresh parameters and only call drupal_static_reset() from test functions.

cwgordon7’s picture

I'm confused by Dries' comment in #5 - why are $reset/$refresh parameters necessary now that we can use drupal_static_reset()? All the extra parameters in arbitrary order indicate bad code design and bad developer experience, so eliminating them should be a good thing. Also, I see nothing in the docs at http://drupal.org/node/224333#static_variable_api or in the original issue to indicate that drupal_static_reset() should only be called from within test functions, and http://api.drupal.org/api/function/drupal_static_reset/7 also shows that the function is called throughout core, not just in the testing system. Furthermore, drupal_static_reset() is a common API and I think that developers should be encouraged to use it rather than calling functions with an arbitrary list of parameters. Please explain your reasoning here.

berdir’s picture

@cwgordon7
See #422370-17: Revisit and convert mail.inc, module.inc, pager.inc to use static caching API and the following comments. In short, Dries thinks we shouldn't use them, many others (me included) think we should.

My arguments *for* using drupal_static_reset() are:
- drupal_static_reset() cannot do more than a $reset parameter, it can only clear the static cache. If more complex stuff needs to happen, function x can re-initialize it's stuff when the static cache has the default value.
- We have currently different ways to pass a reset flag for example an optional argument $reset (on the first, second, third, n-th position) or a key 'type' set to 'reset' in a $options array. With drupal_static_reset() there is only one single way to reset something: drupal_static_reset('function_name')
- Many functions will automatically regenerate the data when the reset flag is true, for example module_list() and node_get_types(). However, this is often unecessary, we may just want to delete the static cache and only have it loaded again when someone tries to access it.

Another thing, Dries argument "It's really messy for one function to mess with another function's innards" is not a argument against drupal_static_reset() (because that can't do anything except resetting), but against drupal_static() itself, which can very well do that. drupal_static() can actually do *much*. For example, it would allow contrib modules to provide different caching mechanisms for drupal_lookup_path() and similiar functions *without* the need for dynamic includes, handlers* and so on. Simply by inserting data into the internal static variable (in hook_init, for example) and loading & storing the updated data again in hook_exit. AFAIK there are some functions that allow that kind of stuff with another type of special arguments , that is another thing we can remove in favor of drupal_static(). Of course, this could also lead to very unexpected behavior...

* I'm not saying that we don't need them...

cwgordon7’s picture

Heh, I even read and commented on that issue... should have remembered that. However, it appears that after Dries' comment in #17, the issue was discussed, and the general consensus appears to have been that using drupal_static_reset() is fine.

JamesAn’s picture

I thought using drupal_static_reset() was the agreement as well. Maybe we need more discussion about the static caching API and how to handle resetting these vars?

pwolanin’s picture

Status: Needs work » Needs review

the refresh param of function theme_get_setting() is never used in core - so removing it is appropriate.

The replacement for the refresh param for list_themes() at the one point where it is used in core with a call to drupal_static_reset('list_themes'); is totally appropriate because we don't need the data, we just want to insure that any future calls do not get stale data.

Status: Needs review » Needs work

The last submitted patch failed testing.

JamesAn’s picture

Status: Needs work » Needs review
StatusFileSize
new4.95 KB

Rolled patch against latest version of D7.
The patch failed against #369409: Move block theme definition from system module to block module, which moved template_preprocess_block() from theme.inc to block.module.

Should work now.

JamesAn’s picture

In system.admin.inc, should we create a list_themes_reset() to wrap up drupal_static_reset('list_themes')?

-  list_themes(TRUE);
+  drupal_static_reset('list_themes');

Status: Needs review » Needs work

The last submitted patch failed testing.

JamesAn’s picture

Status: Needs work » Needs review
StatusFileSize
new4.32 KB

Not sure why it failed, but I've rerolled it against HEAD.

pwolanin’s picture

We shoudl not have pointless wrapper functions - only if we are providing the function as part of the API

JamesAn’s picture

StatusFileSize
new6.92 KB

Ok. I wasn't sure where to draw the line between documenting drupal_static_reset use with wrapper functions and avoiding verbose (and, due to the increased function calls, arguably less efficient) code.

This patch includes a few comment changes. I forgot to remove an @param directive for the $refresh var that this patch removes. The rest of the changes are additions and removals of blank lines within the function comment blocks. A couple of the comment blocks lacked a blank line between the function description and the @param/@return directives. Others included a blank line between the group of @param and the group of @return directives; most other comment blocks have no blank line separating them.

Status: Needs review » Needs work

The last submitted patch failed testing.

JamesAn’s picture

Status: Needs work » Needs review
StatusFileSize
new6.92 KB

Rerolled.

Status: Needs review » Needs work

The last submitted patch failed testing.

Status: Needs work » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.