Skip to content

Commit 512163c

Browse files
committed
Optimize initial page load time
1 parent de6a8a2 commit 512163c

File tree

15 files changed

+352
-122
lines changed

15 files changed

+352
-122
lines changed

docs/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<link rel="stylesheet" href="css/theme.css?v=2.4.0">
1515
<link rel="icon" type="image/png" href="favicon.png">
1616
<script>
17-
var BUILD_TS = 1652848867;
17+
var BUILD_TS = 1654150608;
1818
</script>
1919
<script async src="https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js"></script>
2020
<script type="text/x-mathjax-config">
@@ -26,8 +26,8 @@
2626
</script>
2727
<script src="js/BigInteger.min.js?v=2.4.0" type="text/javascript"></script>
2828
<script src="js/sha1.js?v=2.4.0" type="text/javascript"></script>
29-
<script src="js/data.js?ts=1652848867" type="text/javascript"></script>
30-
<script src="search/bkt_index.js?ts=1652848867" type="text/javascript"></script>
29+
<script src="js/data.js?ts=1654150608" type="text/javascript"></script>
30+
<script src="search/bkt_index.js?ts=1654150608" type="text/javascript"></script>
3131
<script src="js/ral.js?v=2.4.0" type="text/javascript"></script>
3232
<script src="js/main.js?v=2.4.0" type="text/javascript"></script>
3333
<script src="js/nav.js?v=2.4.0" type="text/javascript"></script>

docs/js/content_search.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class ContentSearch {
200200
}
201201
}
202202
}
203-
take_a_break();
203+
await take_a_break();
204204
if(abortSignal.aborted) return;
205205

206206
// Sort results by match score

docs/js/data.js

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/js/main.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,36 @@ function onPageLoad() {
5050

5151
window.onpopstate = onPopState;
5252
window.onkeydown = onKeyDownMain;
53-
init_tree();
53+
54+
// Determine what page id will be loaded
55+
var url = new URL(window.location.href);
56+
var path = url.searchParams.get("p", path);
57+
var parsed_path = parse_path(path);
58+
var id;
59+
if(parsed_path == null) {
60+
id = 0;
61+
} else {
62+
id = parsed_path[0];
63+
}
64+
65+
// Prepare content for initial page load
66+
ral_expand_all_bigint_pass1(id);
67+
var deferred_sb_work;
68+
deferred_sb_work = init_tree_pass1(id);
5469
init_sb_resizer();
55-
load_page_via_url();
70+
71+
// Load content
72+
load_page_via_url().then(() => {
73+
// finish remaining initialization after page load
74+
// defer it to the next animation frame
75+
// TODO: Figure out a better way to defer these to after page rendering
76+
// requestAnimationFrame and other methods don't seem to work
77+
setTimeout(() => {
78+
ral_expand_all_bigint_pass2();
79+
init_tree_pass2(deferred_sb_work);
80+
}, 100);
81+
});
82+
5683
init_index_edit();
5784
userHooks.onPageLoad();
5885
}
@@ -321,6 +348,10 @@ function isDescendant(parent, child) {
321348
return(false);
322349
}
323350

351+
async function take_a_break(){
352+
await new Promise(r => setTimeout(r, 1));
353+
}
354+
324355
//==============================================================================
325356
// Compatibility Workarounds
326357
//==============================================================================

docs/js/nav.js

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ async function load_page(id) {
1616
init_reg_value();
1717
init_radix_buttons();
1818
}
19-
//MathJax.Hub.Queue(["Typeset", MathJax.Hub, main_el]);
2019
MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
2120
userHooks.onContentLoad();
2221
})
@@ -42,47 +41,42 @@ async function fetch_page_content(id){
4241
return awaitable;
4342
}
4443

45-
function load_page_via_url(){
44+
async function load_page_via_url(){
4645
// An event triggered such that the page should be loaded based on the URL
4746
var prev_id = CurrentID;
4847

4948
var url = new URL(window.location.href);
5049
var path = url.searchParams.get("p", path);
51-
if(path == null){
52-
// No path specified. Default to root node
50+
var parsed_path = parse_path(path);
51+
var new_path;
52+
if(parsed_path == null) {
53+
// Bad path. Discard it
54+
new_path = "";
5355
CurrentID = 0;
5456
} else {
55-
// URL contains a hier path
56-
var parsed_path = parse_path(path);
57-
var new_path;
58-
if(parsed_path == null) {
59-
// Bad path. Discard it
60-
new_path = "";
61-
CurrentID = 0;
62-
} else {
63-
// Path is good.
64-
var id, idx_stack;
65-
id = parsed_path[0];
66-
idx_stack = parsed_path[1];
67-
apply_idx_stack(id, idx_stack);
68-
69-
// Recompute the path in case it needs to be cleaned up
70-
new_path = get_path(id);
71-
CurrentID = id;
72-
}
57+
// Path is good.
58+
var id, idx_stack;
59+
id = parsed_path[0];
60+
idx_stack = parsed_path[1];
61+
apply_idx_stack(id, idx_stack);
7362

74-
if(path != new_path){
75-
// Path was sanitized. Patch URL
76-
url.searchParams.set("p", new_path);
77-
window.history.replaceState({}, "", url.toString())
78-
}
63+
// Recompute the path in case it needs to be cleaned up
64+
new_path = get_path(id);
65+
CurrentID = id;
7966
}
67+
68+
if(path != new_path){
69+
// Path was sanitized. Patch URL
70+
url.searchParams.set("p", new_path);
71+
window.history.replaceState({}, "", url.toString())
72+
}
73+
8074
if(prev_id != CurrentID) {
81-
load_page(CurrentID).then(() => {
75+
await load_page(CurrentID).then(() => {
8276
select_tree_node();
8377
expand_to_tree_node();
8478
open_tree_node(CurrentID);
85-
scroll_to_tree_node(CurrentID);
79+
scroll_to_tree_node();
8680
refresh_title();
8781
refresh_target_scroll();
8882
});
@@ -118,7 +112,7 @@ function load_page_via_path(path, url_hash){
118112
select_tree_node();
119113
expand_to_tree_node();
120114
open_tree_node(CurrentID);
121-
scroll_to_tree_node(CurrentID);
115+
scroll_to_tree_node();
122116
refresh_url(url_hash);
123117
refresh_title();
124118
refresh_target_scroll();
@@ -140,7 +134,7 @@ function onClickNodeLink(ev) {
140134
select_tree_node();
141135
expand_to_tree_node();
142136
open_tree_node(id);
143-
scroll_to_tree_node(id);
137+
scroll_to_tree_node();
144138
refresh_url();
145139
refresh_title();
146140
});
@@ -165,7 +159,7 @@ function load_parent_page(){
165159
select_tree_node();
166160
expand_to_tree_node();
167161
open_tree_node(id);
168-
scroll_to_tree_node(id);
162+
scroll_to_tree_node();
169163
refresh_url();
170164
refresh_title();
171165
});

docs/js/ral.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ function parse_path(path){
7676
// [id, idx_stack]
7777
// Any invalid indexes in the path are fixed silently
7878

79+
if(path == null){
80+
return null;
81+
}
82+
7983
// Decompose the path
8084
var pathparts = path.split(".");
8185
var segments = [];
@@ -345,3 +349,38 @@ function toBigInt(str) {
345349
return(bigInt(str));
346350
}
347351
}
352+
353+
function ral_expand_bigint(id){
354+
// Check if RAL entry has been converted yet
355+
if(typeof RALIndex[id].offset !== 'string') return;
356+
357+
// Needs conversion from base-16 string --> bigInt object
358+
RALIndex[id].offset = bigInt(RALIndex[id].offset, 16);
359+
RALIndex[id].size = bigInt(RALIndex[id].size, 16);
360+
if('stride' in RALIndex[id]) RALIndex[id].stride = bigInt(RALIndex[id].stride, 16);
361+
362+
if(is_register(id)) {
363+
for(var i=0; i<RALIndex[id].fields.length; i++){
364+
RALIndex[id].fields[i].reset = bigInt(RALIndex[id].fields[i].reset, 16);
365+
}
366+
}
367+
}
368+
369+
function ral_expand_all_bigint_pass1(first_id){
370+
// RALIndex contains strings that represent integers that need to be expanded
371+
// to bigInt objects
372+
373+
// To keep initial page load fast, only expand ids in the linage of first_id
374+
var ids = get_ancestors(first_id);
375+
ids.push(first_id);
376+
for(var i=0; i<ids.length; i++){
377+
ral_expand_bigint(ids[i]);
378+
}
379+
}
380+
381+
function ral_expand_all_bigint_pass2(){
382+
// Next, asynchronously process all others
383+
for(var i=0; i<RALIndex.length; i++){
384+
ral_expand_bigint(i);
385+
}
386+
}

docs/js/search.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,6 @@ async function start_search(query) {
160160
}
161161
}
162162

163-
164-
async function take_a_break(){
165-
await new Promise(r => setTimeout(r, 1));
166-
}
167-
168163
function add_search_result(text_segments, node_id, idx_stack=null, anchor="", content_preview=null){
169164
// text_segments is an array of segments that should/shouldn't be highlighted
170165
// All odd segments are highlighted via <mark> tag.
@@ -240,7 +235,7 @@ function open_search_result(result_idx){
240235
select_tree_node();
241236
expand_to_tree_node();
242237
open_tree_node(result.node_id);
243-
scroll_to_tree_node(result.node_id);
238+
scroll_to_tree_node();
244239
refresh_url(url_hash);
245240
refresh_title();
246241
refresh_target_scroll();

docs/js/sidebar.js

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,60 @@ var SBResizeState = {};
55
SBResizeState.old_width = 0;
66
SBResizeState.start_x = 0;
77

8-
function init_tree() {
8+
function init_tree_pass1(first_id){
9+
// initialize the bare-minimum of the sidebar tree in order to display the
10+
// first_id page
11+
12+
// array of required ID lineage
13+
var id_chain = get_ancestors(first_id);
14+
id_chain.push(first_id);
15+
16+
// deferred sidebar work for 2nd pass
17+
// array of pairs: [cdiv, id]
18+
var deferred_sb_work = [];
19+
920
var el = document.getElementById("_SBTree");
10-
RootNodeIds.forEach(function(id) {
11-
add_tree_node(el, id);
12-
});
21+
for(var i=0; i<RootNodeIds.length; i++){
22+
add_tree_nodes_in_chain(el, RootNodeIds[i], id_chain, deferred_sb_work);
23+
};
24+
25+
return deferred_sb_work;
26+
}
27+
28+
function init_tree_pass2(deferred_sb_work){
29+
for(var i=0; i<deferred_sb_work.length; i++){
30+
var cdiv = deferred_sb_work[i][0];
31+
var cid = deferred_sb_work[i][1];
32+
add_tree_node_recursive(cdiv, cid);
33+
}
34+
}
35+
36+
function add_tree_nodes_in_chain(parent_el, id, id_chain, deferred_sb_work){
37+
var node = RALIndex[id];
38+
var cdiv;
39+
40+
cdiv = create_tree_node(parent_el, id);
41+
if(node.children.length > 0){
42+
if((id_chain.length != 0) && (id_chain[0] == id)){
43+
// node is in chain. Keep going
44+
id_chain.shift();
45+
for(var i=0; i<node.children.length; i++){
46+
var cid = node.children[i];
47+
add_tree_nodes_in_chain(cdiv, cid, id_chain, deferred_sb_work);
48+
}
49+
} else {
50+
// node is not in chain, but has children. Defer them
51+
for(var i=0; i<node.children.length; i++){
52+
var cid = node.children[i];
53+
deferred_sb_work.push([cdiv, cid]);
54+
}
55+
}
56+
}
1357
}
1458

15-
function add_tree_node(parent_el, id){
59+
function create_tree_node(parent_el, id) {
60+
// Creates a single tree node.
61+
// If has children, returns the div element that shall contain children
1662
var node = RALIndex[id];
1763

1864
var div;
@@ -38,7 +84,7 @@ function add_tree_node(parent_el, id){
3884
txt += "[]";
3985
}
4086
link.innerHTML = txt;
41-
}else{
87+
} else {
4288
link.innerHTML = node.name;
4389
}
4490
div.appendChild(link);
@@ -52,15 +98,28 @@ function add_tree_node(parent_el, id){
5298
cdiv.className = "node-children";
5399
parent_el.appendChild(cdiv);
54100

55-
for(var i=0; i<node.children.length; i++){
56-
add_tree_node(cdiv, node.children[i]);
57-
}
101+
return cdiv;
58102
} else {
59103
// is leaf node
60104
div.classList.add("leaf");
105+
106+
return null;
107+
}
108+
}
109+
110+
function add_tree_node_recursive(parent_el, id){
111+
var node = RALIndex[id];
112+
var cdiv;
113+
114+
cdiv = create_tree_node(parent_el, id);
115+
if(node.children.length > 0){
116+
for(var i=0; i<node.children.length; i++){
117+
add_tree_node_recursive(cdiv, node.children[i]);
118+
}
61119
}
62120
}
63121

122+
64123
function select_tree_node() {
65124
var id = CurrentID;
66125
// Changes the selected tree node
@@ -100,8 +159,8 @@ function expand_to_tree_node() {
100159
}
101160
}
102161

103-
function scroll_to_tree_node(id) {
104-
var node_el = document.getElementById("_SBNode" + id);
162+
function scroll_to_tree_node() {
163+
var node_el = document.getElementById("_SBNode" + CurrentID);
105164
var tree_el = document.getElementById("_SBTreeContainer");
106165

107166
var node_rect = node_el.getBoundingClientRect();

0 commit comments

Comments
 (0)