Skip to content

feat: more efficient ownership widening #11136

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .changeset/mighty-frogs-obey.md

This file was deleted.

113 changes: 46 additions & 67 deletions packages/svelte/src/internal/client/dev/ownership.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** @typedef {{ file: string, line: number, column: number }} Location */

import { STATE_SYMBOL } from '../constants.js';
import { unstate } from '../proxy.js';
import { untrack } from '../runtime.js';
import { get_descriptors } from '../utils.js';

Expand Down Expand Up @@ -92,88 +93,44 @@ export function mark_module_end() {
}
}

let add_owner_visited = new Set();

/**
*
* @param {any} object
* @param {any} owner
*/
export function add_owner(object, owner) {
// Needed because ownership addition can invoke getters on a proxy,
// calling add_owner anew, so just keeping the set as part of
// add_owner_to_object would not be enough.
const prev = add_owner_visited;
try {
add_owner_visited = new Set(add_owner_visited);
untrack(() => {
add_owner_to_object(object, owner, add_owner_visited);
});
} finally {
add_owner_visited = prev;
}
untrack(() => {
var previous_owner = current_owner;
current_owner = owner;
add_owner_to_object(object);
current_owner = previous_owner;
});
}

/**
* @param {any} object
* @param {Function} owner
* @param {Set<any>} visited
*/
function add_owner_to_object(object, owner, visited) {
if (visited.has(object)) return;
visited.add(object);

if (object?.[STATE_SYMBOL]?.o && !object[STATE_SYMBOL].o.has(owner)) {
object[STATE_SYMBOL].o.add(owner);
}
// Not inside previous if-block; there could be normal objects in-between
traverse_for_owners(object, (nested) => add_owner_to_object(nested, owner, visited));
}

let strip_owner_visited = new Set();
/** @type {any} */
export var current_owner = null;

/**
* @param {any} object
* @param {Set<any>} [seen]
*/
export function strip_owner(object) {
// Needed because ownership stripping can invoke getters on a proxy,
// calling strip_owner anew, so just keeping the set as part of
// strip_owner_from_object would not be enough.
const prev = strip_owner_visited;
try {
untrack(() => {
strip_owner_from_object(object, strip_owner_visited);
});
} finally {
strip_owner_visited = prev;
function add_owner_to_object(object, seen = new Set()) {
if (seen.has(object) || typeof object !== 'object' || object === null) {
return;
}
}

/**
* @param {any} object
* @param {Set<any>} visited
*/
function strip_owner_from_object(object, visited) {
if (visited.has(object)) return;
visited.add(object);

if (object?.[STATE_SYMBOL]?.o) {
object[STATE_SYMBOL].o = null;
}
// Not inside previous if-block; there could be normal objects in-between
traverse_for_owners(object, (nested) => strip_owner_from_object(nested, visited));
}

/**
* @param {any} object
* @param {(obj: any) => void} cb
*/
function traverse_for_owners(object, cb) {
if (typeof object === 'object' && object !== null && !(object instanceof EventTarget)) {
for (const key in object) {
cb(object[key]);
object[STATE_SYMBOL].o.add(current_owner);
} else {
seen.add(object);

for (let key in object) {
try {
add_owner_to_object(object[key], seen);
} catch (e) {
// continue
}
}
// deal with state on classes
const proto = Object.getPrototypeOf(object);
if (
proto !== Object.prototype &&
Expand All @@ -187,7 +144,7 @@ function traverse_for_owners(object, cb) {
const get = descriptors[key].get;
if (get) {
try {
cb(object[key]);
get.call(object);
} catch (e) {
// continue
}
Expand All @@ -197,6 +154,28 @@ function traverse_for_owners(object, cb) {
}
}

/**
* @param {any} object
*/
export function strip_owner(object) {
untrack(() => {
strip_owner_from_object(object);
});
}

/**
* @param {any} object
*/
function strip_owner_from_object(object) {
if (object?.[STATE_SYMBOL]?.o) {
object[STATE_SYMBOL].o = null;

for (const key in object) {
strip_owner(object[key]);
}
}
}

/**
* @param {Set<Function>} owners
*/
Expand Down
6 changes: 5 additions & 1 deletion packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
ROOT_EFFECT
} from './constants.js';
import { flush_tasks } from './dom/task.js';
import { add_owner } from './dev/ownership.js';
import { add_owner, current_owner } from './dev/ownership.js';
import { mutate, set, source } from './reactivity/sources.js';
import { update_derived } from './reactivity/deriveds.js';

Expand Down Expand Up @@ -758,6 +758,10 @@ export function get(signal) {
}
}

if (current_owner !== null && signal.v?.[STATE_SYMBOL]?.o) {
signal.v[STATE_SYMBOL].o.add(current_owner);
}

return signal.v;
}

Expand Down
12 changes: 12 additions & 0 deletions packages/svelte/src/internal/client/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,15 @@ export function map_get(map, key) {
export function is_function(thing) {
return typeof thing === 'function';
}

/** @param {any} object */
export function is_exotic_object(object) {
const proto = Object.getPrototypeOf(object);
return (
proto !== Object.prototype &&
proto !== Array.prototype &&
proto !== Map.prototype &&
proto !== Set.prototype &&
proto !== Date.prototype
);
}