Problem/Motivation

Render Caching is great, but debugging asset / library adding and post_render cache can be really complicated, also it is difficult to see if there are cache hits or not.

In Drupal 7 render_cache module, I use twig style debug output, which works great.

Reference: https://github.com/LionsAd/render_cache/blob/770c615f6081e7d415dcae431ee...

Proposed resolution

Cache debug output

In drupal_render_cache_get() (soon in renderer service) add something like (pseudo-code):

  // _after_ get from cache
  if ($this->use_debug) {
     $elements = $this->addDebugOutput($elements, TRUE); // TRUE == Cache Hit
  }

  // _after_ set to cache
  if ($this->use_debug) {
     $elements = $this->addDebugOutput($elements, FALSE); // TRUE == Cache Miss
  }

  function addDebugOutput($elements, $cache_hit) {
        $prefix = '<!-- START RENDER (#pre_render): ' . print_r($elements['#pre_render'], TRUE) . "\n" . ' CACHE INFO: ' . "\n" . print_r($elements['#cache'], TRUE);
        $prefix .= "\nCACHE-HIT: " . $cache_hit ? 'YES' : 'NO' . "\n";
        $prefix .= "\nATTACHED: " . print_r($elements['#attached'], TRUE) . "\n";
        $prefix .= '-->';
        $suffix = '<!-- END RENDER: ' . print_r($elements['#pre_render'], TRUE) . ' -->';
        $elements['#markup'] = "\n$prefix\n" . $elements['#markup'] . "\n$suffix\n";
        return $elements;
  }

This is major, because it is almost impossible to debug render caching else. (I tried other ways ...)

The impact is minimal as when the option is turned off, there is no change in code.

Remaining tasks

- Make a patch!

User interface changes

- None

API changes

- New config option in services.yml to add render cache debug output.

Release notes snippet

A new developer feature to show debug markup for render caching has been added, this can be configured from services.yml and is disabled by default. Sites should ensure that their site-specific services.yml includes the new section.

CommentFileSizeAuthor
#104 interdiff-2381797-102-104.patch1.38 KBslashrsm
#104 2381797-104.patch9.22 KBslashrsm
#102 2381797-102.patch9.18 KBslashrsm
#102 interdiff-2381797-100-102.patch913 bytesslashrsm
#100 interdiff-2381797-95-100.patch1.05 KBslashrsm
#100 2381797-100.patch9.16 KBslashrsm
#95 interdiff-2381797-92-95.txt708 bytesyogeshmpawar
#95 2381797-95.patch9.12 KByogeshmpawar
#92 interdiff_2381797_85-92.txt1000 bytesankithashetty
#92 2381797-92.patch9.12 KBankithashetty
#85 interdiff-2381797-83-85.txt3.8 KBtobiasb
#85 2381797-85.patch9.03 KBtobiasb
#83 interdiff_2381797_81-83.txt623 bytesankithashetty
#83 2381797-83.patch8.51 KBankithashetty
#81 2381797-81.patch8.5 KBdhirendra.mishra
#81 interdiff_73-81.txt2.42 KBdhirendra.mishra
#73 interdiff_2381797-70-73.txt3.81 KBnevergone
#73 2381797-73.patch8.5 KBnevergone
#72 cache-debug-output.JPG95.35 KBkrzysztof domański
#70 interdiff_68_70.txt683 bytesanmolgoyal74
#70 2381797-70.patch7.26 KBanmolgoyal74
#68 2381797-68.patch7.08 KBkrzysztof domański
#68 interdiff-63-68.txt2.81 KBkrzysztof domański
#63 interdiff_62-63.txt78.22 KBjohnwebdev
#63 2381797-63.patch7.59 KBjohnwebdev
#62 interdiff_59-62.txt80.02 KBjohnwebdev
#62 2381797-62.patch85.93 KBjohnwebdev
#59 interdiff_57-59.txt1.33 KBjohnwebdev
#59 2381797-59.patch6.72 KBjohnwebdev
#57 interdiff_55-57.txt526 bytesjohnwebdev
#57 2381797-57.patch6.8 KBjohnwebdev
#55 2381797-55.patch6.17 KBjohnwebdev
#47 interdiff-2381797-45-47.txt1.49 KBchr.fritsch
#47 2381797-47.patch6.1 KBchr.fritsch
#45 interdiff-2381797-43-45.txt1.51 KBchr.fritsch
#45 2381797-45.patch6.1 KBchr.fritsch
#43 2381797-43.patch6.07 KBchr.fritsch
#41 interdiff-2381797-39-41.txt2.36 KBchr.fritsch
#41 2381797-41.patch5.49 KBchr.fritsch
#39 2381797-39.patch3.71 KBchr.fritsch
#17 add_render_cache_debug-2381797-17.patch19.73 KBtom verhaeghe
#17 interdiff-14-17.txt304 bytestom verhaeghe
#17 interdiff-17-2379741-21.txt5.11 KBtom verhaeghe
#14 add_render_cache_debug-2381797-14.patch19.58 KBtom verhaeghe
#11 add_render_cache_debug-2381797-11-do-not-test.patch19.58 KBtom verhaeghe
#11 interdiff-2379741-21.txt4.96 KBtom verhaeghe
#7 add_render_cache_debug-2381797-7-do-not-test.patch4.67 KBtom verhaeghe
#7 interdiff-3-7.txt6.81 KBtom verhaeghe
#3 add_render_cache_debug-2381797-3-do-not-test.patch4.76 KBtom verhaeghe
#20 interdiff-20-2379741-21.txt5.12 KBtom verhaeghe
#20 interdiff-17-20.txt5.12 KBtom verhaeghe
#20 add_render_cache_debug-2381797-20.patch19.75 KBtom verhaeghe

Comments

wim leers’s picture

I'd love this!

tom verhaeghe’s picture

Assigned: Unassigned » tom verhaeghe
tom verhaeghe’s picture

I added the config option 'cache_debug' in services.yml. I also attempted to print the debug data (I omitted cache_info for debugging reasons). When I receive a cache miss, everything works as expected, but when there's a cache hit it doesn't work as I hope it would.

I guess I miss something here...

tom verhaeghe’s picture

Status: Active » Needs review
wim leers’s picture

Status: Needs review » Needs work

Good progress here, thanks :)

  1. +++ b/core/lib/Drupal/Core/Render/Renderer.php
    @@ -67,6 +67,7 @@ public function renderRoot(&$elements) {
    +    $twig_service = \Drupal::service('twig');
    

    This service should be injected.

  2. +++ b/core/lib/Drupal/Core/Render/Renderer.php
    @@ -130,6 +131,12 @@ public function render(&$elements, $is_root_call = FALSE) {
    +          \Drupal::logger('render')->notice('Wrapping with cache debug data on cache GET.');
    

    This is just temporary debug output, while working on this, right? :)

  3. +++ b/core/lib/Drupal/Core/Render/Renderer.php
    @@ -316,6 +323,10 @@ public function render(&$elements, $is_root_call = FALSE) {
         if (isset($elements['#cache'])) {
           drupal_render_cache_set($elements['#markup'], $elements);
    +      if ($twig_service->isCacheDebug()) {
    +        \Drupal::logger('core')->notice('Wrapping with cache debug data on cache SET.');
    +        $elements = $this->addDebugOutput($elements, FALSE);
    +      }
    

    This is why the debug output is only appearing for you in case of a cache miss: this is the code path that deals with a cache miss and writing to the cache.

    You'll also want to call it in this if-test, where a render cache hit is handled:

        // Try to fetch the prerendered element from cache, run any
        // #post_render_cache callbacks and return the final markup.
        if (isset($elements['#cache'])) {
          $cached_element = drupal_render_cache_get($elements);
          if ($cached_element !== FALSE) {
    …
  4. +++ b/core/lib/Drupal/Core/Render/Renderer.php
    @@ -387,4 +398,22 @@ protected function processPostRenderCache(array &$elements) {
    +  }
     }
    

    Needs a newline between these two braces.

wim leers’s picture

+++ b/core/vendor/twig/twig/lib/Twig/Environment.php
@@ -94,11 +94,13 @@ public function __construct(Twig_LoaderInterface $loader = null, $options = arra
+            'cache_debug'         => false,

A matching default value in default.services.yml is still missing for this.

But… this actually doesn't belong in Twig. Twig is a theme engine. It's pluggable. Somebody could replace it.

This render cache debug output is independent of Twig. We probably want a renderer.config parameter in default.services.yml, just like we have twig.config. And in there, we'd then have a debug: false, just like for Twig.

tom verhaeghe’s picture

Status: Needs work » Needs review
StatusFileSize
new6.81 KB
new4.67 KB

Added a renderer.config array, injected it into the Renderer object and changed the way how the config variables are accessed.

Still stuck on where to put the $this->addDebugOutput code, it seems to never hit the cache and I don't know why ... need help :-)

Oh, and the addDebugOutput() method still needs work, i know :-)

wim leers’s picture

Status: Needs review » Needs work

I tested the patch and found a few important problems:

  1. if you have an structure like ['parent' => ['#type' => 'container', '#cache' => […], 'child' => ['#markup' => 'this is the child', '#cache' => […]]]], then the child element's cache debug output is render cached. We don't want that, because then the cache debug output is no longer reliable: it'll continue to say that the child is retrieved from cache (or not), despite the child not having been retrieved separately at all; it's only the parent that was retrieved from render cache, not the child!
  2. Secondly, the issue title specifically says "render cache debug output". I.e. it should only affect those parts of the render array that are intended to be cached. But in the current implementation, every single level of every render array always gets this debug output. That results in too much debug output. You'll want to only show debug output for those elements that may be render cached, basically if (isset($elements['#cache']['cid']) || isset($elements['#cache']['keys']).
fabianx’s picture

We might need to postpone that OR bring over the re-factoring of drupal_render_cache_set / drupal_render_cache_get to live in this class.

Wim, can you point to the issue where you had that conversion to saveToCache / retrieveFromCache, because I don't remember.

The debug output really belongs in those functions.

The rest of the patch looks good already. Thanks so much for working on this, Tom!

wim leers’s picture

can you point to the issue where you had that conversion to saveToCache / retrieveFromCache, because I don't remember.

The debug output really belongs in those functions.a

Agreed.

The patch you're looking for is at #2379741: Add Renderer::getCacheableRenderArray() to encapsulate which data is needed for caching a render array and have views use it.

tom verhaeghe’s picture

StatusFileSize
new4.96 KB
new19.58 KB

Applied patch #2379741: Add Renderer::getCacheableRenderArray() to encapsulate which data is needed for caching a render array and have views use it + rerolled parts of previous patch and printing cache tags and keys (see interdiff).

tom verhaeghe’s picture

Status: Needs work » Needs review
wim leers’s picture

Status: Needs review » Needs work

I've been pushing #2379741: Add Renderer::getCacheableRenderArray() to encapsulate which data is needed for caching a render array and have views use it further, to unblock this issue.

The patch in #11 that's marked as "do-not-test" should actually not be marked as "do-not-test". There should be a version of the patch that does NOT include #2379741, and THAT patch should be marked "do-not-test". Otherwise, we can't know if this is working or not :)

tom verhaeghe’s picture

Status: Needs work » Needs review
StatusFileSize
new19.58 KB

Okay, renamed patch to ensure it's tested.

Status: Needs review » Needs work

The last submitted patch, 14: add_render_cache_debug-2381797-14.patch, failed testing.

fabianx’s picture

The interdiff looks great on making usage of the new container configuration parameter :).

No idea why the last patch failed testing, but as soon as Wim's issue is in, this one will be simpler to review.

Already RTBC (fingers crossed)

tom verhaeghe’s picture

StatusFileSize
new19.73 KB
new304 bytes
new5.11 KB

Okay, found that I have to add the renderer.config default parameters in core.services.yml, so luckily it didn't have to do anything with Wim's issue.

tom verhaeghe’s picture

Status: Needs work » Needs review

The last submitted patch, 17: add_render_cache_debug-2381797-17.patch, failed testing.

tom verhaeghe’s picture

StatusFileSize
new19.75 KB
new5.12 KB
new5.12 KB

Forgot to add parameter 'debug' in core.services.yml which caused many tests to fail. I added debug: false but I'm not sure if this is the best place to put it there.

wim leers’s picture

Issue tags: +Needs tests

Reviewed the last interdiff, some nitpicks. But two more important, more fundamental challenges:

  1. This needs test coverage.
  2. Most importantly: I think the output is actually misleading and I'm not sure if there's a way to fix it… Imagine that you have a render array like this:
    $build = [
      '#markup' => 'parent',
      '#cache' => ['cid' => 'parent'],
      'child' => [
        '#markup' => 'child',
        '#cache' => ['cid' => 'child'],
      ],
    ];
    

    When rendered, the child is rendered first, is cached, and "cache miss" debug output is added to its #markup. No problems so far. Now we render the parent. It's also a cache miss. But its output contains the output of the child, which includes the "cache miss" debug output. This is misleading in two ways:
    1) cache hits for the parent imply that caching of the child simply doesn't matter, so the render cache debug output for the child should not be there at all, 2) if it's going to be there, it should reflect reality, and reality is that the child has been cached in the mean time. But now that the parent is cached, it's going to say forever that the child is not cached, even though it is, both individually and part as of its parent.
    The only mitigating factor is that you could decipher this on your own: HTML is hierarchical as well, so we could argue that when looking at the debug output, you should look at whether there are any parent DOM elements that have debug output, and if so, that output is the relevant output. The child's debug output then indicates whether the parent had to rebuild everything, or was able to reuse cached children or not.

Thoughts?


+++ b/core/lib/Drupal/Core/Render/Renderer.php
@@ -528,4 +546,40 @@ public function getCacheableRenderArray(&$markup, $elements) {
+  /**
+   * @param $elements
+   *   The renderable array that must be wrapped with the cache debug output.
+   * @param $is_cache_hit
+   *   A flag indicating that the cache is hit or miss.
+   * @return array
+   *   The renderable array.
+   */
  1. Missing one-line description at the top.
  2. Missing typehints.
  3. Needs a new line before the @return.
wim leers’s picture

Status: Needs review » Needs work

#2379741: Add Renderer::getCacheableRenderArray() to encapsulate which data is needed for caching a render array and have views use it finally landed, so now you can rip that part out of this patch; that'll make this patch easier to review and reroll :)

fabianx’s picture

tom verhaeghe’s picture

Yup, i'm going to pick this up really soon.

moshe weitzman’s picture

Crickets ... Anyone up for pushing this along?

fabianx’s picture

Assigned: tom verhaeghe » Unassigned

Lets unassign Tom for now :)

I agree it is important, it should also be really easy now.

The last submitted patch, 20: add_render_cache_debug-2381797-20.patch, failed testing.

andypost’s picture

Issue tags: -Novice

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

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should 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.

wim leers’s picture

Component: cache system » render system
wim leers’s picture

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

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should 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.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should 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.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should 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.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should 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.

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

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should 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.

chr.fritsch’s picture

Status: Needs work » Needs review
StatusFileSize
new3.71 KB

This is a reroll of #20. #21 is still not addressed.

Status: Needs review » Needs work

The last submitted patch, 39: 2381797-39.patch, failed testing. View results

chr.fritsch’s picture

Status: Needs work » Needs review
StatusFileSize
new5.49 KB
new2.36 KB

I fixed the failing tests and added a new one to check the debug output.

Regarding #21.2: I don't have a good answer on that. The only thing that came to my mind was changing the children to CACHE-HIT: YES by a regex after the parent was retrieved from the cache.

Status: Needs review » Needs work

The last submitted patch, 41: 2381797-41.patch, failed testing. View results

chr.fritsch’s picture

Status: Needs work » Needs review
StatusFileSize
new6.07 KB

Damn, patch wrong created...

alexpott’s picture

This looks really helpful. I spent some time pondering if this should be merged with twig's debug flag. Because it hard to explain why putting the renderer into debug mode not put twig into debug mode. But I think it is okay to have the two settings because they are different things and having both on at the same time would be very noisy.

+++ b/core/lib/Drupal/Core/Render/Renderer.php
@@ -756,4 +765,43 @@ protected function ensureMarkupIsSafe(array $elements) {
+    $prefix .= "\n  CACHE-HIT: " . ($is_cache_hit ? 'YES' : 'NO') . "";
...
+      $suffix .= "\nCache tags: [";
...
+      $suffix .= "\nCache keys: [";

Why is CACHE_HIT: indented and the Cache tags: and Cache keys: not. Also why is it in CAPS.

chr.fritsch’s picture

StatusFileSize
new6.1 KB
new1.51 KB

I adjusted the output to be more in line with the twig debug output.

Status: Needs review » Needs work

The last submitted patch, 45: 2381797-45.patch, failed testing. View results

chr.fritsch’s picture

Status: Needs work » Needs review
StatusFileSize
new6.1 KB
new1.49 KB

Fixing the test...

geek-merlin’s picture

#44:
> I spent some time pondering if this should be merged with twig's debug flag. ... But I think it is okay to have the two settings because they are different things and having both on at the same time would be very noisy.

Yes to this.

I also wonder how in the future we relate this to Renderviz ( also by Wim ;-)? First i thought this is renderviz in core but that one dumps the cache metadata (not hit/miss). (Kudos, i really fell in love with it!.) That one also includes Cache Context which this does not (see \Drupal
enderviz\Renderer
).

@Wim?

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community

Looks like all feedback has been addressed. And its been 5 years.

andypost’s picture

Version: 8.6.x-dev » 8.8.x-dev
Issue tags: -Needs tests

++

larowlan’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs change record

This looks good to go, can we get a change record please?

geek-merlin’s picture

Status: Needs review » Needs work

> This looks good to go, can we get a change record please?

So this should be NW.

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.

johnwebdev’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record
StatusFileSize
new6.17 KB

Added change record. Rerolled and fixed test.

Status: Needs review » Needs work

The last submitted patch, 55: 2381797-55.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

johnwebdev’s picture

Status: Needs work » Needs review
StatusFileSize
new6.8 KB
new526 bytes
krzysztof domański’s picture

Status: Needs review » Needs work

1. Use assertStringContainsString(). The code will be easier to understand.

-  preg_match('/CACHE-HIT:\s(No|Yes)/', $markup, $matches);
-  $this->assertEquals('No', $matches[1]);
+  $this->assertStringContainsString('CACHE-HIT: No', $markup);
-  preg_match('/CACHE-HIT:\s(No|Yes)/', $markup, $matches);
-  $this->assertEquals('Yes', $matches[1]);
+  $this->assertStringContainsString('CACHE-HIT: Yes', $markup);

2. Coding standards.

--- a/core/lib/Drupal/Core/Render/Renderer.php
+++ b/core/lib/Drupal/Core/Render/Renderer.php
@@ -790,7 +790,7 @@ protected function doCallback($callback_type, $callback, array $args) {
     return $this->doTrustedCallback($callback, $args, $message, TrustedCallbackInterface::THROW_EXCEPTION, RenderCallbackInterface::class);
   }
 
-  /*
+  /**
    * Add cache debug information to the render array.
johnwebdev’s picture

Status: Needs work » Needs review
StatusFileSize
new6.72 KB
new1.33 KB

Adresses #58

krzysztof domański’s picture

Status: Needs review » Needs work

Thanks @johnwebdev!

If installed Drupal core prior to version 9.1.0 the new option will not be in the services.yml file (copied default.services.yml). Then you will see:

Notice: Undefined index: debug in Drupal\Core\Render\Renderer->doRender() (line 530 of core\lib\Drupal\Core\Render\Renderer.php).

That should be enough:

--- a/core/lib/Drupal/Core/Render/Renderer.php
+++ b/core/lib/Drupal/Core/Render/Renderer.php
@@ -119,6 +119,9 @@ public function __construct(ControllerResolverInterface $controller_resolver, Th
     $this->elementInfo = $element_info;
     $this->placeholderGenerator = $placeholder_generator;
     $this->renderCache = $render_cache;
+    if (!isset($renderer_config['debug'])) {
+      $renderer_config['debug'] = FALSE;
+    }
     $this->rendererConfig = $renderer_config;
krzysztof domański’s picture

Let's add a cache context to the debug output.

<!-- CACHE CONTEXTS:
   * languages:language_interface
   * theme
   * url.path
   * user.permissions
-->
johnwebdev’s picture

Status: Needs work » Needs review
StatusFileSize
new85.93 KB
new80.02 KB

Adresses #60 and #61

johnwebdev’s picture

StatusFileSize
new7.59 KB
new78.22 KB

Ah... sorry about that.

krzysztof domański’s picture

[edited]

krzysztof domański’s picture

Status: Needs review » Needs work

1. I tested manually. Works correctly. Before RTBC let's refactor the test. It will be more readable if we use the heredoc syntax.

    $expected = <<<EOF


<!-- START RENDERER -->
<!-- CACHE-HIT: No -->
<!-- CACHE TAGS:
   * render_cache_test_tag
   * render_cache_test_tag1
-->
<!-- CACHE CONTEXTS:
   * languages:language_interface
   * theme
-->
<!-- CACHE KEYS:
   * render_cache_test_key
-->
Test 1
<!-- END RENDER -->

EOF;
    $this->assertEquals($expected, $markup);

2. I discovered two "\n" signs at the beginning of the markup. Is that necessary?

nevergone’s picture

Status: Needs work » Needs review

Please review.

krzysztof domański’s picture

Status: Needs review » Needs work

Back to NW. See #65.

krzysztof domański’s picture

Status: Needs work » Needs review
StatusFileSize
new2.81 KB
new7.08 KB

Addressed #65.

Status: Needs review » Needs work

The last submitted patch, 68: 2381797-68.patch, failed testing. View results

anmolgoyal74’s picture

Status: Needs work » Needs review
StatusFileSize
new7.26 KB
new683 bytes
nevergone’s picture

Please test and review, thanks!

krzysztof domański’s picture

Issue summary: View changes
StatusFileSize
new95.35 KB
nevergone’s picture

StatusFileSize
new8.5 KB
new3.81 KB

Debug output with pre-bubbling cache datas and rendering time.

Please test, review and help extening test.

Status: Needs review » Needs work

The last submitted patch, 73: 2381797-73.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

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.

nevergone’s picture

How could this be improved?

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.

joachim’s picture

> How could this be improved?

It would be great if we didn't have to repeat parameters in sites/development.services.yml which we're not changing, as that violates DRY. I have to do this:

parameters:
  http.response.debug_cacheability_headers: true
  renderer.config:
    required_cache_contexts: ['languages:language_interface', 'theme', 'user.permissions']
    auto_placeholder_conditions:
      max-age: 0
      contexts: ['session', 'user']
      tags: []
    debug: true

when all I really mean is this:

parameters:
  http.response.debug_cacheability_headers: true
  renderer.config:
    # keep everything else the same
    debug: true

But that's out of the scope of this issue, I think. Or not possible at all.

longwave’s picture

@joachim I think that should be a separate issue, it is surely possible that container parameters could be merged, but it might be tricky when you want to explicitly remove members of an array in an override.

longwave’s picture

This patch has been super helpful in debugging some caching issues on a complex site, and it still applies back on 8.9.x which I am grateful for. Thanks to everyone who has contributed so far!

+++ b/core/lib/Drupal/Core/Render/Renderer.php
@@ -781,4 +797,63 @@ protected function doCallback($callback_type, $callback, array $args) {
+    $prefix = "<!-- START RENDERER -->";
...
+    $suffix = "<!-- END RENDER -->";

I think this should be consistent, either RENDER or RENDERER each time.

dhirendra.mishra’s picture

Status: Needs work » Needs review
StatusFileSize
new2.42 KB
new8.5 KB

I have re-rolled it against 9.3.x and updated fix from #80.

longwave’s picture

Status: Needs review » Needs work

Thanks, some coding standards work needed now - these are probably new since the patch was last worked on:

FILE: /var/www/html/core/lib/Drupal/Core/Render/Renderer.php
----------------------------------------------------------------------
FOUND 4 ERRORS AFFECTING 4 LINES
----------------------------------------------------------------------
 813 | ERROR | [x] Parameter comment indentation must be 3 spaces,
     |       |     found 2 spaces
 815 | ERROR | [x] Parameter comment indentation must be 3 spaces,
     |       |     found 2 spaces
 863 | ERROR | [x] Expected 1 blank line after function; 0 found
 864 | ERROR | [x] The closing brace for the class must have an empty
     |       |     line before it
ankithashetty’s picture

Status: Needs work » Needs review
StatusFileSize
new8.51 KB
new623 bytes

Addressed #82 suggestions, thanks!

Status: Needs review » Needs work

The last submitted patch, 83: 2381797-83.patch, failed testing. View results

tobiasb’s picture

Status: Needs work » Needs review
StatusFileSize
new9.03 KB
new3.8 KB

* Added native typehints
* Fixes the tests
* RENDER -> RENDERER

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.

super_romeo’s picture

Thank you for your work!
I guess that it will be useful to add some ID to <!-- START RENDERER --> and <!-- END RENDERER -->.
To understand where is start and end of each block. And to help searching.
E.g. random int will suit. But better md5(serialize($element)) but it takes more time.

luksak’s picture

Status: Needs review » Needs work

This needs a re-roll.

tobiasb’s picture

@Lukas von Blarer
And why?

longwave’s picture

Status: Needs work » Needs review

#85 applies cleanly to 9.4.x and 10.0.x for me.

longwave’s picture

Status: Needs review » Needs work
+++ b/core/assets/scaffold/files/default.services.yml
@@ -132,6 +132,13 @@ parameters:
+    # Not recommended in production environments

We reworded similar sentences in #3166449: Improve wording around twig.cache setting for production environments

I suggest "Enabling render cache debugging is not recommended in production environments."

Otherwise this looks ready to go to me.

ankithashetty’s picture

Status: Needs work » Needs review
StatusFileSize
new9.12 KB
new1000 bytes

Just addressed the change mentioned by @longwave in #91, thanks!

longwave’s picture

Status: Needs review » Reviewed & tested by the community

Thanks! As far as I can see there is nothing left to do here.

joachim’s picture

Status: Reviewed & tested by the community » Needs work

Just a small error in a comment:

+++ b/core/lib/Drupal/Core/Render/Renderer.php
@@ -276,6 +283,10 @@ protected function doRender(&$elements, $is_root_call = FALSE) {
+        // Add debug output to the renderable array on cache miss.
+        if ($this->rendererConfig['debug'] === TRUE) {
+          $elements = $this->addDebugOutput($elements, TRUE);

Comment is wrong here, since the TRUE being passed in is $is_cache_hit.

yogeshmpawar’s picture

Status: Needs work » Needs review
StatusFileSize
new9.12 KB
new708 bytes

Agree with @joachim. Addressed #94

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.

slashrsm’s picture

Status: Needs review » Reviewed & tested by the community

I've applied and tested the patch at on of our projects and I can confirm that it works as expected (and it is super helpful too!).

fabianx’s picture

RTBC + 1

This looks really really good! Thanks for everyone that worked on this over the 8 years.

Let's get this finally in!

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/lib/Drupal/Core/Render/Renderer.php
    @@ -772,4 +788,64 @@ protected function doCallback($callback_type, $callback, array $args) {
    +    if (!empty($elements['#markup'])) {
    +      $elements['#markup'] = "$prefix\n" . $elements['#markup'] . "\n$suffix";
    +    }
    

    If there is no $elements['#markup'] then this method is going to have to a load of work for no reason. Should we do an early return if $elements['#markup'] is empty as we have nowhere to attach the markup too... or should we be creating a #markup key/value...

  2. +++ b/core/lib/Drupal/Core/Render/Renderer.php
    @@ -276,6 +283,10 @@ protected function doRender(&$elements, $is_root_call = FALSE) {
             if (is_string($elements['#markup'])) {
               $elements['#markup'] = Markup::create($elements['#markup']);
             }
    +        // Add debug output to the renderable array on cache hit.
    +        if ($this->rendererConfig['debug'] === TRUE) {
    +          $elements = $this->addDebugOutput($elements, TRUE);
    +        }
    

    This looks very odd... It looks like we'll be losing the Makrup object-ness of $elements['#markup'] is the debug output is added. That is going to cause problems.

slashrsm’s picture

Status: Needs work » Needs review
StatusFileSize
new9.16 KB
new1.05 KB

The last submitted patch, 100: 2381797-100.patch, failed testing. View results

slashrsm’s picture

StatusFileSize
new913 bytes
new9.18 KB
andypost’s picture

This digits makes me think about precision configuration of PHP - 0.0000000123456789

slashrsm’s picture

StatusFileSize
new9.22 KB
new1.38 KB

Great point. What about setting precision to 9 decimal places (nanoseconds) when generating output? I think that we don't need more than that.

I also made the regular expression a bit more specific to make sure that we are actually matching that line.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Thanks 👍 looks ready

longwave’s picture

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

This didn't make it into 9.5.0-beta1 and so I don't think we are eligible to add it now. Bumping to 10.1.x which is the next place it can land; the patch still applies.

nevergone’s picture

Category: Task » Feature request

Please backport 9.5.0.

  • catch committed 77031fd on 10.0.x
    Issue #2381797 by Tom Verhaeghe, slashrsm, johnwebdev, chr.fritsch,...
  • catch committed cc44e69 on 10.1.x
    Issue #2381797 by Tom Verhaeghe, slashrsm, johnwebdev, chr.fritsch,...
catch’s picture

Version: 10.1.x-dev » 9.5.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Committed/pushed to 10.1.x and cherry-picked to 10.0.x, thanks!

I think we might be OK to backport this to 9.5.x during beta - it's a developer-facing feature and doesn't have any API implications, will get another opinion.

xjm’s picture

Issue tags: +Needs release note

  • catch committed 7f72278 on 9.5.x
    Issue #2381797 by Tom Verhaeghe, slashrsm, johnwebdev, chr.fritsch,...
catch’s picture

Issue summary: View changes
Status: Patch (to be ported) » Fixed
Issue tags: -Needs release note +9.5.0 release notes, +10.0.0 release notes, +9.5.0 release highlights

Discussed with @xjm and we both think this can be backported. But adding a release note for the changes to default.services.yml

Not sure where we stand on developer-facing features as release highlights, but tagging for 9.5 release highlights in case we want to include it.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.