parallelize-async-work
ComputerGuy 2 months ago
parent 3e54616f5b
commit 33992ed7e8

@ -88,7 +88,7 @@ export interface ParallelizedChunk {
id: Pattern | null; id: Pattern | null;
init: Expression; init: Expression;
}>; }>;
kind: VariableDeclaration['kind'] | null; kind: 'var' | 'let' | 'const' | null;
/** index in instance body */ /** index in instance body */
position: number; position: number;
bindings: Binding[]; bindings: Binding[];

@ -54,6 +54,7 @@ export function can_be_parallelized(expression, scope, analysis, bindings) {
NewExpression: stop, NewExpression: stop,
StaticBlock: stop, StaticBlock: stop,
Identifier(node, { path }) { Identifier(node, { path }) {
// @ts-expect-error wtf
if (is_reference(node, /** @type {Node} */ (path.at(-1)))) { if (is_reference(node, /** @type {Node} */ (path.at(-1)))) {
references.add(node.name); references.add(node.name);
} }

@ -47,7 +47,9 @@ export function VariableDeclaration(node, context) {
continue; continue;
} }
const kind = node.kind;
if ( if (
kind !== 'using' && kind !== 'await using' &&
init?.type === 'AwaitExpression' && init?.type === 'AwaitExpression' &&
context.state.analysis.instance?.scope === context.state.scope && context.state.analysis.instance?.scope === context.state.scope &&
!is_expression_async(init.argument) !is_expression_async(init.argument)
@ -72,16 +74,16 @@ export function VariableDeclaration(node, context) {
}; };
if ( if (
current_chunk && current_chunk &&
(current_chunk.kind === node.kind || current_chunk.kind === null) (current_chunk.kind === kind || current_chunk.kind === null)
) { ) {
current_chunk.declarators.push(_declarator); current_chunk.declarators.push(_declarator);
current_chunk.bindings.push(...bindings); current_chunk.bindings.push(...bindings);
current_chunk.position = /** @type {Program} */ (parent).body.indexOf(node); current_chunk.position = /** @type {Program} */ (parent).body.indexOf(node);
current_chunk.kind = node.kind; current_chunk.kind = kind;
} else { } else {
/** @type {ParallelizedChunk} */ /** @type {ParallelizedChunk} */
const chunk = { const chunk = {
kind: node.kind, kind,
declarators: [_declarator], declarators: [_declarator],
position, position,
bindings bindings
@ -179,11 +181,13 @@ export function VariableDeclaration(node, context) {
if (rune === '$state' || rune === '$state.raw') { if (rune === '$state' || rune === '$state.raw') {
const state_declarators = []; const state_declarators = [];
const current_chunk = context.state.current_parallelized_chunk; const current_chunk = context.state.current_parallelized_chunk;
const kind = node.kind;
const parallelize = const parallelize =
declarator.id.type === 'Identifier' && declarator.id.type === 'Identifier' &&
context.state.analysis.instance?.scope === context.state.scope && context.state.analysis.instance?.scope === context.state.scope &&
value.type === 'AwaitExpression' && value.type === 'AwaitExpression' &&
!is_expression_async(value.argument) && !is_expression_async(value.argument) &&
kind !== 'using' && kind !== 'await using' &&
can_be_parallelized(value.argument, context.state.scope, context.state.analysis, [ can_be_parallelized(value.argument, context.state.scope, context.state.analysis, [
...(current_chunk?.bindings ?? []), ...(current_chunk?.bindings ?? []),
...bindings ...bindings
@ -294,11 +298,11 @@ export function VariableDeclaration(node, context) {
current_chunk.declarators.push(...declarators); current_chunk.declarators.push(...declarators);
current_chunk.bindings.push(...bindings); current_chunk.bindings.push(...bindings);
current_chunk.position = position; current_chunk.position = position;
current_chunk.kind = node.kind; current_chunk.kind = kind;
} else { } else {
/** @type {ParallelizedChunk} */ /** @type {ParallelizedChunk} */
const chunk = { const chunk = {
kind: node.kind, kind,
declarators, declarators,
position, position,
bindings bindings
@ -322,13 +326,15 @@ export function VariableDeclaration(node, context) {
context.state.analysis.instance && context.state.analysis.instance &&
context.state.scope === context.state.analysis.instance.scope && context.state.scope === context.state.analysis.instance.scope &&
// TODO make it work without this // TODO make it work without this
declarator.id.type === 'Identifier' declarator.id.type === 'Identifier' &&
node.kind !== 'await using' && node.kind !== 'using'
) { ) {
parallelize = can_be_parallelized(value, context.state.scope, context.state.analysis, [ parallelize = can_be_parallelized(value, context.state.scope, context.state.analysis, [
...(current_chunk?.bindings ?? []), ...(current_chunk?.bindings ?? []),
...context.state.scope.get_bindings(declarator) ...context.state.scope.get_bindings(declarator)
]); ]);
} }
const kind = /** @type {ParallelizedChunk['kind']} */ (node.kind);
/** @type {VariableDeclarator[]} */ /** @type {VariableDeclarator[]} */
const derived_declarators = []; const derived_declarators = [];
@ -428,15 +434,15 @@ export function VariableDeclaration(node, context) {
id, id,
init: /** @type {Expression} */ (init) init: /** @type {Expression} */ (init)
})); }));
if (current_chunk && (current_chunk.kind === node.kind || current_chunk.kind === null)) { if (current_chunk && (current_chunk.kind === kind || current_chunk.kind === null)) {
current_chunk.declarators.push(...declarators); current_chunk.declarators.push(...declarators);
current_chunk.bindings.push(...bindings); current_chunk.bindings.push(...bindings);
current_chunk.position = position; current_chunk.position = position;
current_chunk.kind = node.kind; current_chunk.kind = kind;
} else { } else {
/** @type {ParallelizedChunk} */ /** @type {ParallelizedChunk} */
const chunk = { const chunk = {
kind: node.kind, kind,
declarators, declarators,
position, position,
bindings bindings

Loading…
Cancel
Save