Problem/Motivation

The optimization mentioned below was mainly done to improve response times with the entitycache module installed - see #687712-29: Optimize rdf.module for displaying multiple comments:

does not bring any performance impact in core, but makes it possible for entitycache to cache these values.

However...

Proposed resolution

revert #687712-32: Optimize rdf.module for displaying multiple comments

-- Original:

Problem/Motivation

In Drupal 7, #687712: Optimize rdf.module for displaying multiple comments had introduced an optimization in rdf.module for multiple comments:

Most of the extra time is spent in rdf_preprocess_comment() which calls url() multiple times (150 in total per page)

We should check if this is still necessary with all the new Drupal 8 APIs. For one, entity caching is available in core so we would be using this code directly, but at the same time, the entity output is cached, so we may not need this optimization in the end. Better to reduce the amount of code if possible.

Proposed resolution

revert #687712-32: Optimize rdf.module for displaying multiple comments if there is no gain from this old optimization.

Remaining tasks

evaluate if the code introduced in #687712-32: Optimize rdf.module for displaying multiple comments improves performance.

User interface changes

API changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

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.

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.

roderik’s picture

Status: Active » Needs review
FileSize
3.65 KB

I suspects the current situation also has cacheability issues which are solved by reverting #687712-32: Optimize rdf.module for displaying multiple comments.

First testing; commenting later.

roderik’s picture

Version: 8.6.x-dev » 8.8.x-dev
roderik’s picture

The thing with the current code is, it's wrong although I'm not completely sure in what way it should be fixed if it stayed in place:

+++ b/core/modules/rdf/rdf.module
@@ -224,28 +224,6 @@ function rdf_comment_storage_load($comments)
-    // The current function is a storage level hook, so avoid to bubble
-    // bubbleable metadata, because it can be outside of a render context.
-    $comment->rdf_data['entity_uri'] = $entity->toUrl()->toString(TRUE)->getGeneratedUrl();
-    if ($comment->hasParentComment()) {
-      $comment->rdf_data['pid_uri'] = $comment->getParentComment()->toUrl()->toString();
-    }

That second toString() can cause exceptions to be thrown if it's ever called from certain points in the code*. And it's entirely possible for code to indirectly call rdf_comment_storage_load() from such a point - even though Core doesn't do this. (See also: above code comment.)

However I'm doubtful that the solution is to change the ->toString() to ->toString(TRUE)->getGeneratedUrl(). (In other words, I don't trust the reasoning of the code comment above here: just because it can be called outside a render context, does not mean that cacheability metadata should just always be disregarded.)

The patch moves the code populating the RDF attributes back into rdf_preprocess_comment(), where we can just do toString() for both calls, which will never throw an exception (I assume, because a preprocess hook is always executed in a 'managed' render context?)

This ties in with the question: is the RDF metadata for 'entity_uri' and 'pid_uri' always supposed to be permanent and independent of whatever UrlGenerator logic / cache context? The unpatched code implies "yes" for the 'entity_uri' data. The patched code implies "no; take cache context into account". (I think. And I think that's right - though I'm not very experienced with this UrlGenerator / cache contexts stuff.)

So there are two open questions in my head:

  • Is it necessary to re-test whether this patch decreases performance, as the original issue text suggests?
  • We are now not assigning $comment->rdf_data on load. Is that OK / can we just address that in a Change Notification, or is it a backward compatibility issue and should this data stay the same until Drupal 9? (If it should stay the same... then to address the cacheability stuff / exceptions mentioned above, we should do ->toString(TRUE)->getGeneratedUrl() in both calls above, but still disregard that data in rdf_preprocess_comment(); perform the extra toString() calls instead.)

--

* Exceptions are thrown because toString(default/FALSE) bubbles up cacheability metadata in the background. EarlyRenderingControllerWrapperSubscriber::wrapControllerExecutionInRenderContext() will detect bubbled-but-unhandled metadata and throw this exception.

And those points in the code are: 'within a regular controller route' but not within a method that explicitly handles bubbled-up metadata. See e.g. definition of "early rendering" in #2450993-118: Rendered Cache Metadata created during the main controller request gets lost or discussion / link to article in #2638686: Exception in EarlyRenderingControllerWrapperSubscriber is a DX nightmare, remove it.

And I'm sorry for being wordy but... it's just a lot of things to take in / think about, for a not-very-experienced core contributor. I still have to write things out, to be surer I don't make mistakes in reasoning.

roderik’s picture

Issue summary: View changes

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.

Abhijith S’s picture

Patch #7 can't be applied on 9.2.x.Needs reroll

Checking patch core/modules/rdf/rdf.module...
error: while searching for:
  }
}

/**
 * Implements hook_ENTITY_TYPE_storage_load() for comment entities.
 */
function rdf_comment_storage_load($comments) {
  foreach ($comments as $comment) {
    // Pages with many comments can show poor performance. This information
    // isn't needed until rdf_preprocess_comment() is called, but set it here
    // to optimize performance for websites that implement an entity cache.
    $created_mapping = rdf_get_mapping('comment', $comment->bundle())
      ->getPreparedFieldMapping('created');
    /** @var \Drupal\comment\CommentInterface $comment*/
    $comment->rdf_data['date'] = rdf_rdfa_attributes($created_mapping, $comment->get('created')->first()->toArray());
    $entity = $comment->getCommentedEntity();
    // The current function is a storage level hook, so avoid to bubble
    // bubbleable metadata, because it can be outside of a render context.
    $comment->rdf_data['entity_uri'] = $entity->toUrl()->toString(TRUE)->getGeneratedUrl();
    if ($comment->hasParentComment()) {
      $comment->rdf_data['pid_uri'] = $comment->getParentComment()->toUrl()->toString();
    }
  }
}

/**
 * Implements hook_theme().
 */

error: patch failed: core/modules/rdf/rdf.module:224
error: core/modules/rdf/rdf.module: patch does not apply

Abhijith S’s picture

Rerolled patch #7 .Please check.

Status: Needs review » Needs work

The last submitted patch, 15: 2335487-15.patch, failed testing. View results

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.

vikashsoni’s picture

#7 patch not applying in drupal-9.3.x-dev
Needs to Re-roll
for ref. sharing screenshot ....

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.

smustgrave’s picture

Project: Drupal core » Resource Description Framework (RDF)
Version: 9.5.x-dev » 2.x-dev
Component: rdf.module » Code

This could still be an issue.

RDF is moving out of core and into https://www.drupal.org/project/rdf moving there.