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(escapeAllSwigTags): reducing GC overhead #5620

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
30 changes: 23 additions & 7 deletions lib/hexo/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ const isNonWhiteSpaceChar = (char: string) => char !== '\r'
&& char !== '\v'
&& char !== ' ';

class StringBuilder {
private parts: string[];

constructor() {
this.parts = [];
}

append(str: string): void {
this.parts.push(str);
}

toString(): string {
return this.parts.join('');
}
}

class PostRenderEscape {
public stored: string[];
public length: number;
Expand Down Expand Up @@ -74,7 +90,7 @@ class PostRenderEscape {
}
let state = STATE_PLAINTEXT;
let buffer = '';
let output = '';
const output = new StringBuilder();

let swig_tag_name_begin = false;
let swig_tag_name_end = false;
Expand Down Expand Up @@ -104,10 +120,10 @@ class PostRenderEscape {
swig_tag_name_begin = false; // Mark if it is the first non white space char in the swig tag
swig_tag_name_end = false;
} else {
output += char;
output.append(char);
}
} else {
output += char;
output.append(char);
}
} else if (state === STATE_SWIG_TAG) {
if (char === '%' && next_char === '}') { // From swig back to plain text
Expand All @@ -117,7 +133,7 @@ class PostRenderEscape {
} else {
swig_tag_name = '';
state = STATE_PLAINTEXT;
output += PostRenderEscape.escapeContent(this.stored, 'swig', `{%${buffer}%}`);
output.append(PostRenderEscape.escapeContent(this.stored, 'swig', `{%${buffer}%}`));
}

buffer = '';
Expand All @@ -144,7 +160,7 @@ class PostRenderEscape {
if (char === '}' && next_char === '}') {
idx++;
state = STATE_PLAINTEXT;
output += PostRenderEscape.escapeContent(this.stored, 'swig', `{{${buffer}}}`);
output.append(PostRenderEscape.escapeContent(this.stored, 'swig', `{{${buffer}}}`));
buffer = '';
} else {
buffer = buffer + char;
Expand Down Expand Up @@ -174,7 +190,7 @@ class PostRenderEscape {

if (swig_full_tag_end_buffer.includes(`end${swig_tag_name}`)) {
state = STATE_PLAINTEXT;
output += PostRenderEscape.escapeContent(this.stored, 'swig', `{%${swig_full_tag_start_buffer}%}${buffer}{%${swig_full_tag_end_buffer}%}`);
output.append(PostRenderEscape.escapeContent(this.stored, 'swig', `{%${swig_full_tag_start_buffer}%}${buffer}{%${swig_full_tag_end_buffer}%}`));
idx = _idx;
swig_full_tag_start_buffer = '';
swig_full_tag_end_buffer = '';
Expand All @@ -188,7 +204,7 @@ class PostRenderEscape {
}
}

return output;
return output.toString();
}
}

Expand Down
Loading