Skip to content

Commit 60acb25

Browse files
bajrangCoderclaude
andauthored
perf: smaller startup bundle, cached encodings and faster command registration (#2931)
* perf: shrink startup bundle and cache native encodings - Load lib/run (markdown-it, its plugins and the markdown preview) on first run/preview instead of in main.js. - Load mime-types and its MIME database on demand at each lookup site. - Skip load-time validation for bundled editor themes; they are still validated when applied, and plugin themes are still validated when registered. - Cache the Android charset list per app build, OS version and device model instead of rebuilding it natively on every launch. - Fetch the sidebar app chunks in parallel while adding them in their original order. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * perf: make command registration and keymap rebuilds cheap Each command registration (including every plugin addCommand) rebuilt the whole keymap, re-parsing both key strings for every pairwise conflict check, and immediately reconfigured every editor pane. Profiling on a device showed ~1.8s of main-thread CPU spent here while plugins loaded. - Cache canonicalized key strings; the result depends only on the input. - Find the first conflicting claim without copying the claimed-keys map for every candidate. - Rebuild the keymap lazily: registration marks it dirty and every reader rebuilds first, so reads stay current. The returned command still has its final key and description immediately. - Apply keymap refreshes once per burst in a microtask, which always runs before the next key event. - Drop the module-load rebuild that setKeyBindings immediately repeated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5a7cc93 commit 60acb25

17 files changed

Lines changed: 365 additions & 61 deletions

‎src/cm/commandRegistry.js‎

Lines changed: 77 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ let cachedKeymap = [];
151151
/** @type {Set<EditorView>} */
152152
const commandViews = new Set();
153153

154+
/**
155+
* Commands are often registered in bursts (a plugin adding several at once),
156+
* so the keymap is rebuilt lazily on the next read instead of per command.
157+
*/
158+
let keymapDirty = true;
159+
160+
/** @type {Set<EditorView>} views waiting for the updated keymap */
161+
const pendingKeymapViews = new Set();
162+
let keymapRefreshScheduled = false;
163+
154164
const CODEMIRROR_COMMAND_ENTRIES = Object.entries(cmCommands).filter(
155165
([name, value]) =>
156166
typeof value === "function" && CODEMIRROR_COMMAND_NAMES.has(name),
@@ -164,7 +174,6 @@ registerCoreCommands();
164174
registerLspCommands();
165175
registerLintCommands();
166176
registerCommandsFromKeyBindings();
167-
rebuildKeymap();
168177

169178
function registerCoreCommands() {
170179
addCommand({
@@ -1514,19 +1523,34 @@ function buildResolvedKeyBindingsSnapshot() {
15141523
);
15151524
}
15161525

1526+
/**
1527+
* Resolve a command's effective description and key from the bindings.
1528+
* @returns {string|null} the key source
1529+
*/
1530+
function syncCommandBinding(command, name) {
1531+
const bindingInfo = resolveBindingInfo(name);
1532+
command.description = bindingInfo?.description || command.defaultDescription;
1533+
command.key =
1534+
bindingInfo && Object.prototype.hasOwnProperty.call(bindingInfo, "key")
1535+
? bindingInfo.key
1536+
: (command.defaultKey ?? null);
1537+
return command.key;
1538+
}
1539+
1540+
function invalidateKeymap() {
1541+
keymapDirty = true;
1542+
}
1543+
1544+
function ensureKeymap() {
1545+
if (keymapDirty) rebuildKeymap();
1546+
}
1547+
15171548
function rebuildKeymap() {
15181549
cachedResolvedKeyBindings = buildResolvedKeyBindingsSnapshot();
15191550
const candidates = [];
15201551
let order = 0;
15211552
commandMap.forEach((command, name) => {
1522-
const bindingInfo = resolveBindingInfo(name);
1523-
command.description =
1524-
bindingInfo?.description || command.defaultDescription;
1525-
const keySource =
1526-
bindingInfo && Object.prototype.hasOwnProperty.call(bindingInfo, "key")
1527-
? bindingInfo.key
1528-
: (command.defaultKey ?? null);
1529-
command.key = keySource;
1553+
const keySource = syncCommandBinding(command, name);
15301554
const combos = parseKeyString(keySource);
15311555
combos.forEach((combo) => {
15321556
const cmKey = toCodeMirrorKey(combo);
@@ -1551,9 +1575,15 @@ function rebuildKeymap() {
15511575
const conflicts = [];
15521576
for (const candidate of candidates) {
15531577
const canonicalKey = canonicalizeKeyBinding(candidate.key);
1554-
const claimed = Array.from(claimedKeys.entries()).find(([key]) =>
1555-
keyBindingsConflict(key, canonicalKey),
1556-
);
1578+
// First conflicting claim in insertion order, without copying the map
1579+
// for every candidate.
1580+
let claimed = null;
1581+
for (const entry of claimedKeys) {
1582+
if (keyBindingsConflict(entry[0], canonicalKey)) {
1583+
claimed = entry;
1584+
break;
1585+
}
1586+
}
15571587
if (claimed) {
15581588
const [claimedKey, owner] = claimed;
15591589
const appCommandShadowsCodeMirrorDefault =
@@ -1596,6 +1626,7 @@ function rebuildKeymap() {
15961626
cachedKeyBindingConflicts = conflicts;
15971627
cachedKeymap = bindings;
15981628
resolvedKeyBindingsVersion += 1;
1629+
keymapDirty = false;
15991630
return bindings;
16001631
}
16011632

@@ -1643,6 +1674,7 @@ export function executeCommand(name, view, args) {
16431674
}
16441675

16451676
export function getRegisteredCommands() {
1677+
ensureKeymap();
16461678
return Array.from(commandMap.values()).map((command) => ({
16471679
name: command.name,
16481680
description: command.description || command.defaultDescription,
@@ -1651,22 +1683,27 @@ export function getRegisteredCommands() {
16511683
}
16521684

16531685
export function getResolvedKeyBindings() {
1686+
ensureKeymap();
16541687
return cachedResolvedKeyBindings;
16551688
}
16561689

16571690
export function getEffectiveKeyBindings() {
1691+
ensureKeymap();
16581692
return cachedEffectiveKeyBindings;
16591693
}
16601694

16611695
export function getKeyBindingConflicts() {
1696+
ensureKeymap();
16621697
return cachedKeyBindingConflicts.map((conflict) => ({ ...conflict }));
16631698
}
16641699

16651700
export function getResolvedKeyBindingsVersion() {
1701+
ensureKeymap();
16661702
return resolvedKeyBindingsVersion;
16671703
}
16681704

16691705
export function getCommandKeymapExtension() {
1706+
ensureKeymap();
16701707
return commandKeymapCompartment.of(keymap.of(cachedKeymap));
16711708
}
16721709

@@ -1754,9 +1791,11 @@ export function registerExternalCommand(descriptor = {}) {
17541791
const stored = commandMap.get(name);
17551792
if (stored) {
17561793
stored.key = normalized.key ?? stored.key;
1794+
// The returned command reflects its final binding right away.
1795+
syncCommandBinding(stored, name);
17571796
}
17581797

1759-
rebuildKeymap();
1798+
invalidateKeymap();
17601799
return stored;
17611800
}
17621801

@@ -1765,13 +1804,34 @@ export function removeExternalCommand(name) {
17651804
const exists = commandMap.has(name);
17661805
if (!exists) return false;
17671806
commandMap.delete(name);
1768-
rebuildKeymap();
1807+
invalidateKeymap();
17691808
return true;
17701809
}
17711810

1811+
/**
1812+
* Apply the current keymap to a view. Calls made in the same task are applied
1813+
* together in a microtask, which always runs before the next key event.
1814+
*/
17721815
export function refreshCommandKeymap(view) {
17731816
const resolvedView = resolveView(view);
1774-
applyCommandKeymap(resolvedView);
1817+
if (!resolvedView) return;
1818+
pendingKeymapViews.add(resolvedView);
1819+
if (keymapRefreshScheduled) return;
1820+
keymapRefreshScheduled = true;
1821+
Promise.resolve().then(flushKeymapRefresh);
1822+
}
1823+
1824+
function flushKeymapRefresh() {
1825+
keymapRefreshScheduled = false;
1826+
const views = Array.from(pendingKeymapViews);
1827+
pendingKeymapViews.clear();
1828+
for (const view of views) {
1829+
try {
1830+
applyCommandKeymap(view);
1831+
} catch (error) {
1832+
console.error("Failed to apply command keymap", error);
1833+
}
1834+
}
17751835
}
17761836

17771837
function normalizeExternalCommand(descriptor) {
@@ -1827,8 +1887,9 @@ function normalizeExternalKey(bindKey) {
18271887
return combos.length ? combos.join("|") : null;
18281888
}
18291889

1830-
function applyCommandKeymap(view, bindings = cachedKeymap) {
1890+
function applyCommandKeymap(view, bindings) {
18311891
if (!view) return;
1892+
ensureKeymap();
18321893
commandViews.add(view);
18331894
view.dispatch({
18341895
effects: commandKeymapCompartment.reconfigure(

‎src/cm/keyBindingUtils.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,18 @@ export function toCodeMirrorKey(combo) {
4545
return strokes.length ? strokes.join(" ") : null;
4646
}
4747

48+
// Conflict checks compare every binding with every other one each time the
49+
// keymap is rebuilt (once per registered command), so cache the parsed form.
50+
const canonicalKeyCache = new Map();
51+
4852
export function canonicalizeKeyBinding(combo) {
49-
return toCodeMirrorKey(combo)?.toLowerCase() || null;
53+
if (typeof combo !== "string") {
54+
return toCodeMirrorKey(combo)?.toLowerCase() || null;
55+
}
56+
if (canonicalKeyCache.has(combo)) return canonicalKeyCache.get(combo);
57+
const canonicalKey = toCodeMirrorKey(combo)?.toLowerCase() || null;
58+
canonicalKeyCache.set(combo, canonicalKey);
59+
return canonicalKey;
5060
}
5161

5262
/**

‎src/cm/themes/index.js‎

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,19 @@ function resolveThemeEntryExtensions(theme, fallbackExtensions) {
116116
}
117117

118118
export function addTheme(id, caption, isDark, getExtension, config = null) {
119+
return registerTheme(id, caption, isDark, getExtension, config, true);
120+
}
121+
122+
/**
123+
* Registers a bundled theme. These are known to be valid, so the up-front
124+
* validation (which builds every theme's extensions at startup) is skipped;
125+
* they are still validated when applied.
126+
*/
127+
function addBuiltInTheme(id, caption, isDark, getExtension, config = null) {
128+
return registerTheme(id, caption, isDark, getExtension, config, false);
129+
}
130+
131+
function registerTheme(id, caption, isDark, getExtension, config, validate) {
119132
const key = String(id || "")
120133
.trim()
121134
.toLowerCase();
@@ -129,7 +142,7 @@ export function addTheme(id, caption, isDark, getExtension, config = null) {
129142
config: config || null,
130143
};
131144

132-
if (!validateThemeExtensions(key, theme.getExtension())) {
145+
if (validate && !validateThemeExtensions(key, theme.getExtension())) {
133146
return false;
134147
}
135148

@@ -164,115 +177,127 @@ export function removeTheme(id) {
164177
themes.delete(String(id).toLowerCase());
165178
}
166179

167-
addTheme("one_dark", "One Dark", true, () => [oneDark], oneDarkConfig);
168-
addTheme(auraConfig.name, "Aura", !!auraConfig.dark, () => aura(), auraConfig);
169-
addTheme(
180+
addBuiltInTheme("one_dark", "One Dark", true, () => [oneDark], oneDarkConfig);
181+
addBuiltInTheme(
182+
auraConfig.name,
183+
"Aura",
184+
!!auraConfig.dark,
185+
() => aura(),
186+
auraConfig,
187+
);
188+
addBuiltInTheme(
170189
cobaltConfig.name,
171190
"Cobalt",
172191
!!cobaltConfig.dark,
173192
() => cobalt(),
174193
cobaltConfig,
175194
);
176-
addTheme(
195+
addBuiltInTheme(
177196
noctisLilacConfig.name,
178197
noctisLilacConfig.caption || "Noctis Lilac",
179198
!!noctisLilacConfig.dark,
180199
() => noctisLilac(),
181200
noctisLilacConfig,
182201
);
183-
addTheme(
202+
addBuiltInTheme(
184203
draculaConfig.name,
185204
"Dracula",
186205
!!draculaConfig.dark,
187206
() => dracula(),
188207
draculaConfig,
189208
);
190-
addTheme(nordConfig.name, "Nord", !!nordConfig.dark, () => nord(), nordConfig);
191-
addTheme(
209+
addBuiltInTheme(
210+
nordConfig.name,
211+
"Nord",
212+
!!nordConfig.dark,
213+
() => nord(),
214+
nordConfig,
215+
);
216+
addBuiltInTheme(
192217
gruvboxDarkConfig.name,
193218
"Gruvbox Dark",
194219
!!gruvboxDarkConfig.dark,
195220
() => gruvboxDark(),
196221
gruvboxDarkConfig,
197222
);
198-
addTheme(
223+
addBuiltInTheme(
199224
ayuDarkConfig.name,
200225
"Ayu Dark",
201226
!!ayuDarkConfig.dark,
202227
() => ayuDark(),
203228
ayuDarkConfig,
204229
);
205-
addTheme(
230+
addBuiltInTheme(
206231
materialPalenightConfig.name,
207232
"Material Palenight",
208233
!!materialPalenightConfig.dark,
209234
() => materialPalenight(),
210235
materialPalenightConfig,
211236
);
212-
addTheme(
237+
addBuiltInTheme(
213238
githubDarkConfig.name,
214239
"GitHub Dark",
215240
!!githubDarkConfig.dark,
216241
() => githubDark(),
217242
githubDarkConfig,
218243
);
219-
addTheme(
244+
addBuiltInTheme(
220245
githubLightConfig.name,
221246
"GitHub Light",
222247
!!githubLightConfig.dark,
223248
() => githubLight(),
224249
githubLightConfig,
225250
);
226-
addTheme(
251+
addBuiltInTheme(
227252
solarizedDarkConfig.name,
228253
"Solarized Dark",
229254
!!solarizedDarkConfig.dark,
230255
() => solarizedDark(),
231256
solarizedDarkConfig,
232257
);
233-
addTheme(
258+
addBuiltInTheme(
234259
solarizedLightConfig.name,
235260
"Solarized Light",
236261
!!solarizedLightConfig.dark,
237262
() => solarizedLight(),
238263
solarizedLightConfig,
239264
);
240-
addTheme(
265+
addBuiltInTheme(
241266
tokyoNightDayConfig.name,
242267
"Tokyo Night Day",
243268
!!tokyoNightDayConfig.dark,
244269
() => tokyoNightDay(),
245270
tokyoNightDayConfig,
246271
);
247-
addTheme(
272+
addBuiltInTheme(
248273
tokyoNightConfig.name,
249274
"Tokyo Night",
250275
!!tokyoNightConfig.dark,
251276
() => tokyoNight(),
252277
tokyoNightConfig,
253278
);
254-
addTheme(
279+
addBuiltInTheme(
255280
tomorrowNightConfig.name,
256281
"Tomorrow Night",
257282
!!tomorrowNightConfig.dark,
258283
() => tomorrowNight(),
259284
tomorrowNightConfig,
260285
);
261-
addTheme(
286+
addBuiltInTheme(
262287
tomorrowNightBrightConfig.name,
263288
"Tomorrow Night Bright",
264289
!!tomorrowNightBrightConfig.dark,
265290
() => tomorrowNightBright(),
266291
tomorrowNightBrightConfig,
267292
);
268-
addTheme(
293+
addBuiltInTheme(
269294
monokaiConfig.name,
270295
"Monokai",
271296
!!monokaiConfig.dark,
272297
() => monokai(),
273298
monokaiConfig,
274299
);
275-
addTheme(
300+
addBuiltInTheme(
276301
vscodeDarkConfig.name,
277302
"VS Code Dark",
278303
!!vscodeDarkConfig.dark,
@@ -281,7 +306,7 @@ addTheme(
281306
);
282307

283308
for (const config of catppuccinConfigs) {
284-
addTheme(
309+
addBuiltInTheme(
285310
config.name,
286311
config.caption,
287312
config.dark,

0 commit comments

Comments
 (0)