module_implements($hook, $sort = FALSE, $reset = FALSE) uses an internal cache (based on drupal_static_fast as well as cache_get()). The $sort argument is not used in the cache key, so when a cached result is returned, the sorting does not reflect $sort in the current call but its value when the cache entry was generated.
This patch makes module_implements() sort the module list itself rather than relying on the $sort argument for module_list(). I don't expect this to have a big performance impact. In core, module_implements() is only called with $sort == TRUE by help module (and even there this seems redundant, because the help module sorts the modules again). In D8 we may consider removing the $sort argument altogether.
Comment | File | Size | Author |
---|---|---|---|
#20 | module-implements-sort-9.patch | 8.37 KB | c960657 |
#17 | module-implements-sort-8.patch | 8.35 KB | c960657 |
#16 | module-implements-sort-7.patch | 8.13 KB | c960657 |
#12 | module-implements-sort-6.patch | 8.22 KB | c960657 |
#6 | module-implements-sort-5.patch | 11.88 KB | c960657 |
Comments
Comment #1
c960657 CreditAttribution: c960657 commentedThis patch also fixed the bug/oddity that hook_module_implements_alter() is invoked even if when $sort == TRUE, i.e. when the caller requests the modules to be returned in alphabetical order.
Comment #2
c960657 CreditAttribution: c960657 commentedReroll.
Comment #3
c960657 CreditAttribution: c960657 commentedReroll.
Comment #4
c960657 CreditAttribution: c960657 commentedComment #5
mikl CreditAttribution: mikl commentedLooks good to me. I think this should be merged for D7 (as it fixes a bug in the API).
For D8, however, I would be more in favour of removing the $sort option. It's apparently not widely used (as this bug has gone unnoticed), and there's no advantage to doing sort() in module_implements() rather than the calling code.
Comment #6
c960657 CreditAttribution: c960657 commentedGood point. I agree with mikl that $sort is redundant. The function was called with $sort == TRUE twice in core (in the Help module), but in both cases the sorting was irrelevant.
This patch removed the $sort argument from module_implements() as well as module_list().
Comment #7
mikl CreditAttribution: mikl commented+1 for cleaner APIs. Works as advertised.
Comment #8
sunGood catch. Looks ready to fly for me.
Comment #9
c960657 CreditAttribution: c960657 commentedThe patch in #4 applies to D7.
Comment #10
catch#6: module-implements-sort-5.patch queued for re-testing.
Comment #12
c960657 CreditAttribution: c960657 commentedReroll (due to #577950: API-cleanup: Factor module_implements $refresh parameter into it's own function).
Comment #14
sunBased on the patch size, looks like you missed a hunk?
Comment #15
c960657 CreditAttribution: c960657 commentedI hope not. But some of the changes were made redundant by the introduction of the new module_implements_reset() in #577950: API-cleanup: Factor module_implements $refresh parameter into it's own function.
Comment #16
c960657 CreditAttribution: c960657 commentedUpdated D8 patch. The patch in #4 applies to D7.
Comment #17
c960657 CreditAttribution: c960657 commentedReroll after #22336: Move all core Drupal files under a /core folder to improve usability and upgrades.
Comment #18
c960657 CreditAttribution: c960657 commented#17: module-implements-sort-8.patch queued for re-testing.
Comment #20
c960657 CreditAttribution: c960657 commentedReroll.
Comment #21
bojanz CreditAttribution: bojanz commentedSorry, it seems I've just duplicated some of your work (I blame the airplane non-connectivity).
I'm trying to fix the disabled modules situation and ran into the absolute mess that module_list() is.
Provided #1503224: Cleanup module_list() that tries to do a more complete cleanup, removes the $sort parameter like your patch does, and addresses some additional points.
So if the approach I took looks fine, maybe we can continue there? We'll add a note to the maintainers that you should get commit credit as well.
Comment #22
c960657 CreditAttribution: c960657 commentedSure, let's continue in #1503224: Cleanup module_list().