Skip to content

Commit 550cecf

Browse files
authored
chore: shave off more bytes (#9540)
* shavings * dro unused max safe int * get rid of array_ref and object_ref * extract STATUS_MASK * fix test * revert is_controlled change * more run_all
1 parent bd2a586 commit 550cecf

File tree

7 files changed

+64
-58
lines changed

7 files changed

+64
-58
lines changed

packages/svelte/src/internal/client/reconciler.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,7 @@ export function reconcile_tracked_array(
341341
insert_each_item_block(block, dom, is_controlled, null);
342342
}
343343
} else {
344-
var should_update_block =
345-
(flags & EACH_ITEM_REACTIVE) !== 0 || (flags & EACH_INDEX_REACTIVE) !== 0;
344+
var should_update_block = (flags & (EACH_ITEM_REACTIVE | EACH_INDEX_REACTIVE)) !== 0;
346345
var start = 0;
347346

348347
/** @type {null | Text | Element | Comment} */

packages/svelte/src/internal/client/render.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,8 +2256,8 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re
22562256
);
22572257
push_destroy_fn(each, () => {
22582258
const flags = block.f;
2259-
const is_controlled = (flags & EACH_IS_CONTROLLED) !== 0;
22602259
const anchor_node = block.a;
2260+
const is_controlled = (flags & EACH_IS_CONTROLLED) !== 0;
22612261
let fallback = current_fallback;
22622262
while (fallback !== null) {
22632263
const dom = fallback.d;
@@ -3081,7 +3081,10 @@ export function mount(component, options) {
30813081
if (options.recover !== false && hydration_fragment !== null) {
30823082
// eslint-disable-next-line no-console
30833083
console.error(
3084-
'Hydration failed because the initial UI does not match what was rendered on the server.',
3084+
'ERR_SVELTE_HYDRATION_MISMATCH' +
3085+
(DEV
3086+
? ': Hydration failed because the initial UI does not match what was rendered on the server.'
3087+
: ''),
30853088
error
30863089
);
30873090
remove(hydration_fragment);

packages/svelte/src/internal/client/runtime.js

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { DEV } from 'esm-env';
12
import { subscribe_to_store } from '../../store/utils.js';
2-
import { EMPTY_FUNC } from '../common.js';
3+
import { EMPTY_FUNC, run_all } from '../common.js';
34
import { unwrap } from './render.js';
45
import { is_array } from './utils.js';
56

@@ -21,7 +22,6 @@ const IS_EFFECT = EFFECT | PRE_EFFECT | RENDER_EFFECT | SYNC_EFFECT;
2122

2223
const FLUSH_MICROTASK = 0;
2324
const FLUSH_SYNC = 1;
24-
const MAX_SAFE_INT = Number.MAX_SAFE_INTEGER;
2525

2626
export const UNINITIALIZED = Symbol();
2727

@@ -457,8 +457,11 @@ function flush_queued_effects(effects) {
457457
if (length > 0) {
458458
if (flush_count > 100) {
459459
throw new Error(
460-
'Maximum update depth exceeded. This can happen when a reactive block or effect ' +
461-
'repeatedly sets a new value. Svelte limits the number of nested updates to prevent infinite loops.'
460+
'ERR_SVELTE_TOO_MANY_UPDATES' +
461+
(DEV
462+
? ': Maximum update depth exceeded. This can happen when a reactive block or effect ' +
463+
'repeatedly sets a new value. Svelte limits the number of nested updates to prevent infinite loops.'
464+
: '')
462465
);
463466
}
464467
flush_count++;
@@ -524,9 +527,7 @@ function process_task() {
524527
is_task_queued = false;
525528
const tasks = current_queued_tasks.slice();
526529
current_queued_tasks = [];
527-
for (let i = 0; i < tasks.length; i++) {
528-
tasks[i]();
529-
}
530+
run_all(tasks);
530531
}
531532

532533
/**
@@ -968,9 +969,12 @@ export function set_signal_value(signal, value) {
968969
(current_consumer.f & DERIVED) !== 0
969970
) {
970971
throw new Error(
971-
"Unsafe mutations during Svelte's render or derived phase are not permitted in runes mode. " +
972-
'This can lead to unexpected errors and possibly cause infinite loops.\n\nIf this mutation is not meant ' +
973-
'to be reactive do not use the "$state" rune for that declaration.'
972+
'ERR_SVELTE_UNSAFE_MUTATION' +
973+
(DEV
974+
? ": Unsafe mutations during Svelte's render or derived phase are not permitted in runes mode. " +
975+
'This can lead to unexpected errors and possibly cause infinite loops.\n\nIf this mutation is not meant ' +
976+
'to be reactive do not use the "$state" rune for that declaration.'
977+
: '')
974978
);
975979
}
976980
if (
@@ -1005,10 +1009,10 @@ export function set_signal_value(signal, value) {
10051009
if (current_effect === null && current_queued_pre_and_render_effects.length === 0) {
10061010
const update_callbacks = component_context?.u;
10071011
if (update_callbacks != null) {
1008-
update_callbacks.b.forEach(/** @param {any} c */ (c) => c());
1012+
run_all(update_callbacks.b);
10091013
const managed = managed_effect(() => {
10101014
destroy_signal(managed);
1011-
update_callbacks.a.forEach(/** @param {any} c */ (c) => c());
1015+
run_all(update_callbacks.a);
10121016
});
10131017
}
10141018
}
@@ -1026,21 +1030,20 @@ export function destroy_signal(signal) {
10261030
const flags = signal.f;
10271031
destroy_references(signal);
10281032
remove_consumer(signal, 0, true);
1029-
signal.i = null;
1030-
signal.r = null;
1031-
signal.y = null;
1032-
signal.x = null;
1033-
signal.b = null;
1034-
signal.v = /** @type {V} */ (null);
1035-
signal.d = null;
1036-
signal.c = null;
1033+
signal.i =
1034+
signal.r =
1035+
signal.y =
1036+
signal.x =
1037+
signal.b =
1038+
// @ts-expect-error - this is fine, since we're assigning to null to clear out a destroyed signal
1039+
signal.v =
1040+
signal.d =
1041+
signal.c =
1042+
null;
10371043
set_signal_status(signal, DESTROYED);
10381044
if (destroy !== null) {
10391045
if (is_array(destroy)) {
1040-
let i;
1041-
for (i = 0; i < destroy.length; i++) {
1042-
destroy[i]();
1043-
}
1046+
run_all(destroy);
10441047
} else {
10451048
destroy();
10461049
}
@@ -1146,7 +1149,10 @@ function internal_create_effect(type, init, sync, block, schedule) {
11461149
*/
11471150
export function user_effect(init) {
11481151
if (current_effect === null) {
1149-
throw new Error('The Svelte $effect rune can only be used during component initialisation.');
1152+
throw new Error(
1153+
'ERR_SVELTE_ORPHAN_EFFECT' +
1154+
(DEV ? ': The Svelte $effect rune can only be used during component initialisation.' : '')
1155+
);
11501156
}
11511157
const apply_component_effect_heuristics =
11521158
current_effect.f & RENDER_EFFECT &&
@@ -1203,7 +1209,10 @@ export function managed_pre_effect(init, sync) {
12031209
export function pre_effect(init) {
12041210
if (current_effect === null) {
12051211
throw new Error(
1206-
'The Svelte $effect.pre rune can only be used during component initialisation.'
1212+
'ERR_SVELTE_ORPHAN_EFFECT' +
1213+
(DEV
1214+
? ': The Svelte $effect.pre rune can only be used during component initialisation.'
1215+
: '')
12071216
);
12081217
}
12091218
const sync = current_effect !== null && (current_effect.f & RENDER_EFFECT) !== 0;
@@ -1273,24 +1282,15 @@ export function push_destroy_fn(signal, destroy_fn) {
12731282
}
12741283
}
12751284

1285+
const STATUS_MASK = ~(DIRTY | MAYBE_DIRTY | CLEAN);
12761286
/**
12771287
* @template V
12781288
* @param {import('./types.js').Signal<V>} signal
12791289
* @param {number} status
12801290
* @returns {void}
12811291
*/
12821292
export function set_signal_status(signal, status) {
1283-
const flags = signal.f;
1284-
if ((flags & status) === 0) {
1285-
if ((flags & MAYBE_DIRTY) !== 0) {
1286-
signal.f ^= MAYBE_DIRTY;
1287-
} else if ((flags & CLEAN) !== 0) {
1288-
signal.f ^= CLEAN;
1289-
} else if ((flags & DIRTY) !== 0) {
1290-
signal.f ^= DIRTY;
1291-
}
1292-
signal.f ^= status;
1293-
}
1293+
signal.f = (signal.f & STATUS_MASK) | status;
12941294
}
12951295

12961296
/**
@@ -1484,7 +1484,10 @@ export function safe_equal(a, b) {
14841484
export function get_or_init_context_map() {
14851485
const component_context = current_component_context;
14861486
if (component_context === null) {
1487-
throw new Error('Context can only be used during component initialisation.');
1487+
throw new Error(
1488+
'ERR_SVELTE_ORPHAN_CONTEXT' +
1489+
(DEV ? 'Context can only be used during component initialisation.' : '')
1490+
);
14881491
}
14891492
let context_map = component_context.c;
14901493
if (context_map === null) {

packages/svelte/src/internal/client/transitions.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { EACH_IS_ANIMATED, EACH_IS_CONTROLLED } from '../../constants.js';
2+
import { run_all } from '../common.js';
23
import {
34
AWAIT_BLOCK,
45
DYNAMIC_COMPONENT_BLOCK,
@@ -322,9 +323,7 @@ function create_transition(dom, init, direction, effect) {
322323
const is_outro = curr_direction === 'out';
323324
/** @type {Animation | TickAnimation} */ (animation).pause();
324325
if (is_outro) {
325-
for (const sub of subs) {
326-
sub();
327-
}
326+
run_all(subs);
328327
subs = [];
329328
}
330329
dispatch_event(dom, is_outro ? 'outroend' : 'introend');
@@ -378,9 +377,7 @@ function create_transition(dom, init, direction, effect) {
378377
},
379378
// cleanup
380379
x() {
381-
for (const sub of subs) {
382-
sub();
383-
}
380+
run_all(subs);
384381
subs = [];
385382
},
386383
r: direction,
@@ -554,7 +551,7 @@ export function trigger_transitions(transitions, target_direction, from) {
554551
destroy_signal(e);
555552
const e2 = managed_effect(() => {
556553
destroy_signal(e2);
557-
outros.forEach(/** @param {any} o */ (o) => o());
554+
run_all(outros);
558555
});
559556
}, false);
560557
}
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
// Store the references to globals in case someone tries to monkey patch these, causing the below
22
// to de-opt (this occurs often when using popular extensions).
3-
export const object_ref = Object;
4-
export const array_ref = Array;
5-
6-
export const is_array = array_ref.isArray;
7-
export const array_from = array_ref.from;
8-
export const define_property = object_ref.defineProperty;
9-
export const get_descriptor = object_ref.getOwnPropertyDescriptor;
10-
export const get_descriptors = object_ref.getOwnPropertyDescriptors;
3+
export var is_array = Array.isArray;
4+
export var array_from = Array.from;
5+
export var define_property = Object.defineProperty;
6+
export var get_descriptor = Object.getOwnPropertyDescriptor;
7+
export var get_descriptors = Object.getOwnPropertyDescriptors;

packages/svelte/src/internal/common.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ export function is_promise(value) {
1616
typeof (/** @type {any} */ (value).then) === 'function'
1717
);
1818
}
19+
20+
/** @param {Array<() => void>} arr */
21+
export function run_all(arr) {
22+
for (var i = 0; i < arr.length; i++) {
23+
arr[i]();
24+
}
25+
}

packages/svelte/tests/hydration/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const { test, run } = suite<HydrationTest>(async (config, cwd) => {
7171
const error = console.error;
7272
let got_hydration_error = false;
7373
console.error = (message: any) => {
74-
if (typeof message === 'string' && message.startsWith('Hydration failed')) {
74+
if (typeof message === 'string' && message.startsWith('ERR_SVELTE_HYDRATION_MISMATCH')) {
7575
got_hydration_error = true;
7676
if (!config.expect_hydration_error) {
7777
error(message);

0 commit comments

Comments
 (0)