-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
chore: very minor perf improvement when fallback values are used #11921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
I vaguely remember having added this due to some code transformations. Now that this function looks exactly like its synchronous counterpart we can just remove it in favor of the sync one. |
I'm trying to wrap my head round whether we need any of this. By commenting stuff out and seeing which tests fail, I see that this... let value = $state(0);
const update = async () => {
({ value = await Promise.resolve(1) } = { value: 2 });
} ...results in this: const update = async (_, value) => {
await (async () => {
const tmp = { value: 2 };
(
$.set(value, $.proxy(await $.value_or_fallback_async(tmp.value, async () => await Promise.resolve(1))))
);
return tmp;
})();
}; Why isn't it just this? const update = async (_, value) => {
const tmp = { value: 2 };
$.set(value, $.proxy(tmp.value !== undefined ? tmp.value : await Promise.resolve(1)))
}; Are there cases where we really need all that machinery? |
Sadly yes: let value = $state(0);
const update = async () => {
let x = { foo: ({ value = await Promise.resolve(1), x = await value } = { value: 2 })};
} |
99% of For the edge cases that remain, I'm pretty sure we can still make things a lot simpler. input: <script>
let value = $state(0);
const update = async () => {
({ value = await one() } = { value: await two() });
let x = {
foo: ({ value = await three() } = { value: await four() })
};
}
</script> output: import * as $ from "svelte/internal/client";
export default function App($$anchor) {
let value = $.source(0);
const update = async () => {
await (async () => {
const tmp = { value: await two() };
(
$.set(value, $.proxy(await $.value_or_fallback_async(tmp.value, async () => await one())))
);
return tmp;
})();
let x = {
foo: await (async () => {
const tmp_1 = { value: await four() };
(
$.set(value, $.proxy(await $.value_or_fallback_async(tmp_1.value, async () => await three())))
);
return tmp_1;
})()
};
};
} better output (three fewer import * as $ from "svelte/internal/client";
export default function App($$anchor) {
let value = $.source(0);
const update = async () => {
const tmp = { value: await two() };
$.set(value, $.proxy(tmp.value !== undefined ? tmp.value : await one()));
let x = {
foo: await (async (tmp_1) => {
$.set(value, $.proxy(tmp.value !== undefined ? tmp.value : await three()));
return tmp_1;
})({ value: await four() })
};
};
} |
Should we postpone this to 5.x? Feels like a micro optimization we can do later any time. |
Yes, we should refactor |
From this discussion #11842 (comment) it seems best for performance to avoid having unnecessary
async
functionsThis is a pretty minimal change, so feel free to close it if it's not worth it. But I noticed while investigating that this was the only place in the codebase where this occurs, so I figured I might as well update it