AjaxResponseSubscriber should lazily instantiate AjaxResponseAttachmentsProcessor

Created on 27 March 2024, 9 months ago
Updated 16 April 2024, 8 months ago

Problem/Motivation

AjaxResponseAttachmentsProcessor has lots of dependencies but is only used if the response is an Ajax response.

At the moment all the following services are instantiated early because AjaxResponseSubscriber also responds to request events, but the attachments processor is not needed until later (and often not needed at all):

> ajax_response.attachments_processor
>> asset.resolver
>>> library.discovery
>>>> library.discovery.collector
>>>>> library.discovery.parser
>>>>>> library.libraries_directory_file_finder
>>>>>> extension.path.resolver
>>> library.dependency_resolver
>> asset.css.collection_renderer
>>> asset.query_string
>>> file_url_generator
>> asset.js.collection_renderer
>> renderer
>>> plugin.manager.element_info
>>> render_placeholder_generator
>>>> cache_contexts_manager
>>> render_cache
>>>> variation_cache_factory

Steps to reproduce

Proposed resolution

Inject AjaxResponseAttachmentsProcessor as a service closure and instantiate it only when needed.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Fixed

Version

10.3 ✨

Component
Request processingΒ  β†’

Last updated 6 days ago

No maintainer
Created by

πŸ‡¬πŸ‡§United Kingdom longwave UK

Live updates comments and jobs are added and updated live.
  • Performance

    It affects performance. It is often combined with the Needs profiling tag.

Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @longwave
  • Status changed to Needs review 9 months ago
  • πŸ‡¬πŸ‡§United Kingdom longwave UK
  • Pipeline finished with Failed
    9 months ago
    #130955
  • Pipeline finished with Failed
    9 months ago
    Total: 613s
    #130964
  • First commit to issue fork.
  • Pipeline finished with Success
    9 months ago
    Total: 612s
    #131809
  • Status changed to RTBC 9 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Only rebased to get the tests to run again since I couldn't run manually. The failure before was random and related to migration not connecting to the database.

    All green now.

    Refactor seems fine using the autowire, comparing to other instances in core of how it was used.

  • Status changed to Needs work 9 months ago
  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    We have a couple of modules this is going to break. See https://git.drupalcode.org/search?group_id=2&repository_ref=&scope=blobs...

    I think we should use the DeprecatedServicePropertyTrait to provide a way for anything that extends the class to access the old property and get a deprecation notice.

  • Status changed to Needs review 9 months ago
  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    Added DeprecatedServicePropertyTrait, not sure it's worth adding a test.

    > \Drupal::service('ajax_response.subscriber')->ajaxResponseAttachmentsProcessor;
    = Drupal\Core\Ajax\AjaxResponseAttachmentsProcessor {#10035}
    
  • Status changed to RTBC 9 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Will lean on your committer hat if tests would be needed.

  • Status changed to Fixed 9 months ago
  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    Committed the version without the DeprecatedServicePropertyTrait to 11.x and the version with to 10.3.x.

    Committed 5833d31 and pushed to 11.x. Thanks!
    Committed 17b4882 and pushed to 10.3.x. Thanks!

    • alexpott β†’ committed 17b48823 on 10.3.x
      Issue #3436468 by longwave, smustgrave, alexpott: AjaxResponseSubscriber...
    • alexpott β†’ committed 5833d313 on 11.x
      Issue #3436468 by longwave, smustgrave, alexpott: AjaxResponseSubscriber...
  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

    Thanks for the excellent issue summary, @longwave! Unless I'm mistaken, this means you've just made all non-HTML responses faster, because those would not use the attachments processor service. IOW: this must've made all JSON:API and REST responses slightly faster? (And presumably GraphQL too.)

  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    It's all non-Ajax responses that are theoretically faster, because the processor is only needed if the response object is an AjaxResponse. But I'm not sure it's measurably faster, as object construction in PHP is relatively fast anyway, and a lot of those services are still needed on many requests, just later on in the request cycle. But if it saves a few nanoseconds here and there then it all adds up eventually!

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

Production build 0.71.5 2024