[clang-tools-extra] a9b1e80 - [clang-doc] add async loading (#93276)

via cfe-commits cfe-commits at lists.llvm.org
Sun Jun 30 08:52:39 PDT 2024


Author: PeterChou1
Date: 2024-06-30T08:52:36-07:00
New Revision: a9b1e80acbb3249a245a7bbd8c8f89607bcad954

URL: https://github.com/llvm/llvm-project/commit/a9b1e80acbb3249a245a7bbd8c8f89607bcad954
DIFF: https://github.com/llvm/llvm-project/commit/a9b1e80acbb3249a245a7bbd8c8f89607bcad954.diff

LOG: [clang-doc] add async loading (#93276)

Fixes https://github.com/llvm/llvm-project/issues/93273

This patch changes the way clang-doc loads html indexes. Previously
clang-doc loaded the index via an index_json.js file which uses
JSON.parse to parse the file. This patches changes that to use an async
function called LoadIndex which enables asynchronous loading making the
initial page load not be blocked by loading a large JavaScript object.

Added: 
    

Modified: 
    clang-tools-extra/clang-doc/HTMLGenerator.cpp
    clang-tools-extra/clang-doc/assets/index.js
    clang-tools-extra/test/clang-doc/basic-project.test

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-doc/HTMLGenerator.cpp b/clang-tools-extra/clang-doc/HTMLGenerator.cpp
index 4c72b329507d3..b5da17564c2e0 100644
--- a/clang-tools-extra/clang-doc/HTMLGenerator.cpp
+++ b/clang-tools-extra/clang-doc/HTMLGenerator.cpp
@@ -993,9 +993,9 @@ static llvm::Error serializeIndex(ClangDocContext &CDCtx) {
       });
     });
   };
-  OS << "var JsonIndex = `\n";
+  OS << "async function LoadIndex() {\nreturn";
   IndexToJSON(CDCtx.Idx);
-  OS << "`;\n";
+  OS << ";\n}";
   return llvm::Error::success();
 }
 

diff  --git a/clang-tools-extra/clang-doc/assets/index.js b/clang-tools-extra/clang-doc/assets/index.js
index a5ac90f2e06e7..49818763a4393 100644
--- a/clang-tools-extra/clang-doc/assets/index.js
+++ b/clang-tools-extra/clang-doc/assets/index.js
@@ -80,8 +80,8 @@ function createIndex(Index) {
 
 // Runs after DOM loads
 document.addEventListener("DOMContentLoaded", function() {
-  // JsonIndex is a variable from another file that contains the index
-  // in JSON format
-  var Index = JSON.parse(JsonIndex);
-  createIndex(Index);
+  // LoadIndex is an asynchronous function that will be generated clang-doc.
+  // It ensures that the function call will not block as soon the page loads,
+  // since the index object are often huge and can contain thousands of lines.
+  LoadIndex().then((Index) => { createIndex(Index); });
 });

diff  --git a/clang-tools-extra/test/clang-doc/basic-project.test b/clang-tools-extra/test/clang-doc/basic-project.test
index 3118c20495987..bab5f8e1761bc 100644
--- a/clang-tools-extra/test/clang-doc/basic-project.test
+++ b/clang-tools-extra/test/clang-doc/basic-project.test
@@ -7,8 +7,8 @@
 // RUN: FileCheck %s -input-file=%t/docs/GlobalNamespace/Rectangle.html -check-prefix=HTML-RECTANGLE
 // RUN: FileCheck %s -input-file=%t/docs/GlobalNamespace/Circle.html -check-prefix=HTML-CIRCLE
 
-// JSON-INDEX: var JsonIndex = `
-// JSON-INDEX-NEXT: {
+// JSON-INDEX: async function LoadIndex() {
+// JSON-INDEX-NEXT: return{
 // JSON-INDEX-NEXT:   "USR": "{{([0-9A-F]{40})}}",
 // JSON-INDEX-NEXT:   "Name": "",
 // JSON-INDEX-NEXT:   "RefType": "default",
@@ -51,7 +51,8 @@
 // JSON-INDEX-NEXT:       ]
 // JSON-INDEX-NEXT:     }
 // JSON-INDEX-NEXT:   ]
-// JSON-INDEX-NEXT: }`;
+// JSON-INDEX-NEXT: };
+// JSON-INDEX-NEXT: }
 
 // HTML-SHAPE: <!DOCTYPE html>
 // HTML-SHAPE-NEXT: <meta charset="utf-8"/>


        


More information about the cfe-commits mailing list