Updated: Comment #4
Problem/Motivation
The function mailsystem_get_classes()
is called on every page load. And in the most cases it's not necessary to rebuild the data as they don't change often and "unfortunately" the executed query is complex and takes its time. This unnecessarily slows down the page execution time.
Proposed resolution
Introduce the usage of a persistent cache. If you use the DB as cache it won't save you a huge amount of time but as soon as you use an in-memory cache backend like memcache or redis the gain will be essential as you definitely save queries to the db.
Remaining tasks
Reviews needed.
User interface changes
none.
API changes
none.
Original report by @denny84
I am attaching image showing devel output for CK 2.2 Its very slow on my dual core AMD zend server. I even turned on redis cache, but still no improvements. Any advice would be great. Thanks.
Comment | File | Size | Author |
---|---|---|---|
#16 | Screen shot 2014-09-15 at 22.40.08.png | 51.54 KB | apotek |
#13 | mailsystem-use-caching-for-mailsystem_get_classes-1905544-13.patch | 2.24 KB | apotek |
#4 | mailsystem-use-caching-for-mailsystem_get_classes-1905544-4.patch | 1.66 KB | das-peter |
commerce_devel.jpg | 161.65 KB | denny84 |
Comments
Comment #1
denny84 CreditAttribution: denny84 commentedChanging category to Support Request.
Comment #2
jsacksick CreditAttribution: jsacksick commentedThis seems to be an issue related to mailsystem, not to Kickstart...
Comment #3
dahousecat CreditAttribution: dahousecat commentedIt seems slow on my system too so I've just added some caching into the mailsystem_get_classes function.
So now the top of the function looks like this:
And added this under the ksort:
So the whole function looks like this: http://pastebin.com/DLHZr2eS
Comment #4
das-peter CreditAttribution: das-peter commentedSame issue here. The patch attached introduces a persistent cache similar to what @dahousecat proposed.
Also added a cache clear in
mailsystem_create_class()
.Issue summary updated.
Comment #5
botrisconfirm this to work.
Comment #6
plachWorking well here.
Comment #7
erikwebb CreditAttribution: erikwebb commentedI can confirm the same diagnosis here. There's no reason for this to be called on every page load. I'm not even sure it needs to be called in hook_init() at all, but that's probably worth a separate issue.
Comment #8
david_garcia CreditAttribution: david_garcia commentedWorking +1
Comment #9
erikwebb CreditAttribution: erikwebb commentedComment #10
Fabianx CreditAttribution: Fabianx commentedRTBC +1
Another high profile site that uses this in production, saves 100ms here ...
Comment #11
apotek CreditAttribution: apotek commentedI think there's a bug in the patch. The bug does not keep the patch from working, or from being an improvement, but rather simply invalidates the point of the static cache.
I believe this line
should be like this:
$mailsystem_get_classes variable is never declared in the preceding function code so the first evaluation will always be TRUE. So this "works" but you don't get the real benefit of the statically cached variable.
@erikwebb
I have to say I agree. I don't see why this has to happen on every _init(). Say you just want to run a drush command like drush sql-cli etc. Why should this mailsystem_get_classes() be fired for that? In general, my rule of thumb is if you think you have to use hook_init(), you probably shouldn't :).
Comment #12
apotek CreditAttribution: apotek commentedSetting to needs work to address the misnamed variable.
Comment #13
apotek CreditAttribution: apotek commentedRevised patch according to observations in comment 12.
Even though 7.x-3.x doesn't use the hook_init(), it might still benefit from this patch so it might want to be ported once approved and commited to the released version 7.x-2.x.
Comment #14
Fabianx CreditAttribution: Fabianx commentedBack to RTBC
Comment #15
apotek CreditAttribution: apotek commentedDeployed to hugely heavily loaded production server and saw an immediate and clear performance increase on the database side. The SELECT from registry query targeted by this patch used to represent 37% of our database server busyness. It is now down to 13%.
RTBC +1.
Comment #16
apotek CreditAttribution: apotek commentedAttaching screenshot to show huge improvement due to this patch.
Comment #17
mikeytown2 CreditAttribution: mikeytown2 commentedPatch works! +1 RTBC
Comment #18
dwkitchen CreditAttribution: dwkitchen commentedAgreed, tested this. After clearing out some other issues this was regularly appearing as the slowest query on every page load.
Comment #19
apotek CreditAttribution: apotek commentedWhat are the remaining steps to get this patch committed? Do maintainers have any concerns here?
Comment #20
Fabianx CreditAttribution: Fabianx commentedMoving to critical as the performance suffering is real enough to make this critical.
Comment #21
Dmitriy.trt CreditAttribution: Dmitriy.trt commentedProposed solution should work, but why do we need
mailsystem_get_classes()
call frommailsystem_init()
on every request in general? Class registry is re-built on every cache reset (database update, module list updates, etc) and Mailsystem module manually parses the file on new class creation. So, are there any reasons for this call here? Why not just remove this call and get speed-up even for websites with DB cache?Comment #22
apotek CreditAttribution: apotek commented@Dmitriy.trt
I agree. It should be removed. But doing so might affect other things. It's more important to fix the immediate issue, and then open a second issue for whether there should be any calls in hook_init().
This has been marked critical for 4 months and has a patch that is RTBC and ready to go. I see nothing holding this back and it would better to get this into the code and then look at the overall boostrapping picture with regards to this module.
Comment #24
Nafes CreditAttribution: Nafes as a volunteer commentedCommitted. Thank you for the patch, apotek!
Comment #25
kylebrowning CreditAttribution: kylebrowning commented#2558713: Mail system should not be using hook_init
Comment #27
Manuel Garcia CreditAttribution: Manuel Garcia as a volunteer and at Appnovation for Pfizer, Inc. commentedJust wanted to mention this proposal here, since its related to performance as well, it could use a review and people following this issue would probably be interested:
#2708415: Move mailsystem persistent cache in cache_bootstrap