Skip to content

Commit eb80b77

Browse files
committed
chore: avoid microtasks when flushing sync
1 parent c051a6e commit eb80b77

File tree

3 files changed

+55
-40
lines changed

3 files changed

+55
-40
lines changed

.changeset/polite-melons-tickle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
chore: avoid microtasks when flushing sync

benchmarking/benchmarks/reactivity/index.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,32 @@ import {
2424
// Not all tests are the same, and many parts have been tweaked to capture different data.
2525

2626
export const reactivity_benchmarks = [
27-
sbench_create_signals,
28-
sbench_create_0to1,
29-
sbench_create_1to1,
30-
sbench_create_2to1,
31-
sbench_create_4to1,
32-
sbench_create_1000to1,
33-
sbench_create_1to2,
34-
sbench_create_1to4,
35-
sbench_create_1to8,
36-
sbench_create_1to1000,
37-
kairo_avoidable_owned,
38-
kairo_avoidable_unowned,
39-
kairo_broad_owned,
40-
kairo_broad_unowned,
41-
kairo_deep_owned,
42-
kairo_deep_unowned,
27+
// sbench_create_signals,
28+
// sbench_create_0to1,
29+
// sbench_create_1to1,
30+
// sbench_create_2to1,
31+
// sbench_create_4to1,
32+
// sbench_create_1000to1,
33+
// sbench_create_1to2,
34+
// sbench_create_1to4,
35+
// sbench_create_1to8,
36+
// sbench_create_1to1000,
37+
// kairo_avoidable_owned,
38+
// kairo_avoidable_unowned,
39+
// kairo_broad_owned,
40+
// kairo_broad_unowned,
41+
// kairo_deep_owned,
42+
// kairo_deep_unowned,
4343
kairo_diamond_owned,
4444
kairo_diamond_unowned,
45-
kairo_triangle_owned,
46-
kairo_triangle_unowned,
47-
kairo_mux_owned,
48-
kairo_mux_unowned,
49-
kairo_repeated_owned,
50-
kairo_repeated_unowned,
51-
kairo_unstable_owned,
52-
kairo_unstable_unowned,
53-
mol_bench_owned,
54-
mol_bench_unowned
45+
// kairo_triangle_owned,
46+
// kairo_triangle_unowned,
47+
// kairo_mux_owned,
48+
// kairo_mux_unowned,
49+
// kairo_repeated_owned,
50+
// kairo_repeated_unowned,
51+
// kairo_unstable_owned,
52+
// kairo_unstable_unowned,
53+
// mol_bench_owned,
54+
// mol_bench_unowned
5555
];

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const handled_errors = new WeakSet();
4747
let is_throwing_error = false;
4848

4949
let is_flushing = false;
50+
let is_flushing_sync = false;
5051

5152
/** @type {Effect | null} */
5253
let last_scheduled_effect = null;
@@ -734,7 +735,9 @@ function flush_queued_effects(effects) {
734735
export function schedule_effect(signal) {
735736
if (!is_flushing) {
736737
is_flushing = true;
737-
queueMicrotask(flush_queued_root_effects);
738+
if (!is_flushing_sync) {
739+
queueMicrotask(flush_queued_root_effects);
740+
}
738741
}
739742

740743
var effect = (last_scheduled_effect = signal);
@@ -818,23 +821,30 @@ function process_effects(root) {
818821
* @returns {T}
819822
*/
820823
export function flushSync(fn) {
821-
var result;
822-
823-
if (fn) {
824-
is_flushing = true;
825-
flush_queued_root_effects();
826-
result = fn();
827-
}
824+
var previously_flushing_sync = is_flushing_sync;
828825

829-
flush_tasks();
826+
try {
827+
var result;
828+
is_flushing_sync = true;
830829

831-
while (queued_root_effects.length > 0) {
832-
is_flushing = true;
833-
flush_queued_root_effects();
830+
if (fn) {
831+
is_flushing = true;
832+
flush_queued_root_effects();
833+
result = fn();
834+
}
835+
834836
flush_tasks();
837+
838+
while (queued_root_effects.length > 0) {
839+
is_flushing = true;
840+
flush_queued_root_effects();
841+
flush_tasks();
842+
}
843+
844+
return /** @type {T} */ (result);
845+
} finally {
846+
is_flushing_sync = previously_flushing_sync;
835847
}
836-
837-
return /** @type {T} */ (result);
838848
}
839849

840850
/**

0 commit comments

Comments
 (0)