Skip to content

Commit ba75d65

Browse files
committed
test: without probabilities on dynamic deps
1 parent 4a22d7f commit ba75d65

File tree

3 files changed

+21
-67
lines changed

3 files changed

+21
-67
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-console */
12
import { isBrowser } from '@builder.io/qwik/build';
23
import {
34
config,
@@ -53,10 +54,12 @@ export const parseBundleGraph = (serialized: (string | number)[]) => {
5354

5455
export const getBundle = (name: string) => {
5556
let bundle = bundles.get(name);
57+
console.log('bundle :', bundle);
5658
if (!bundle) {
5759
let deps: ImportProbability[] | undefined;
5860
if (graph) {
5961
deps = graph.get(name);
62+
console.log('deps :', deps);
6063
if (!deps) {
6164
return;
6265
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ export const adjustProbabilities = (
184184
seen ||= new Set();
185185
seen.add(bundle);
186186
for (const dep of bundle.$deps$) {
187+
console.log('dep :', dep);
187188
const depBundle = getBundle(dep.$name$)!;
188189
const prevAdjust = dep.$factor$;
189190
/**

packages/qwik/src/optimizer/src/plugins/bundle-graph.ts

Lines changed: 17 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1+
import { getSymbolHash } from 'packages/qwik/src/core/qrl/qrl-class';
12
import type { QwikBundle, QwikBundleGraph, QwikManifest } from '../types';
23

3-
const minimumSpeed = 300; // kbps
4-
// size that takes 0.5 seconds to download at minimumSpeed
5-
const slowSize = 0.5 / ((minimumSpeed * 1024) / 8);
6-
74
/**
85
* A function that returns a map of bundle names to their dependencies.
96
*
@@ -13,14 +10,7 @@ export type BundleGraphAdder = (
1310
manifest: QwikManifest
1411
) => Record<string, { imports?: string[]; dynamicImports?: string[] }>;
1512

16-
const getSymbolHash = (symbolName: string) => {
17-
const index = symbolName.lastIndexOf('_');
18-
if (index > -1) {
19-
return symbolName.slice(index + 1);
20-
}
21-
return symbolName;
22-
};
23-
13+
const dynamicTag = '<dynamic>';
2414
/**
2515
* This creates a compact array of dependencies for each bundle. It also contains the symbols. The
2616
* format is:
@@ -42,18 +32,18 @@ export function convertManifestToBundleGraph(
4232
if (!manifest.bundles) {
4333
return [];
4434
}
45-
// All known chunks and symbols
35+
// All known chunks
4636
const graph = { ...manifest.bundles };
47-
for (const [symbol, bundleName] of Object.entries(manifest.mapping)) {
48-
const hash = getSymbolHash(symbol);
49-
if (hash) {
50-
/**
51-
* We use dynamic imports so that we will get probabilities for the bundle when preloading the
52-
* symbol. We still confirm load at 100% probability with the bundle name.
53-
*/
54-
graph[hash] = { dynamicImports: [bundleName] } as QwikBundle;
55-
}
56-
}
37+
// Symbols
38+
Object.assign(
39+
graph,
40+
Object.fromEntries(
41+
Object.entries(manifest.mapping).map(([symbol, chunkname]) => [
42+
getSymbolHash(symbol),
43+
{ imports: [chunkname] } as QwikBundle,
44+
])
45+
)
46+
);
5747
// Routes etc
5848
if (bundleGraphAdders) {
5949
for (const adder of bundleGraphAdders) {
@@ -64,11 +54,6 @@ export function convertManifestToBundleGraph(
6454
}
6555
}
6656

67-
// Remove the preloader, it will already be loaded and has no dependencies
68-
if (manifest.preloader) {
69-
delete graph[manifest.preloader];
70-
}
71-
7257
// Filter out external and non-segment dynamic imports
7358
for (const bundleName of Object.keys(graph)) {
7459
const bundle = graph[bundleName];
@@ -135,47 +120,13 @@ export function convertManifestToBundleGraph(
135120
clearTransitiveDeps(deps, depName);
136121
}
137122
const dynDeps = new Set(bundle.dynamicImports!);
138-
const depProbability = new Map<string, number>();
139123
for (const depName of dynDeps) {
140124
clearTransitiveDeps(dynDeps, depName);
141-
const dep = graph[depName];
142-
143-
// Calculate the probability of the dependency
144-
// Start with a 50% chance
145-
let probability = 0.5;
146-
// Add a 4% chance for each interactivity point (max 20%)
147-
probability += (dep.interactivity || 0) * 0.04;
148-
149-
// If the dependency has a segment from the same parent, it's more likely to be loaded
150-
if (bundle.origins && dep.origins) {
151-
for (const origin of bundle.origins) {
152-
if (dep.origins.some((o) => o.startsWith(origin))) {
153-
// Add a 25% chance
154-
probability += 0.25;
155-
break;
156-
}
157-
}
158-
}
159-
160-
// If the dependency is a likely big import graph, it should be loaded earlier so it doesn't get blocked by smaller files, but when unlikely it should be loaded later so it doesn't block other files
161-
if (dep.total > slowSize) {
162-
probability += probability > 0.5 ? 0.02 : -0.02;
163-
}
164-
165-
depProbability.set(depName, probability);
166125
}
167-
168126
if (dynDeps.size > 0) {
169-
const sorted = Array.from(dynDeps).sort(
170-
(a, b) => depProbability.get(b)! - depProbability.get(a)!
171-
);
172-
let lastProbability = -1;
173127
// We rely on the Set keeping the items in order, everything after this is dynamic
174-
for (const depName of sorted) {
175-
if (depProbability.get(depName)! !== lastProbability) {
176-
lastProbability = depProbability.get(depName)!;
177-
deps.add(-Math.round(lastProbability * 10) as any as string);
178-
}
128+
deps.add(dynamicTag);
129+
for (const depName of dynDeps) {
179130
deps.add(depName);
180131
}
181132
}
@@ -195,9 +146,8 @@ export function convertManifestToBundleGraph(
195146
let { index, deps } = bundle;
196147
index++;
197148
for (const depName of deps) {
198-
if (typeof depName === 'number') {
199-
// negative number means dynamic import
200-
bundleGraph[index++] = depName;
149+
if (depName === dynamicTag) {
150+
bundleGraph[index++] = -1;
201151
continue;
202152
}
203153
const dep = map.get(depName)!;

0 commit comments

Comments
 (0)