Stop the settings write loop between windows
Two or more windows saved the appearance section to each other without end. The host stores object keys in alphabetical order. ThemeSync compared the stored value and the normalized value as JSON text, so a different key order started a new save in each window. Each save replaced settings.json, about 150 times each second. On a SATA SSD without a DRAM cache, these writes stalled all disk access for 6 to 8 seconds, about every 40 seconds. - ThemeSync compares the two values with sameSettingsValue, which ignores the key order. - The host skips a write when the section already holds the value, and sends no settings-changed event. This also stops a similar loop in another section. - New tests: key order in sameSettingsValue, an appearance from the host that needs no new save, and an unchanged host write.
This commit is contained in:
@@ -56,6 +56,23 @@ function encode(section: SettingsSection, value: unknown): string {
|
||||
return section === 'language' && typeof value === 'string' ? value : JSON.stringify(value);
|
||||
}
|
||||
|
||||
// The same value with the object keys in alphabetical order, at every depth.
|
||||
function sortKeys(value: unknown): unknown {
|
||||
if (Array.isArray(value)) return value.map(sortKeys);
|
||||
if (!value || typeof value !== 'object') return value;
|
||||
const source = value as Record<string, unknown>;
|
||||
return Object.fromEntries(Object.keys(source).sort().map(key => [key, sortKeys(source[key])]));
|
||||
}
|
||||
|
||||
/**
|
||||
* True when two section values hold the same data. The key order of objects does not matter.
|
||||
* The desktop host stores the keys in alphabetical order, so a value that comes back from the host
|
||||
* can have a different key order than the value that a window saved.
|
||||
*/
|
||||
export function sameSettingsValue(a: unknown, b: unknown): boolean {
|
||||
return JSON.stringify(sortKeys(a)) === JSON.stringify(sortKeys(b));
|
||||
}
|
||||
|
||||
function notify(section: SettingsSection) {
|
||||
for (const callback of [...subscribers.get(section) ?? []]) {
|
||||
try { callback(); } catch (reason) { console.error(`A ${section} settings subscriber failed.`, reason); }
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
import { syncTableFormulas } from '$lib/formulas';
|
||||
import { syncFileTitle } from '$lib/editor/fileTitle';
|
||||
import { onMount } from 'svelte';
|
||||
import { onSettingsChange, readSection, writeSection } from '$lib/settings/store';
|
||||
import { onSettingsChange, readSection, sameSettingsValue, writeSection } from '$lib/settings/store';
|
||||
import { applyAppearance, readAppearance } from './appearance';
|
||||
onMount(() => {
|
||||
const stopLocale = syncLocale();
|
||||
@@ -14,7 +14,8 @@
|
||||
const stored = readSection('appearance');
|
||||
const appearance = readAppearance();
|
||||
// Save the normalized value, for example a new ID for a renamed theme.
|
||||
if (stored !== undefined && JSON.stringify(stored) !== JSON.stringify(appearance)) {
|
||||
// Ignore the key order: the host sorts the keys, so a text compare saves again in each window without end.
|
||||
if (stored !== undefined && !sameSettingsValue(stored, appearance)) {
|
||||
writeSection('appearance', appearance).catch(() => { /* The normalized value applies until the next save. */ });
|
||||
}
|
||||
applyAppearance(appearance);
|
||||
|
||||
Reference in New Issue
Block a user