Skip to content

Simplify createChildren#22270

Merged
2 commits merged into
masterfrom
createChildren
Mar 28, 2018
Merged

Simplify createChildren#22270
2 commits merged into
masterfrom
createChildren

Conversation

@ghost
Copy link
Copy Markdown

@ghost ghost commented Mar 1, 2018

The algorithm for getting children doesn't need access to the privates of NodeObject, only the assignment to ._children does, which can easily be factored out.

@ghost ghost requested a review from armanio123 March 1, 2018 20:21
@DanielRosenwasser
Copy link
Copy Markdown
Member

While I'd expect this to be strictly faster, can we get some perf numbers here? Additionally, please get sign-off from @mhegazy and @sheetalkamat as well.

@ghost
Copy link
Copy Markdown
Author

ghost commented Mar 5, 2018

Tested:

const benchmark = require("benchmark");
// If you test both typescripts in the same process, it seems that whichever runs second is slower.
// So must run this script twice, once with require("typescript") and once with below.
const ts = require("../../TypeScript/built/local/typescript.js");
const text = require("fs").readFileSync("jquery-3.3.1.js", "utf-8"); // Get the uncompressed version from http://jquery.com/download/
const file = ts.createSourceFile("a.js", text, ts.ScriptTarget.ESNext, /*setParentNodes*/ true);

const suite = new benchmark.Suite;
suite.add("countChildren", () => countChildren(file));
function countChildren(node) {
    let count = 1;
    for (const child of node.getChildren(file))
        count += countChildren(child);
    return count;
}
suite.on("complete", function() {
    this.forEach(({name, stats}) => console.log(`${name}: ${stats.mean * 1000}ms`));
});
suite.on("error", err => { throw err.target.error; });
suite.run();

On my machine it runs in about 10ms with either version. Don't see any difference. Wouldn't expect a difference since this PR just changed the location of the code and didn't change the actual operations needed to get the children.

@ghost
Copy link
Copy Markdown
Author

ghost commented Mar 27, 2018

@DanielRosenwasser @sheetalkamat @mhegazy Could someone review?

@mhegazy
Copy link
Copy Markdown
Contributor

mhegazy commented Mar 27, 2018

What is the impetus for this change?

@ghost
Copy link
Copy Markdown
Author

ghost commented Mar 27, 2018

This change doesn't really change the algorithm used to calculate a node's children. It just moves that algorithm out to a helper function that's not inside the context of NodeObject -- and it returns a value instead of assigning to a private property. The idea is that the code shouldn't be stateful when it's not necessary.

Comment thread src/services/services.ts Outdated
}
}

function createChildren(node: Node, sourceFile: SourceFileLike = node.getSourceFile()): Node[] {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to do this initialization all the time. the idea was to defer the walk up the tree until you need the source file. e.g. no children, or in jsDoc

@mhegazy
Copy link
Copy Markdown
Contributor

mhegazy commented Mar 28, 2018

This change doesn't really change the algorithm used to calculate a node's children. It just moves that algorithm out to a helper function that's not inside the context of NodeObject -- and it returns a value instead of assigning to a private property. The idea is that the code shouldn't be stateful when it's not necessary.

the issue is that it is not clear that is is different :). for future PRs like this one consider splitting them into multiple commits, where the first is just moving the code around, and then subsequent commits are refactoring it. this way reviewers can see the incremental change tot he logic.

@ghost
Copy link
Copy Markdown
Author

ghost commented Mar 28, 2018

Made a simpler PR in #22938.

@mhegazy
Copy link
Copy Markdown
Contributor

mhegazy commented Mar 28, 2018

this one is fine. just one comment above.

@ghost ghost merged commit 7d39b45 into master Mar 28, 2018
@ghost ghost deleted the createChildren branch March 28, 2018 20:57
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants