I noticed this as part of the patch I rolled at #2005970: In renderable arrays, #type should provide a #theme suggestion, similar to how drupal_prepare_form() works.
@Cottser suggested that I could spin it off into its own issue and get it profiled and possibly committed independently.
Basically drupal_render() does this:
// Get the children of the element, sorted by weight.
$children = element_children($elements, TRUE);
// Initialize this element's #children, unless a #pre_render callback already
// preset #children.
if (!isset($elements['#children'])) {
$elements['#children'] = '';
}
// Call the element's #theme function if it is set. Then any children of the
// element have to be rendered there. If the internal #render_children
// property is set, do not call the #theme function to prevent infinite
// recursion.
if (isset($elements['#theme']) && !isset($elements['#render_children'])) {
$elements['#children'] = theme($elements['#theme'], $elements);
}
// If #theme was not set and the element has children, render them now.
// This is the same process as drupal_render_children() but is inlined
// for speed.
if ($elements['#children'] === '') {
foreach ($children as $key) {
$elements['#children'] .= drupal_render($elements[$key]);
}
}
And doesn't use $children anywhere else. Calling element_children() like this is a waste of CPU for #theme renderable arrays.
Comments
Comment #1
thedavidmeister commentedComment #2
thedavidmeister commentedComment #3
thedavidmeister commentedRelated #2005970: In renderable arrays, #type should provide a #theme suggestion, similar to how drupal_prepare_form() works
Comment #4
star-szrWill profile this tonight, planning on testing 50 comments.
Comment #5
star-szrSeems like quite a nice performance boost, let's get this in! Great find @thedavidmeister.
Scenario: Node page as anonymous user with 50 threaded comments displayed. Bartik theme.
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=51afe99cebff9&...
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=51afe99cebff9&...
Comment #6
alexpottSo the thing is is that element_children makes changes to $elements as it passes in the the value by reference and does all sorts of sorting...
So in order to put this in we need proof that nothing needs the changes made by
element_children()to$elements.The green test run in #1 is probably not enough because not many of our tests actually test the order that things appear on the page.
Comment #7
star-szrAh, very good point @alexpott.
…and looking at this again, if nothing else needs the sorting, we could make this:
But this needs more research and testing.
Comment #8
thedavidmeister commentedI think if we had no theme functions, and only theme templates that would be proof enough as templates do not care about the order of child elements, right?
Comment #9
thedavidmeister commentedSo, this is basically postponed on (at least) #1757550: [Meta] Convert core theme functions to Twig templates
Comment #10
mgiffordMost of them are in now. What else should we be waiting on at this point?
Comment #11
tr commentedelement_children() no longer exists. See #2226621: Remove usage of element_info(), element_child() and element_children(). Deprecate element_info_property().
Comment #12
star-szrThe code still exists, just both in classes now.
Comment #13
star-szrTried to change the title, trying again.
Comment #14
tr commentedFYI the functions element_child() and element_children() are completely gone from core, it is NOT just the usage that was removed. There's no change notice about this, so you have to grep the code base to confirm, or you have to have code that uses them which was completely broken when the core change was pushed ... :-(
Comment #15
joelpittetThis is the change notice for the deprecation:
https://www.drupal.org/node/2173683
Here's the removal:
https://www.drupal.org/node/2443805
Which probably needed it's own change record or something?
Comment #16
tr commentedYeah, major API changes pushed without a change record constitutes sociopathic behavior IMO. In particular anything theme-related, since the Twig stuff is cached on disk so a change like this brings a D8 site down hard (yes, I can turn off this caching, but sometime I forget when I'm doing say the 10th core re-install of the week). I know we don't support beta-to-beta upgrades yet, but really how hard is it to give fair warning that pulling a core commit will require a core reinstall?
Anyway, sorry for the off-topic rant, just wanted to give notice to anyone working on this issue that element_children() is no longer around.
Comment #17
joelpittetI agree it should've had a CR and it still could. Maybe you can write up the CR on that issue that removed it and we can still publish it so people are aware.
I'm sure the change was made with the best of intentions, many function removals are to allow for proper unit testing. And a bit of a battle between beurocracy vs the pressure to deliver makes it easy to have slips in some areas and CRs and documentation are in need of help as they are the first to be forgotten. So hopefully you can help out in this area?
Comment #18
joelpittetMaybe we can do this for 8.1.x
Comment #24
markhalliwellAlso, this issue shouldn't need a CR as it should be a purely internal change to optimize performance (not changing any logic).
Comment #26
MerryHamster commented8.7.x branch has many changes. Changes for this patch now are here core/lib/Drupal/Core/Render/Renderer.php:448
and if I correct understand we don't need this issue anymore.
Comment #27
jofitzRe-rolled.
Comment #36
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #37
nikhil_110 commentedAttached patch against Drupal 10.1.x
Patch #27 is not applied for Drupal 10.1.x so Inter-diff file is not added.
Comment #38
nikhil_110 commentedComment #39
smustgrave commentedThis feels more like a feature request to me. Or something that should least have test coverage.
This was profiled 10 years ago but with the new system wonder if it should be profiled again?