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.