Skip to content

Commit

Permalink
chore: more signal fine-tuning (#9531)
Browse files Browse the repository at this point in the history
* chore: opt for two signal data-structures to reduce memory usage
  • Loading branch information
trueadm authored Nov 18, 2023
1 parent bbd1a6c commit 298da65
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 190 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-dots-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

chore: more signal perf tuning
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ export const global_visitors = {
},
MemberExpression(node, { state, next }) {
if (node.object.type === 'ThisExpression') {
// rewrite `this.#foo` as `this.#foo.value` inside a constructor
// rewrite `this.#foo` as `this.#foo.v` inside a constructor
if (node.property.type === 'PrivateIdentifier') {
const field = state.private_state.get(node.property.name);

if (field) {
return state.in_constructor ? b.member(node, b.id('value')) : b.call('$.get', node);
return state.in_constructor ? b.member(node, b.id('v')) : b.call('$.get', node);
}
}

// rewrite `this.foo` as `this.#foo.value` inside a constructor
// rewrite `this.foo` as `this.#foo.v` inside a constructor
if (node.property.type === 'Identifier' && !node.computed) {
const field = state.public_state.get(node.property.name);

if (field && state.in_constructor) {
return b.member(b.member(b.this, field.id), b.id('value'));
return b.member(b.member(b.this, field.id), b.id('v'));
}
}
}
Expand Down
15 changes: 5 additions & 10 deletions packages/svelte/src/internal/client/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
EACH_IS_CONTROLLED,
EACH_INDEX_REACTIVE,
EACH_ITEM_REACTIVE,
EACH_IS_ANIMATED,
PassiveDelegatedEvents,
DelegatedEvents
} from '../../constants.js';
Expand Down Expand Up @@ -624,7 +623,7 @@ export function bind_playback_rate(media, get_value, update) {
// Needs to happen after the element is inserted into the dom, else playback will be set back to 1 by the browser.
// For hydration we could do it immediately but the additional code is not worth the lost microtask.

/** @type {import('./types.js').Signal | undefined} */
/** @type {import('./types.js').ComputationSignal | undefined} */
let render;
let destroyed = false;
const effect = managed_effect(() => {
Expand Down Expand Up @@ -2083,7 +2082,7 @@ export function update_each_item_block(block, item, index, type) {
if (transitions !== null && (type & EACH_KEYED) !== 0) {
let prev_index = block.index;
if (index_is_reactive) {
prev_index = /** @type {import('./types.js').Signal<number>} */ (prev_index).value;
prev_index = /** @type {import('./types.js').Signal<number>} */ (prev_index).v;
}
const items = block.parent.items;
if (prev_index !== index && /** @type {number} */ (index) < items.length) {
Expand Down Expand Up @@ -2125,7 +2124,7 @@ export function destroy_each_item_block(
if (!controlled && dom !== null) {
remove(dom);
}
destroy_signal(/** @type {import('./types.js').Signal} */ (block.effect));
destroy_signal(/** @type {import('./types.js').EffectSignal} */ (block.effect));
}
}

Expand Down Expand Up @@ -2244,11 +2243,7 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re
? []
: Array.from(maybe_array);
if (key_fn !== null) {
const length = array.length;
keys = Array(length);
for (let i = 0; i < length; i++) {
keys[i] = key_fn(array[i]);
}
keys = array.map(key_fn);
}
if (fallback_fn !== null) {
if (array.length === 0) {
Expand Down Expand Up @@ -3163,7 +3158,7 @@ export function mount(component, options) {
if (hydration_fragment !== null) {
remove(hydration_fragment);
}
destroy_signal(/** @type {import('./types.js').Signal} */ (block.effect));
destroy_signal(/** @type {import('./types.js').EffectSignal} */ (block.effect));
}
];
}
Expand Down
Loading

1 comment on commit 298da65

@vercel
Copy link

@vercel vercel bot commented on 298da65 Nov 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

svelte-5-preview – ./sites/svelte-5-preview

svelte-5-preview-svelte.vercel.app
svelte-octane.vercel.app
svelte-5-preview-git-main-svelte.vercel.app
svelte-5-preview.vercel.app

Please sign in to comment.