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

thedavidmeister’s picture

Status: Active » Needs review
StatusFileSize
new1.16 KB
thedavidmeister’s picture

Issue tags: +needs profiling
star-szr’s picture

Assigned: Unassigned » star-szr

Will profile this tonight, planning on testing 50 comments.

star-szr’s picture

Assigned: star-szr » Unassigned
Status: Needs review » Reviewed & tested by the community
Issue tags: -needs profiling

Seems 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.

=== 8.x..8.x compared (51afe99cebff9..51afeaa2e9325):

ct  : 233,757|233,757|0|0.0%
wt  : 985,069|984,888|-181|-0.0%
cpu : 953,525|953,873|348|0.0%
mu  : 33,683,624|33,683,624|0|0.0%
pmu : 34,205,392|34,205,392|0|0.0%

http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=51afe99cebff9&...

=== 8.x..lazy_children-2012800-1 compared (51afe99cebff9..51afeb006b56a):

ct  : 233,757|232,845|-912|-0.4%
wt  : 985,069|981,646|-3,423|-0.3%
cpu : 953,525|948,915|-4,610|-0.5%
mu  : 33,683,624|33,682,864|-760|-0.0%
pmu : 34,205,392|34,192,296|-13,096|-0.0%

http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=51afe99cebff9&...

alexpott’s picture

Status: Reviewed & tested by the community » Needs review

So 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.

star-szr’s picture

Ah, very good point @alexpott.

…and looking at this again, if nothing else needs the sorting, we could make this:

foreach (element_children($elements, TRUE) as $key) {
  $elements['#children'] .= drupal_render($elements[$key]);
}

But this needs more research and testing.

thedavidmeister’s picture

Status: Needs review » Needs work

I 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?

thedavidmeister’s picture

Status: Needs work » Postponed

So, this is basically postponed on (at least) #1757550: [Meta] Convert core theme functions to Twig templates

mgifford’s picture

Issue summary: View changes

Most of them are in now. What else should we be waiting on at this point?

tr’s picture

star-szr’s picture

The code still exists, just both in classes now.

star-szr’s picture

Title: drupal_render() could be lazier about calling element_children() » \Drupal\Core\Render\Renderer::doRender() could be lazier about calling Element::children()

Tried to change the title, trying again.

tr’s picture

FYI 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 ... :-(

joelpittet’s picture

This 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?

tr’s picture

Yeah, 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.

joelpittet’s picture

I 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?

joelpittet’s picture

Version: 8.0.x-dev » 8.1.x-dev
Status: Postponed » Needs review
Issue tags: +Needs change record

Maybe we can do this for 8.1.x

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

markhalliwell’s picture

Status: Needs review » Needs work
Issue tags: -Needs change record +Needs reroll

Also, this issue shouldn't need a CR as it should be a purely internal change to optimize performance (not changing any logic).

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

MerryHamster’s picture

8.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.

jofitz’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new1.17 KB

Re-rolled.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new144 bytes

The 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.

nikhil_110’s picture

StatusFileSize
new2.56 KB

Attached patch against Drupal 10.1.x

Patch #27 is not applied for Drupal 10.1.x so Inter-diff file is not added.

Checking patch core/lib/Drupal/Core/Render/Renderer.php...
error: while searching for:
drupal_process_states($elements);
}

// 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'])) {

error: patch failed: core/lib/Drupal/Core/Render/Renderer.php:404
error: core/lib/Drupal/Core/Render/Renderer.php: patch does not apply

nikhil_110’s picture

Status: Needs work » Needs review
smustgrave’s picture

Category: Task » Feature request
Status: Needs review » Needs work
Issue tags: +Needs issue summary update, +needs profiling, +Needs Review Queue Initiative

This 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?

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.