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: avoid excessive linear scanning for large functions #1399

Open
wants to merge 2 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
50 changes: 28 additions & 22 deletions lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,25 @@ const TreeCopier = function TreeCopier(

const TCp: TreeCopierType = TreeCopier.prototype;

const originalProperty: PropertyDescriptor = {
configurable: false,
enumerable: false,
writable: true,
};

TCp.copy = function (node) {
if (typeof node !== "object") {
return node;
}

if (this.seen.has(node)) {
return this.seen.get(node);
}

if (isArray.check(node)) {
const copy: any = new Array(node.length);
const copy: any = node.slice();
this.seen.set(node, copy);
node.forEach(function (this: any, item: any, i: any) {
node.forEach(function (this: TreeCopierType, item: any, i: any) {
copy[i] = this.copy(item);
}, this);
return copy;
Expand All @@ -174,36 +184,29 @@ TCp.copy = function (node) {

util.fixFaultyLocations(node, this.lines);

const copy: any = Object.create(Object.getPrototypeOf(node), {
original: {
// Provide a link from the copy to the original.
value: node,
configurable: false,
enumerable: false,
writable: true,
},
});
const copy: any = Object.create(Object.getPrototypeOf(node));
// Provide a link from the copy to the original.
Object.defineProperty(copy, "original", originalProperty);
copy.original = node;

this.seen.set(node, copy);

const loc = node.loc;
const oldIndent = this.indent;
let newIndent = oldIndent;

const oldStartTokenIndex = this.startTokenIndex;
const oldEndTokenIndex = this.endTokenIndex;

const type = node.type;
if (loc) {
// When node is a comment, we set node.loc.indent to
// node.loc.start.column so that, when/if we print the comment by
// itself, we can strip that much whitespace from the left margin of
// the comment. This only really matters for multiline Block comments,
// but it doesn't hurt for Line comments.
if (
node.type === "Block" ||
node.type === "Line" ||
node.type === "CommentBlock" ||
node.type === "CommentLine" ||
type === "Block" ||
type === "Line" ||
type === "CommentBlock" ||
type === "CommentLine" ||
this.lines.isPrecededOnlyByWhitespace(loc.start)
) {
newIndent = this.indent = loc.start.column;
Expand All @@ -221,16 +224,19 @@ TCp.copy = function (node) {
this.findTokenRange(loc);
}

const oldStartTokenIndex = this.startTokenIndex;
const oldEndTokenIndex = this.endTokenIndex;

const keys = Object.keys(node);
const keyCount = keys.length;
for (let i = 0; i < keyCount; ++i) {
const key = keys[i];
if (key === "loc") {
copy[key] = node[key];
} else if (key === "tokens" && node.type === "File") {
copy.loc = loc;
} else if (key === "tokens" && type === "File") {
// Preserve file.tokens (uncopied) in case client code cares about
// it, even though Recast ignores it when reprinting.
copy[key] = node[key];
copy.tokens = node.tokens;
} else {
copy[key] = this.copy(node[key]);
}
Expand Down Expand Up @@ -262,7 +268,7 @@ TCp.findTokenRange = function (loc) {
// *before* loc.end, we need to fast-forward this.endTokenIndex first.
while (this.endTokenIndex < loc.tokens.length) {
const token = loc.tokens[this.endTokenIndex];
if (util.comparePos(token.loc.end, loc.end) < 0) {
if (util.comparePos(token.loc.end, loc.end) <= 0) {
++this.endTokenIndex;
} else break;
}
Expand Down