Skip to content

Commit 4b010c6

Browse files
committed
modify how context is built up to reduce some hotspots
1 parent 7675fe0 commit 4b010c6

1 file changed

Lines changed: 17 additions & 18 deletions

File tree

src/xjslt.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -399,12 +399,9 @@ export function processNode(
399399
mergeTemplateGenerators(ruleTemplates, nonRuleTemplates),
400400
);
401401
const next = templates.next();
402+
context.nextMatches = templates;
402403
if (!next.done) {
403-
evaluateTemplate(
404-
next.value,
405-
{ ...context, nextMatches: templates },
406-
params,
407-
);
404+
evaluateTemplate(next.value, context, params);
408405
}
409406
}
410407

@@ -419,9 +416,10 @@ export function nextMatch(
419416
if (nextMatches) {
420417
const next = nextMatches.next();
421418
if (!next.done) {
419+
context.nextMatches = nextMatches;
422420
evaluateTemplate(
423421
next.value,
424-
{ ...context, nextMatches: nextMatches },
422+
context,
425423
data.params,
426424
);
427425
}
@@ -444,9 +442,10 @@ export function applyImports(
444442
next = nextMatches.next();
445443
}
446444
if (!next.done) {
445+
context.nextMatches = nextMatches;
447446
evaluateTemplate(
448447
next.value,
449-
{ ...context, nextMatches: nextMatches },
448+
context,
450449
data.params,
451450
);
452451
}
@@ -495,9 +494,12 @@ function iterateNodes<T>(
495494
func: SequenceConstructorWithReturn<T>,
496495
): T[] {
497496
let position = 0;
497+
const tmpContext = { ...context, contextList };
498498
return contextList.map((contextItem) => {
499499
position++;
500-
return func({ ...context, contextItem, contextList, position });
500+
tmpContext.contextItem = contextItem;
501+
tmpContext.position = position;
502+
return func(tmpContext);
501503
});
502504
}
503505

@@ -656,16 +658,14 @@ export function applyTemplates(
656658
data.sortKeyComponents,
657659
namespaceResolver,
658660
);
659-
iterateNodes(sorted, context, (context) => {
661+
iterateNodes(sorted, {...context, mode }, (context) => {
662+
context.variableScopes = extendScope(context.variableScopes);
660663
processNode(
661-
{
662-
...context,
663-
mode: mode,
664-
variableScopes: extendScope(context.variableScopes),
665-
},
664+
context,
666665
data.params,
667666
data.namespaces,
668667
);
668+
context.variableScopes.pop();
669669
});
670670
}
671671

@@ -1249,10 +1249,9 @@ export function forEach(
12491249
namespaceResolver,
12501250
);
12511251
iterateNodes(nodeList, context, (context) => {
1252-
func({
1253-
...context,
1254-
variableScopes: extendScope(context.variableScopes),
1255-
});
1252+
context.variableScopes = extendScope(context.variableScopes);
1253+
func(context);
1254+
context.variableScopes.pop();
12561255
});
12571256
}
12581257
}

0 commit comments

Comments
 (0)