Skip to content

Commit 62f5aa8

Browse files
committed
fix(preloader): highly likely = likely
1 parent d78d69d commit 62f5aa8

File tree

4 files changed

+37
-13
lines changed

4 files changed

+37
-13
lines changed

packages/qwik/src/core/preloader/bundle-graph.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ export const parseBundleGraph = (serialized: (string | number)[]) => {
3131
if (idx < 0) {
3232
probability = -idx / 10;
3333
} else {
34-
deps.push({ $name$: serialized[idx] as string, $probability$: probability, $factor$: 1 });
34+
deps.push({
35+
$name$: serialized[idx] as string,
36+
$importProbability$: probability,
37+
$factor$: 1,
38+
});
3539
}
3640
i++;
3741
}

packages/qwik/src/core/preloader/preloader.unit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ test('preloader script', () => {
2121
* dereference objects etc, but that actually results in worse compression
2222
*/
2323
const compressed = compress(Buffer.from(preLoader), { mode: 1, quality: 11 });
24-
expect([compressed.length, preLoader.length]).toEqual([1785, 5285]);
24+
expect([compressed.length, preLoader.length]).toEqual([1816, 5423]);
2525
});

packages/qwik/src/core/preloader/queue.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ export const trigger = () => {
8282
Math.max(1, config.$maxIdlePreloads$ * probability)
8383
: // While the graph is not available, we limit to 2 preloads
8484
2;
85-
// When we're 100% sure, everything needs to be queued
86-
if (probability === 1 || preloadCount < allowedPreloads) {
85+
// When we're 99% sure, everything needs to be queued
86+
if (probability >= 0.99 || preloadCount < allowedPreloads) {
8787
queue.shift();
8888
preloadOne(bundle);
8989
} else {
@@ -142,17 +142,27 @@ const preloadOne = (bundle: BundleImport) => {
142142
doc.head.appendChild(link);
143143
};
144144

145+
/**
146+
* Adjust the probability of a bundle based on the probability of its dependent bundles, and queue
147+
* it if it's likely enough to be preloaded.
148+
*
149+
* Note that if the probability is 100%, we treat the dynamic imports as 99% sure, and both will be
150+
* preloaded without limit.
151+
*
152+
* We also limit "organic" probability to 98% so they don't get unlimited preloads.
153+
*/
145154
export const adjustProbabilities = (
146155
bundle: BundleImport,
147-
adjustFactor: number,
156+
newInverseProbability: number,
148157
seen?: Set<BundleImport>
149158
) => {
150159
if (seen?.has(bundle)) {
151160
return;
152161
}
153162

154163
const previousInverseProbability = bundle.$inverseProbability$;
155-
bundle.$inverseProbability$ *= adjustFactor;
164+
bundle.$inverseProbability$ = newInverseProbability;
165+
// Don't propagate tiny changes
156166
if (previousInverseProbability - bundle.$inverseProbability$ < 0.01) {
157167
return;
158168
}
@@ -178,6 +188,10 @@ export const adjustProbabilities = (
178188
const probability = 1 - bundle.$inverseProbability$;
179189
for (const dep of bundle.$deps$) {
180190
const depBundle = getBundle(dep.$name$)!;
191+
if (depBundle.$inverseProbability$ === 0) {
192+
// it's already at max probability
193+
continue;
194+
}
181195
const prevAdjust = dep.$factor$;
182196
/**
183197
* The chance that a dep won't be loaded is 1-(the chance that the dep will be loaded)*(the
@@ -189,14 +203,20 @@ export const adjustProbabilities = (
189203
* But when we're very likely to load the current bundle, make the dynamic imports very likely
190204
* too.
191205
*/
192-
const newInverseProbability =
193-
dep.$probability$ !== 1 && adjustFactor < 0.1 ? 0.01 : 1 - dep.$probability$ * probability;
206+
let newInverseProbability: number;
207+
if (probability >= 0.99) {
208+
// we're loaded at max probability, so elevate dynamic imports to 99% sure
209+
newInverseProbability = Math.min(0.01, 1 - dep.$importProbability$);
210+
} else {
211+
const newInverseImportProbability = 1 - dep.$importProbability$ * probability;
212+
const factor = newInverseImportProbability / prevAdjust;
213+
// limit organic probability to 98%
214+
newInverseProbability = Math.max(0.02, depBundle.$inverseProbability$ * factor);
215+
dep.$factor$ = factor;
216+
}
194217

195218
/** We need to undo the previous adjustment */
196-
const factor = newInverseProbability / prevAdjust;
197-
dep.$factor$ = factor;
198-
199-
adjustProbabilities(depBundle, factor, seen);
219+
adjustProbabilities(depBundle, newInverseProbability, seen);
200220
}
201221
}
202222
};

packages/qwik/src/core/preloader/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export type ImportProbability = {
2424
/** Bundle name */
2525
$name$: string;
2626
/** Probability */
27-
$probability$: number;
27+
$importProbability$: number;
2828
/** Probability adjust factor */
2929
$factor$: number;
3030
};

0 commit comments

Comments
 (0)