Skip to content

Commit

Permalink
generator support: eliminated performance sink in 'node-processor', s…
Browse files Browse the repository at this point in the history
…olves #1293

* added content length tracking to avoid repeated string concatenation under way
  • Loading branch information
sailingKieler committed Nov 21, 2023
1 parent 99bfb85 commit b16bc7f
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/langium/src/generator/node-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Context {
private traceData: InternalTraceRegion[] = [];

private lines: string[][] = [[]];
private length: number = 0;

constructor(defaultIndent?: string | number) {
if (typeof defaultIndent === 'string') {
Expand All @@ -35,6 +36,10 @@ class Context {
return this.lines.map(e => e.join('')).join('');
}

get contentLength(): number {
return this.length;
}

get currentLineNumber(): number {
return this.lines.length - 1;
}
Expand All @@ -45,7 +50,7 @@ class Context {

get currentPosition(): OffsetAndPosition {
return {
offset: this.content.length,
offset: this.contentLength,
line: this.currentLineNumber,
character: this.currentLineContent.length
};
Expand All @@ -55,6 +60,7 @@ class Context {
if (value.length > 0) {
const beforePos = isIndent && this.currentPosition;
this.lines[this.currentLineNumber].push(value);
this.length += value.length;
if (beforePos) {
this.indentPendingTraceRegions(beforePos);
}
Expand Down Expand Up @@ -85,6 +91,7 @@ class Context {
}

resetCurrentLine() {
this.length -= this.lines[this.currentLineNumber].join('').length;
this.lines[this.currentLineNumber] = [];
this.pendingIndent = true;
}
Expand Down

0 comments on commit b16bc7f

Please sign in to comment.