Skip to content
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

perf: optimize imports that are not mutated or reassigned #8948

Merged
merged 6 commits into from Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/svelte/src/compiler/compile/Component.js
Expand Up @@ -772,12 +772,23 @@ export default class Component {
}
const writable =
node.type === 'VariableDeclaration' && (node.kind === 'var' || node.kind === 'let');
const imported = node.type.startsWith('Import');

let immutable = false;
if (node.type === 'VariableDeclaration' && node.kind === 'const') {
immutable = true;
for (const declaration of node.declarations) {
benmccann marked this conversation as resolved.
Show resolved Hide resolved
if (declaration.init.type !== 'Literal' || typeof declaration.init.value === 'object') {
benmccann marked this conversation as resolved.
Show resolved Hide resolved
immutable = false;
}
}
}

this.add_var(node, {
name,
initialised: instance_scope.initialised_declarations.has(name),
imported: node.type.startsWith('Import'),
writable,
imported
immutable
benmccann marked this conversation as resolved.
Show resolved Hide resolved
});
this.node_for_declaration.set(name, node);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/compiler/compile/internal_exports.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -54,7 +54,8 @@ export default class Expression {

/** @type {Array<import('estree').Node | import('estree').Node[]>} */
declarations = [];
/** */

/** @type {boolean} */
uses_context = false;

/** @type {import('estree').Node} */
Expand Down Expand Up @@ -128,7 +129,7 @@ export default class Expression {
.forEach((name) => dependencies.add(name));
}
} else {
if (!lazy) {
if (!lazy && !component.var_lookup.get(name)?.immutable) {
dependencies.add(name);
}
component.add_reference(node, name);
Expand Down Expand Up @@ -231,6 +232,8 @@ export default class Expression {
if (this.manipulated) return this.manipulated;
const { component, declarations, scope_map: map, template_scope, owner } = this;
let scope = this.scope;

/** @type {import('estree').FunctionExpression | import('estree').ArrowFunctionExpression | null} */
let function_expression;

/** @type {Set<string>} */
Expand Down
1 change: 1 addition & 0 deletions packages/svelte/src/compiler/interfaces.d.ts
Expand Up @@ -383,6 +383,7 @@ export interface Var {
writable?: boolean;

// used internally, but not exposed
immutable?: boolean;
global?: boolean;
internal?: boolean; // event handlers, bindings
initialised?: boolean;
Expand Down