Add caching for printing (#2259)
* Add caching for printing For printing the last argument expansion, we need to print the same node a bit differently with a flag. It's not easy to re-print just the node that is different so we end up printing all the sub-tree twice. Since it is often nested, it means that we run into an exponential complexity. I finally found a simple solution: we can use a Map to store the nodes and their printed values. It is really cheap to do so (I can't notice a time difference with or without on normal code) and allows us to stop going through the two sub-trees in the bad case. Fixes #1250 * Update printer.jsmaster
parent
6663bab8a3
commit
8def8e13b0
|
@ -4657,26 +4657,41 @@ function isObjectType(n) {
|
|||
function printAstToDoc(ast, options, addAlignmentSize) {
|
||||
addAlignmentSize = addAlignmentSize || 0;
|
||||
|
||||
const cache = new Map();
|
||||
|
||||
function printGenerically(path, args) {
|
||||
const node = path.getValue();
|
||||
|
||||
const shouldCache = node && typeof node === "object" && args === undefined;
|
||||
if (shouldCache && cache.has(node)) {
|
||||
return cache.get(node);
|
||||
}
|
||||
|
||||
const parent = path.getParentNode(0);
|
||||
// We let JSXElement print its comments itself because it adds () around
|
||||
// UnionTypeAnnotation has to align the child without the comments
|
||||
let res;
|
||||
if (
|
||||
(node && node.type === "JSXElement") ||
|
||||
(parent &&
|
||||
(parent.type === "UnionTypeAnnotation" ||
|
||||
parent.type === "TSUnionType"))
|
||||
) {
|
||||
return genericPrint(path, options, printGenerically, args);
|
||||
res = genericPrint(path, options, printGenerically, args);
|
||||
} else {
|
||||
res = comments.printComments(
|
||||
path,
|
||||
p => genericPrint(p, options, printGenerically, args),
|
||||
options,
|
||||
args && args.needsSemi
|
||||
);
|
||||
}
|
||||
|
||||
return comments.printComments(
|
||||
path,
|
||||
p => genericPrint(p, options, printGenerically, args),
|
||||
options,
|
||||
args && args.needsSemi
|
||||
);
|
||||
if (shouldCache) {
|
||||
cache.set(node, res);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
let doc = printGenerically(new FastPath(ast));
|
||||
|
|
|
@ -10,7 +10,17 @@ someObject.someFunction().then(function() {
|
|||
return someObject.someFunction().then(function() {
|
||||
return someObject.someFunction().then(function() {
|
||||
return someObject.someFunction().then(function() {
|
||||
anotherFunction();
|
||||
return someObject.someFunction().then(function() {
|
||||
return someObject.someFunction().then(function() {
|
||||
return someObject.someFunction().then(function() {
|
||||
return someObject.someFunction().then(function() {
|
||||
return someObject.someFunction().then(function() {
|
||||
anotherFunction();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -30,7 +40,17 @@ someObject.someFunction().then(function() {
|
|||
return someObject.someFunction().then(function() {
|
||||
return someObject.someFunction().then(function() {
|
||||
return someObject.someFunction().then(function() {
|
||||
anotherFunction();
|
||||
return someObject.someFunction().then(function() {
|
||||
return someObject.someFunction().then(function() {
|
||||
return someObject.someFunction().then(function() {
|
||||
return someObject.someFunction().then(function() {
|
||||
return someObject.someFunction().then(function() {
|
||||
anotherFunction();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -7,7 +7,17 @@ someObject.someFunction().then(function() {
|
|||
return someObject.someFunction().then(function() {
|
||||
return someObject.someFunction().then(function() {
|
||||
return someObject.someFunction().then(function() {
|
||||
anotherFunction();
|
||||
return someObject.someFunction().then(function() {
|
||||
return someObject.someFunction().then(function() {
|
||||
return someObject.someFunction().then(function() {
|
||||
return someObject.someFunction().then(function() {
|
||||
return someObject.someFunction().then(function() {
|
||||
anotherFunction();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue