Updated: Comment #7
Problem/Motivation
When using threaded comments, template_preprocess_comment() in comment.module loads the parent comment when rendering every comment in the thread.This duplicates a lot of the preparation of the comment variables that's already been done when rendering the actual parent comment, assuming it's done on the same page. That's likely to be a noticeable regression when you have a lot of threaded comments on a page, for example on groups.drupal.org. We should look at finding way to re-use the variables created for the parent comment I think.
Proposed resolution
- create a repeatable test case (generating a number of threaded comments)
- provide an initial xhprof profiling run for benchmarking
- look at finding way to re-use the variables created for rendering the parent comment
- review the variables which get created in this preprocess and remove any which are not used in the template
Remaining tasks
Everything.
Related Issues
- Part of #1744302: [meta] Resolve known performance regressions in Drupal 8
- Follow up of #1272870: No semantics for nested comments / bad for screen-readers
Original report by catch
Follow-up from [#1272870:79]
+ if ($comment->pid > 0) { + // Fetch and store the parent comment information for use in templates. + $comment_parent = comment_load($comment->pid); + $variables['parent_comment'] = $comment_parent; + $variables['parent_author'] = theme('username', array('account' => $comment_parent)); + $variables['parent_created'] = format_date($comment_parent->created); + $variables['parent_changed'] = format_date($comment_parent->changed); + $uri_parent = $comment_parent->uri(); + $uri_parent['options'] += array('attributes' => array('class' => 'permalink', 'rel' => 'bookmark')); + $variables['parent_title'] = l($comment_parent->subject, $uri_parent['path'], $uri_parent['options']); + $variables['parent_permalink'] = l(t('Parent permalink'), $uri_parent['path'], $uri_parent['options']); + $variables['parent'] = t('In reply to !parent_title by !parent_username', +This means that if you're using threaded comments, then we're going to duplicate a lot of the preparation of the comment variables (format_date() twice, theme_username() for example), that's already been done when rendering the actual parent comment, assuming it's done on the same page. That's likely to be a noticeable regression when you have a lot of threaded comments on a page, for example on groups.drupal.org. We should look at finding way to re-use the variables created for the parent comment I think.
Also there are variables created here that never get used? Can we just remove those please?
| Comment | File | Size | Author |
|---|---|---|---|
| #82 | reroll_diff_64-82.txt | 9.13 KB | pooja saraah |
| #82 | 1857946-82.patch | 5.46 KB | pooja saraah |
| #64 | 1857946-64.patch | 7.23 KB | rpayanm |
Comments
Comment #1
fagoWe are not only duplicating the parent template variables, we are loading the parents twice also. If the parent is rendered on the same page, it is still loaded again when rendering its children as comments have static entity load caches turned off.
Comment #2
mgiffordThis can be fixed after feature freeze I assume.
Comment #3
berdirThe loading problem is being fixed in #1844146: comment_load() call in template_preprocess_comment() re-loads comment parent entities because of disabled static cache.
This is different. What @catch means is that for example if you have a parent comment with 100 sub-comments, then we are calling "format_date($comment_parent->created)" over and over again for the parent comment for every comment below it.
Comment #4
dcam commentedhttp://drupal.org/node/1427826 contains instructions for updating the issue summary with the summary template.
The summary may need to be updated with information from comments.
Comment #5
gábor hojtsyHow is this a major performance problem? (vs. a normal one).
Comment #6
catchIt was major with the double loading of comments (actually more like critical), but I think it's probably 'normal' now. There's situations with threaded comments where it'll still be a significant regression but that requires threaded comments with lots of replies to the same comment.
Comment #7
tsphethean commentedUpdated issue summary, needs profiling.
Comment #7.0
tsphethean commentedUpdated issue summary.
Comment #7.1
tsphethean commentedUpdated issue summary.
Comment #8
tsphethean commentedI was taking a look at this and was thinking we could solve it with a static variable containing all the parent entities loaded in the page request, and checking for it in the static before loading it - does that sound a sensible way forward or do we need to be looking at something higher up the call stack?
Comment #8.0
tsphethean commentedUpdated issue summary.
Comment #9
andypostComment #10
andypost1)
Comment::hasParentComment()should not load parent entity.2) Suppose removing "parent" is not possible without breaking following "accessibility":
If we get rid of this line then parent entity could not be loaded,
but the same time when thread of comments is rendering parent comment already loaded so fine.
At least better to minimize a defaults for "parent"
Comment #11
mgiffordIs there another way to do this? What if the link is just to the parent, without actually defining the parent title?
It would be important to have some semantic relationship, but maybe we could do a simpler one so that comment template variables aren't built twice...
Comment #12
mgifforddoesn't apply.
Comment #13
mgiffordComment #14
andypostAt least this part should be commited
Comment #15
mgiffordhasParentComment() looks fairly straight forward, but then so do the changes to comment.html.twig.
Would it help if I rolled a patch just for everything but the changes to comment.module?
Comment #16
andypostSure!
I just think that better to limit variables related to parent comment.
If contrib will need more vars it's easy to add them and anyway will need to change template
Comment #17
andypostThat means:
1) there's
comment_parent: Full parent comment entity (if any).variable already2) a12y needs "parent" (last patch forgets to translate this
3) templates can access to parent comment properties via
comment_parent.subjectso no reason to prepare so much useless variables.Comment #18
mgiffordI don't think it needs more variables, but I'm pretty certain that the inline docs in comment.html.twig were wrong.
No new variables have been added in this patch. Thanks for pointing out the missing internationalization.
I didn't touch on anything by comment_parent.subject, but right now it seems to be working.
Comment #19
andypostMike, great! now we should remove the same string from preprocess.
Also
s/entity/commented_entity- fix wrong name in templateComment #20
mgiffordI don't know why this seems to be happening, but I'm not seeing
<p class="comment-parent visually-hidden">...</p>in #19 but do in #18.I'm not sure why this would be but I spun up to instances to compare and they both had the same issue. Why would parent_comment be true in one and not the other?
EDIT: Example (for the next 3 hrs) - http://sa5dd4e9d2ca61c1.s2.simplytest.me/node/1
Comment #21
andypost@mgifford You was right! I missed to change bartik's template
Comment #22
dawehnerGreat fixes here!
We afaik use single quotes also for strings in templates
Comment #23
andypostsure, now that needs profiling, suppose the main difference would be caused by
that code
Comment #24
berdirI actually just opened #2328629: Comment::hasParentComment() tries to load comment with ID 0 today, with a similar change for that method.
Comment #25
andypostproperly merged head and incorporate #24
Comment #26
andypostProfiling of node with 50 threaded comments

Comment #27
andypostre-roll
Comment #28
andypostLooks like duplicated issue #2002094: Improve performance of comment.html.twig
Comment #29
mgiffordSeems to work fine in my testing on SimplyTest.me
I created environment, added a node, then proceeded to add comments and child comments.
The code comments look like good additions too. Think this is ready for Core.
Comment #30
alexpottI'm not sure that the solution fully addresses @catch's original concerns about calling format_date and drupal_render($username) multiple times on the same parent comment.
Comment #31
andypostthe best we can do is to remove 'parent_author' from prepare, but this causes an a11y regression
Comment #32
andypostduplicated commentComment #33
mgiffordCould we just add a static variable so that format_date and drupal_render($username) are stored rather than re-generated for comments?
Comment #34
catchWhat about removing all the variables that aren't used from the preprocess?
Comment #35
andypost@catch they are all used, see #16-17
Comment #36
andypostHas separate issue #2010196: Rename comment parent theme variable
Comment #41
mgiffordComment #42
rpayanmComment #44
smashwini commentedComment #45
smashwini commentedRerolled patch with correction of comment indentation.
Comment #49
gauravjeet commentedI removed a major chunk of code from the patch, and found the comments to be working fine for me. There is proper indentation for child comments and the parent comment seems to be fine. No need for this code in the comment.module file.
Please reply, if I'm on the wrong track
Comment #51
smashwini commentedComment #52
smashwini commentedComment #53
andypostSuppose it ready
Comment #54
rahul.shindeComment #55
star-szrOn my phone but I am pretty sure this misses updating Classy's comment template.
Comment #56
rpayanmComment #57
andypost@Cottser please take a look at related #2031883: Markup for: comment.html.twig
indent problem (seems tab)
Comment #58
rpayanmComment #59
andypostI'm going to organize all this render/performance issues in one meta.
Comments are improved in #2031883-34: Markup for: comment.html.twig
Also bigger performance gain in #2318579-22: Remove comment_prepare_thread()
RTBC, because this is a most independent issue
Comment #61
andypostComment #62
kerby70 commentedReroll attached, just 1 fuzz and 2 line shift, no real changes.
Comment #64
rpayanmComment #65
andypostgreat!
Comment #66
alexpottSo the patch and the issue summary and title seem completely at odds - what am I missing?
Comment #67
andypostComment #74
andypostComment #81
larowlanAdding some tags
Comment #82
pooja saraah commentedAttached patch against Drupal 9.5.x
Attached reroll patch
Comment #83
pooja saraah commentedComment #84
smustgrave commentedLast patch had some failures.
Also issue summary update was requested and still needs to happen it appears.