JsComponent::getScopedDependencies() and ::getDependencyLibraries() shouldn't get auto-saves unless previewing
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3539147. --> Reported by: [mglaman](https://www.drupal.org/user/2416470) Related to !630 !1400 >>> <h3 id="overview">Overview</h3> <p><em>Introduced in <span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/experience_builder/issues/3518191" title="Status: Closed (fixed)">#3518191: Build import maps for code component dependencies, attach their CSS</a></span>.</em></p> <p>I started doing some profiling of XB for performance. Viewing an XB page took 228ms roughly. However I noticed 51x queries to key_value_expire. Which is where auto-saves live.</p> <pre>select ... from key_value_expire where ? &gt; :now and ? in (:keys__0) and ? = :collection</pre><p>Which I thought was weird: I was viewing the page as an anonymous user. <code>JsComponent::getScopedDependencies</code> is loading autosaves. It's pretty cheap but has a scaling problem. 17 calls was only 13ms, but someone could have 50 components. AutoSaveManager has a cache and on cache miss it hits the DB for the auto-save. If we're not editing the entity, there is no auto-save. Which means we always cache miss and hit the DB for no reason.</p> <p>I had 198 calls to Memcached::getMulti as well.</p> <p>The problem is that <code>$dependencyAutoSave = $this-&gt;autoSaveManager-&gt;getAutoSaveEntity($js_component_dependency);</code> is always called even if we're not in a preview. Later on in <code>shouldLoadAssetFromAutoSave</code> there is a preview check.</p> <p>We should avoid loading auto-saves unless in preview when rendering components.</p> <h3 id="proposed-resolution">Proposed resolution</h3> <p>In <code> renderComponent</code> we should use an empty auto-save if not in preview:</p> <pre>&nbsp; public function renderComponent(array $inputs, string $componentUuid, bool $isPreview = FALSE): array {<br>&nbsp;&nbsp;&nbsp; $component = $this-&gt;getJavaScriptComponent();<br><br>&nbsp;&nbsp;&nbsp; if ($isPreview) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $autoSave = $this-&gt;autoSaveManager-&gt;getAutoSaveEntity($component);<br>&nbsp;&nbsp;&nbsp; } else {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $autoSave = AutoSaveEntity::empty();<br>&nbsp;&nbsp;&nbsp; }</pre><p>This change brought my response time from 228ms to 106ms! My memcache calls went from 198 calls to 63 and call DB queries to key_value_expire were removed.</p> <h3 id="ui-changes">User interface changes</h3>
issue