You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ma...@apache.org on 2025/01/27 06:28:42 UTC

(superset) branch cache_bootstrap_data created (now faf618e8a5)

This is an automated email from the ASF dual-hosted git repository.

maximebeauchemin pushed a change to branch cache_bootstrap_data
in repository https://gitbox.apache.org/repos/asf/superset.git


      at faf618e8a5 feat: cache the fontend's bootstrap data

This branch includes the following new commits:

     new faf618e8a5 feat: cache the fontend's bootstrap data

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(superset) 01/01: feat: cache the fontend's bootstrap data

Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

maximebeauchemin pushed a commit to branch cache_bootstrap_data
in repository https://gitbox.apache.org/repos/asf/superset.git

commit faf618e8a5a102bbff995ec8639c36c7124236cb
Author: Maxime Beauchemin <ma...@gmail.com>
AuthorDate: Sun Jan 26 22:20:19 2025 -0800

    feat: cache the fontend's bootstrap data
    
    While reviewing https://github.com/apache/superset/pull/30134 which seemed to make heavy use of `getBootstrapData`, I realized that it's usage is occurs 21 times across the codebase while it may be a fairly expensive operation (JSON.parse on an object that may be significant in size at times (?)).
    
    Now it's easy to cache the result in a closure and ensure it happens only once per SPA-load, but opening this as a DRAFT as we probably already store it in many places.
    
    Tradeoff here is compute VS memory, and clearly we need in memory somewhere, but probably in most cases, already have it in the state app somewhere. Ideally the app would read it once and make it available in its state, as opposed to different modules calling it on-demand...
    
    PR is more of a request for comments.
---
 superset-frontend/src/utils/getBootstrapData.ts | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/superset-frontend/src/utils/getBootstrapData.ts b/superset-frontend/src/utils/getBootstrapData.ts
index e426498e57..88ab11f544 100644
--- a/superset-frontend/src/utils/getBootstrapData.ts
+++ b/superset-frontend/src/utils/getBootstrapData.ts
@@ -16,12 +16,18 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-
 import { BootstrapData } from 'src/types/bootstrapTypes';
 import { DEFAULT_BOOTSTRAP_DATA } from 'src/constants';
 
+let cachedBootstrapData: BootstrapData | null = null;
+
 export default function getBootstrapData(): BootstrapData {
-  const appContainer = document.getElementById('app');
-  const dataBootstrap = appContainer?.getAttribute('data-bootstrap');
-  return dataBootstrap ? JSON.parse(dataBootstrap) : DEFAULT_BOOTSTRAP_DATA;
+  if (cachedBootstrapData === null) {
+    const appContainer = document.getElementById('app');
+    const dataBootstrap = appContainer?.getAttribute('data-bootstrap');
+    cachedBootstrapData = dataBootstrap
+      ? JSON.parse(dataBootstrap)
+      : DEFAULT_BOOTSTRAP_DATA;
+  }
+  return cachedBootstrapData;
 }