prettier/src/doc/doc-utils.js

219 lines
5.5 KiB
JavaScript
Raw Normal View History

"use strict";
fix(perf): convert traverseDoc from recursion to stack (#4776) (#4848) In addition to a tiny performance improvement outlined below, the CPU profile of traverseDoc is now more readable. Also anonymous arrow functions changed to named regular functions so that they are properly displayed in the CPU profile, and moved to outer scope where there's no closure so that they aren't re-created (this change's performance is dependent on JS engine implementation and optimization details). Before (profile): ``` 7129.9 ms 5.43 % 13349.9 ms 10.18 % traverseDocRec 7067.4 ms 5.39 % 11285.5 ms 8.60 % traverseDocRec 31.5 ms 0.02 % 1031.9 ms 0.79 % traverseDoc 23.6 ms 0.02 % 12313.4 ms 9.39 % traverseDoc 2.6 ms 0.00 % 0.3 ms 0.00 % (anonymous) 1.7 ms 0.00 % 1.7 ms 0.00 % call 1.6 ms 0.00 % 1.6 ms 0.00 % call 0.5 ms 0.00 % 0.5 ms 0.00 % conditionalGroup 0.4 ms 0.00 % 0.4 ms 0.00 % printDocToString$1 0.1 ms 0.00 % 0.1 ms 0.00 % printGenerically 0.1 ms 0.00 % 0.1 ms 0.00 % t 0.1 ms 0.00 % 0.1 ms 0.00 % ifBreak 0.1 ms 0.00 % 0.1 ms 0.00 % (anonymous) 0 ms 0 % 0.1 ms 0.00 % forEach ``` After (profile): ``` 6937.9 ms 5.37 % 12872.5 ms 9.97 % traverseDoc 5944.0 ms 4.60 % 11047.3 ms 8.55 % propagateBreaks 735.7 ms 0.57 % 1358.3 ms 1.05 % findInDoc 257.9 ms 0.20 % 466.7 ms 0.36 % findInDoc 0.1 ms 0.00 % 0.1 ms 0.00 % has 0.1 ms 0.00 % 0.1 ms 0.00 % printArgumentsList ``` Before (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/4b52c027-ef62-49d6-8770-179e805a0f43 For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier-2/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.774598830700336, [debug] "ms": 128.624 [debug] } ``` After (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/aa76e134-a68c-44ed-89a8-efb68bc46baa For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.888114977163907, [debug] "ms": 126.773 [debug] } ```
2018-07-16 06:02:14 +03:00
// Using a unique object to compare by reference.
const traverseDocOnExitStackMarker = {};
Fix edge cases triggered by newlines in arrow functions (#1217) This one is pretty crazy. In #927, I changed ```js concat(["(", join(concat([", "]), printed), ")"]), ``` into ```js concat(["(", join(concat([", "]), printedLastArgExpanded), ")"]), ``` which makes the example in #1203 look ugly. The crazy thing is that `JSON.stringify(printed) === JSON.stringify(printedLastArgExpanded)`. So they are deep equal but not the same object. In a non-mutative world, this should cause any problem, but we actually mutate those to propagate breaks. In the break propagation, we only looked at the first one in case of a conditional group. But, because the two were the same object then it also applied to the second one and happened to be the correct behavior! When I changed that piece of code to be two distinct objects, it no longer worked by accident and caused a bunch of weird issues where breaks didn't propagate correctly. The solution for this case is to propagate the breaks on all the conditions. I'm pretty sure that this is the expected behavior, we want to deeply recurse in all of them, we don't propagate it up the boundary anyway. The other use case for `traverseInDoc()` is `findInDoc()`, right now it searches for the first conditional group but it seems very arbitrary. I changed it to not search on any and none of the tests are failing, so I think it's safe(tm). If it triggers weird behavior, then it'll at least be expected and not randomly explode at us if we move groups around. I tried to go through all the conditions for `findInDoc()` but it triggers a few failures (the output look bad). I'm not really sure why. https://gist.github.com/vjeux/5fb7566cc3d65974817d512d1ef6abe1 Fix #1203
2017-04-13 19:21:18 +03:00
function traverseDoc(doc, onEnter, onExit, shouldTraverseConditionalGroups) {
fix(perf): convert traverseDoc from recursion to stack (#4776) (#4848) In addition to a tiny performance improvement outlined below, the CPU profile of traverseDoc is now more readable. Also anonymous arrow functions changed to named regular functions so that they are properly displayed in the CPU profile, and moved to outer scope where there's no closure so that they aren't re-created (this change's performance is dependent on JS engine implementation and optimization details). Before (profile): ``` 7129.9 ms 5.43 % 13349.9 ms 10.18 % traverseDocRec 7067.4 ms 5.39 % 11285.5 ms 8.60 % traverseDocRec 31.5 ms 0.02 % 1031.9 ms 0.79 % traverseDoc 23.6 ms 0.02 % 12313.4 ms 9.39 % traverseDoc 2.6 ms 0.00 % 0.3 ms 0.00 % (anonymous) 1.7 ms 0.00 % 1.7 ms 0.00 % call 1.6 ms 0.00 % 1.6 ms 0.00 % call 0.5 ms 0.00 % 0.5 ms 0.00 % conditionalGroup 0.4 ms 0.00 % 0.4 ms 0.00 % printDocToString$1 0.1 ms 0.00 % 0.1 ms 0.00 % printGenerically 0.1 ms 0.00 % 0.1 ms 0.00 % t 0.1 ms 0.00 % 0.1 ms 0.00 % ifBreak 0.1 ms 0.00 % 0.1 ms 0.00 % (anonymous) 0 ms 0 % 0.1 ms 0.00 % forEach ``` After (profile): ``` 6937.9 ms 5.37 % 12872.5 ms 9.97 % traverseDoc 5944.0 ms 4.60 % 11047.3 ms 8.55 % propagateBreaks 735.7 ms 0.57 % 1358.3 ms 1.05 % findInDoc 257.9 ms 0.20 % 466.7 ms 0.36 % findInDoc 0.1 ms 0.00 % 0.1 ms 0.00 % has 0.1 ms 0.00 % 0.1 ms 0.00 % printArgumentsList ``` Before (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/4b52c027-ef62-49d6-8770-179e805a0f43 For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier-2/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.774598830700336, [debug] "ms": 128.624 [debug] } ``` After (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/aa76e134-a68c-44ed-89a8-efb68bc46baa For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.888114977163907, [debug] "ms": 126.773 [debug] } ```
2018-07-16 06:02:14 +03:00
const docsStack = [doc];
while (docsStack.length !== 0) {
const doc = docsStack.pop();
if (doc === traverseDocOnExitStackMarker) {
onExit(docsStack.pop());
continue;
}
let shouldRecurse = true;
if (onEnter) {
if (onEnter(doc) === false) {
shouldRecurse = false;
}
}
fix(perf): convert traverseDoc from recursion to stack (#4776) (#4848) In addition to a tiny performance improvement outlined below, the CPU profile of traverseDoc is now more readable. Also anonymous arrow functions changed to named regular functions so that they are properly displayed in the CPU profile, and moved to outer scope where there's no closure so that they aren't re-created (this change's performance is dependent on JS engine implementation and optimization details). Before (profile): ``` 7129.9 ms 5.43 % 13349.9 ms 10.18 % traverseDocRec 7067.4 ms 5.39 % 11285.5 ms 8.60 % traverseDocRec 31.5 ms 0.02 % 1031.9 ms 0.79 % traverseDoc 23.6 ms 0.02 % 12313.4 ms 9.39 % traverseDoc 2.6 ms 0.00 % 0.3 ms 0.00 % (anonymous) 1.7 ms 0.00 % 1.7 ms 0.00 % call 1.6 ms 0.00 % 1.6 ms 0.00 % call 0.5 ms 0.00 % 0.5 ms 0.00 % conditionalGroup 0.4 ms 0.00 % 0.4 ms 0.00 % printDocToString$1 0.1 ms 0.00 % 0.1 ms 0.00 % printGenerically 0.1 ms 0.00 % 0.1 ms 0.00 % t 0.1 ms 0.00 % 0.1 ms 0.00 % ifBreak 0.1 ms 0.00 % 0.1 ms 0.00 % (anonymous) 0 ms 0 % 0.1 ms 0.00 % forEach ``` After (profile): ``` 6937.9 ms 5.37 % 12872.5 ms 9.97 % traverseDoc 5944.0 ms 4.60 % 11047.3 ms 8.55 % propagateBreaks 735.7 ms 0.57 % 1358.3 ms 1.05 % findInDoc 257.9 ms 0.20 % 466.7 ms 0.36 % findInDoc 0.1 ms 0.00 % 0.1 ms 0.00 % has 0.1 ms 0.00 % 0.1 ms 0.00 % printArgumentsList ``` Before (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/4b52c027-ef62-49d6-8770-179e805a0f43 For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier-2/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.774598830700336, [debug] "ms": 128.624 [debug] } ``` After (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/aa76e134-a68c-44ed-89a8-efb68bc46baa For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.888114977163907, [debug] "ms": 126.773 [debug] } ```
2018-07-16 06:02:14 +03:00
if (onExit) {
docsStack.push(doc);
docsStack.push(traverseDocOnExitStackMarker);
}
if (shouldRecurse) {
fix(perf): convert traverseDoc from recursion to stack (#4776) (#4848) In addition to a tiny performance improvement outlined below, the CPU profile of traverseDoc is now more readable. Also anonymous arrow functions changed to named regular functions so that they are properly displayed in the CPU profile, and moved to outer scope where there's no closure so that they aren't re-created (this change's performance is dependent on JS engine implementation and optimization details). Before (profile): ``` 7129.9 ms 5.43 % 13349.9 ms 10.18 % traverseDocRec 7067.4 ms 5.39 % 11285.5 ms 8.60 % traverseDocRec 31.5 ms 0.02 % 1031.9 ms 0.79 % traverseDoc 23.6 ms 0.02 % 12313.4 ms 9.39 % traverseDoc 2.6 ms 0.00 % 0.3 ms 0.00 % (anonymous) 1.7 ms 0.00 % 1.7 ms 0.00 % call 1.6 ms 0.00 % 1.6 ms 0.00 % call 0.5 ms 0.00 % 0.5 ms 0.00 % conditionalGroup 0.4 ms 0.00 % 0.4 ms 0.00 % printDocToString$1 0.1 ms 0.00 % 0.1 ms 0.00 % printGenerically 0.1 ms 0.00 % 0.1 ms 0.00 % t 0.1 ms 0.00 % 0.1 ms 0.00 % ifBreak 0.1 ms 0.00 % 0.1 ms 0.00 % (anonymous) 0 ms 0 % 0.1 ms 0.00 % forEach ``` After (profile): ``` 6937.9 ms 5.37 % 12872.5 ms 9.97 % traverseDoc 5944.0 ms 4.60 % 11047.3 ms 8.55 % propagateBreaks 735.7 ms 0.57 % 1358.3 ms 1.05 % findInDoc 257.9 ms 0.20 % 466.7 ms 0.36 % findInDoc 0.1 ms 0.00 % 0.1 ms 0.00 % has 0.1 ms 0.00 % 0.1 ms 0.00 % printArgumentsList ``` Before (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/4b52c027-ef62-49d6-8770-179e805a0f43 For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier-2/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.774598830700336, [debug] "ms": 128.624 [debug] } ``` After (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/aa76e134-a68c-44ed-89a8-efb68bc46baa For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.888114977163907, [debug] "ms": 126.773 [debug] } ```
2018-07-16 06:02:14 +03:00
// When there are multiple parts to process,
// the parts need to be pushed onto the stack in reverse order,
// so that they are processed in the original order
// when the stack is popped.
if (doc.type === "concat" || doc.type === "fill") {
fix(perf): convert traverseDoc from recursion to stack (#4776) (#4848) In addition to a tiny performance improvement outlined below, the CPU profile of traverseDoc is now more readable. Also anonymous arrow functions changed to named regular functions so that they are properly displayed in the CPU profile, and moved to outer scope where there's no closure so that they aren't re-created (this change's performance is dependent on JS engine implementation and optimization details). Before (profile): ``` 7129.9 ms 5.43 % 13349.9 ms 10.18 % traverseDocRec 7067.4 ms 5.39 % 11285.5 ms 8.60 % traverseDocRec 31.5 ms 0.02 % 1031.9 ms 0.79 % traverseDoc 23.6 ms 0.02 % 12313.4 ms 9.39 % traverseDoc 2.6 ms 0.00 % 0.3 ms 0.00 % (anonymous) 1.7 ms 0.00 % 1.7 ms 0.00 % call 1.6 ms 0.00 % 1.6 ms 0.00 % call 0.5 ms 0.00 % 0.5 ms 0.00 % conditionalGroup 0.4 ms 0.00 % 0.4 ms 0.00 % printDocToString$1 0.1 ms 0.00 % 0.1 ms 0.00 % printGenerically 0.1 ms 0.00 % 0.1 ms 0.00 % t 0.1 ms 0.00 % 0.1 ms 0.00 % ifBreak 0.1 ms 0.00 % 0.1 ms 0.00 % (anonymous) 0 ms 0 % 0.1 ms 0.00 % forEach ``` After (profile): ``` 6937.9 ms 5.37 % 12872.5 ms 9.97 % traverseDoc 5944.0 ms 4.60 % 11047.3 ms 8.55 % propagateBreaks 735.7 ms 0.57 % 1358.3 ms 1.05 % findInDoc 257.9 ms 0.20 % 466.7 ms 0.36 % findInDoc 0.1 ms 0.00 % 0.1 ms 0.00 % has 0.1 ms 0.00 % 0.1 ms 0.00 % printArgumentsList ``` Before (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/4b52c027-ef62-49d6-8770-179e805a0f43 For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier-2/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.774598830700336, [debug] "ms": 128.624 [debug] } ``` After (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/aa76e134-a68c-44ed-89a8-efb68bc46baa For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.888114977163907, [debug] "ms": 126.773 [debug] } ```
2018-07-16 06:02:14 +03:00
for (let ic = doc.parts.length, i = ic - 1; i >= 0; --i) {
docsStack.push(doc.parts[i]);
}
} else if (doc.type === "if-break") {
if (doc.flatContents) {
fix(perf): convert traverseDoc from recursion to stack (#4776) (#4848) In addition to a tiny performance improvement outlined below, the CPU profile of traverseDoc is now more readable. Also anonymous arrow functions changed to named regular functions so that they are properly displayed in the CPU profile, and moved to outer scope where there's no closure so that they aren't re-created (this change's performance is dependent on JS engine implementation and optimization details). Before (profile): ``` 7129.9 ms 5.43 % 13349.9 ms 10.18 % traverseDocRec 7067.4 ms 5.39 % 11285.5 ms 8.60 % traverseDocRec 31.5 ms 0.02 % 1031.9 ms 0.79 % traverseDoc 23.6 ms 0.02 % 12313.4 ms 9.39 % traverseDoc 2.6 ms 0.00 % 0.3 ms 0.00 % (anonymous) 1.7 ms 0.00 % 1.7 ms 0.00 % call 1.6 ms 0.00 % 1.6 ms 0.00 % call 0.5 ms 0.00 % 0.5 ms 0.00 % conditionalGroup 0.4 ms 0.00 % 0.4 ms 0.00 % printDocToString$1 0.1 ms 0.00 % 0.1 ms 0.00 % printGenerically 0.1 ms 0.00 % 0.1 ms 0.00 % t 0.1 ms 0.00 % 0.1 ms 0.00 % ifBreak 0.1 ms 0.00 % 0.1 ms 0.00 % (anonymous) 0 ms 0 % 0.1 ms 0.00 % forEach ``` After (profile): ``` 6937.9 ms 5.37 % 12872.5 ms 9.97 % traverseDoc 5944.0 ms 4.60 % 11047.3 ms 8.55 % propagateBreaks 735.7 ms 0.57 % 1358.3 ms 1.05 % findInDoc 257.9 ms 0.20 % 466.7 ms 0.36 % findInDoc 0.1 ms 0.00 % 0.1 ms 0.00 % has 0.1 ms 0.00 % 0.1 ms 0.00 % printArgumentsList ``` Before (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/4b52c027-ef62-49d6-8770-179e805a0f43 For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier-2/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.774598830700336, [debug] "ms": 128.624 [debug] } ``` After (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/aa76e134-a68c-44ed-89a8-efb68bc46baa For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.888114977163907, [debug] "ms": 126.773 [debug] } ```
2018-07-16 06:02:14 +03:00
docsStack.push(doc.flatContents);
}
if (doc.breakContents) {
docsStack.push(doc.breakContents);
}
} else if (doc.type === "group" && doc.expandedStates) {
if (shouldTraverseConditionalGroups) {
fix(perf): convert traverseDoc from recursion to stack (#4776) (#4848) In addition to a tiny performance improvement outlined below, the CPU profile of traverseDoc is now more readable. Also anonymous arrow functions changed to named regular functions so that they are properly displayed in the CPU profile, and moved to outer scope where there's no closure so that they aren't re-created (this change's performance is dependent on JS engine implementation and optimization details). Before (profile): ``` 7129.9 ms 5.43 % 13349.9 ms 10.18 % traverseDocRec 7067.4 ms 5.39 % 11285.5 ms 8.60 % traverseDocRec 31.5 ms 0.02 % 1031.9 ms 0.79 % traverseDoc 23.6 ms 0.02 % 12313.4 ms 9.39 % traverseDoc 2.6 ms 0.00 % 0.3 ms 0.00 % (anonymous) 1.7 ms 0.00 % 1.7 ms 0.00 % call 1.6 ms 0.00 % 1.6 ms 0.00 % call 0.5 ms 0.00 % 0.5 ms 0.00 % conditionalGroup 0.4 ms 0.00 % 0.4 ms 0.00 % printDocToString$1 0.1 ms 0.00 % 0.1 ms 0.00 % printGenerically 0.1 ms 0.00 % 0.1 ms 0.00 % t 0.1 ms 0.00 % 0.1 ms 0.00 % ifBreak 0.1 ms 0.00 % 0.1 ms 0.00 % (anonymous) 0 ms 0 % 0.1 ms 0.00 % forEach ``` After (profile): ``` 6937.9 ms 5.37 % 12872.5 ms 9.97 % traverseDoc 5944.0 ms 4.60 % 11047.3 ms 8.55 % propagateBreaks 735.7 ms 0.57 % 1358.3 ms 1.05 % findInDoc 257.9 ms 0.20 % 466.7 ms 0.36 % findInDoc 0.1 ms 0.00 % 0.1 ms 0.00 % has 0.1 ms 0.00 % 0.1 ms 0.00 % printArgumentsList ``` Before (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/4b52c027-ef62-49d6-8770-179e805a0f43 For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier-2/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.774598830700336, [debug] "ms": 128.624 [debug] } ``` After (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/aa76e134-a68c-44ed-89a8-efb68bc46baa For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.888114977163907, [debug] "ms": 126.773 [debug] } ```
2018-07-16 06:02:14 +03:00
for (let ic = doc.expandedStates.length, i = ic - 1; i >= 0; --i) {
docsStack.push(doc.expandedStates[i]);
}
} else {
fix(perf): convert traverseDoc from recursion to stack (#4776) (#4848) In addition to a tiny performance improvement outlined below, the CPU profile of traverseDoc is now more readable. Also anonymous arrow functions changed to named regular functions so that they are properly displayed in the CPU profile, and moved to outer scope where there's no closure so that they aren't re-created (this change's performance is dependent on JS engine implementation and optimization details). Before (profile): ``` 7129.9 ms 5.43 % 13349.9 ms 10.18 % traverseDocRec 7067.4 ms 5.39 % 11285.5 ms 8.60 % traverseDocRec 31.5 ms 0.02 % 1031.9 ms 0.79 % traverseDoc 23.6 ms 0.02 % 12313.4 ms 9.39 % traverseDoc 2.6 ms 0.00 % 0.3 ms 0.00 % (anonymous) 1.7 ms 0.00 % 1.7 ms 0.00 % call 1.6 ms 0.00 % 1.6 ms 0.00 % call 0.5 ms 0.00 % 0.5 ms 0.00 % conditionalGroup 0.4 ms 0.00 % 0.4 ms 0.00 % printDocToString$1 0.1 ms 0.00 % 0.1 ms 0.00 % printGenerically 0.1 ms 0.00 % 0.1 ms 0.00 % t 0.1 ms 0.00 % 0.1 ms 0.00 % ifBreak 0.1 ms 0.00 % 0.1 ms 0.00 % (anonymous) 0 ms 0 % 0.1 ms 0.00 % forEach ``` After (profile): ``` 6937.9 ms 5.37 % 12872.5 ms 9.97 % traverseDoc 5944.0 ms 4.60 % 11047.3 ms 8.55 % propagateBreaks 735.7 ms 0.57 % 1358.3 ms 1.05 % findInDoc 257.9 ms 0.20 % 466.7 ms 0.36 % findInDoc 0.1 ms 0.00 % 0.1 ms 0.00 % has 0.1 ms 0.00 % 0.1 ms 0.00 % printArgumentsList ``` Before (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/4b52c027-ef62-49d6-8770-179e805a0f43 For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier-2/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.774598830700336, [debug] "ms": 128.624 [debug] } ``` After (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/aa76e134-a68c-44ed-89a8-efb68bc46baa For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.888114977163907, [debug] "ms": 126.773 [debug] } ```
2018-07-16 06:02:14 +03:00
docsStack.push(doc.contents);
}
} else if (doc.contents) {
fix(perf): convert traverseDoc from recursion to stack (#4776) (#4848) In addition to a tiny performance improvement outlined below, the CPU profile of traverseDoc is now more readable. Also anonymous arrow functions changed to named regular functions so that they are properly displayed in the CPU profile, and moved to outer scope where there's no closure so that they aren't re-created (this change's performance is dependent on JS engine implementation and optimization details). Before (profile): ``` 7129.9 ms 5.43 % 13349.9 ms 10.18 % traverseDocRec 7067.4 ms 5.39 % 11285.5 ms 8.60 % traverseDocRec 31.5 ms 0.02 % 1031.9 ms 0.79 % traverseDoc 23.6 ms 0.02 % 12313.4 ms 9.39 % traverseDoc 2.6 ms 0.00 % 0.3 ms 0.00 % (anonymous) 1.7 ms 0.00 % 1.7 ms 0.00 % call 1.6 ms 0.00 % 1.6 ms 0.00 % call 0.5 ms 0.00 % 0.5 ms 0.00 % conditionalGroup 0.4 ms 0.00 % 0.4 ms 0.00 % printDocToString$1 0.1 ms 0.00 % 0.1 ms 0.00 % printGenerically 0.1 ms 0.00 % 0.1 ms 0.00 % t 0.1 ms 0.00 % 0.1 ms 0.00 % ifBreak 0.1 ms 0.00 % 0.1 ms 0.00 % (anonymous) 0 ms 0 % 0.1 ms 0.00 % forEach ``` After (profile): ``` 6937.9 ms 5.37 % 12872.5 ms 9.97 % traverseDoc 5944.0 ms 4.60 % 11047.3 ms 8.55 % propagateBreaks 735.7 ms 0.57 % 1358.3 ms 1.05 % findInDoc 257.9 ms 0.20 % 466.7 ms 0.36 % findInDoc 0.1 ms 0.00 % 0.1 ms 0.00 % has 0.1 ms 0.00 % 0.1 ms 0.00 % printArgumentsList ``` Before (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/4b52c027-ef62-49d6-8770-179e805a0f43 For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier-2/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.774598830700336, [debug] "ms": 128.624 [debug] } ``` After (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/aa76e134-a68c-44ed-89a8-efb68bc46baa For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.888114977163907, [debug] "ms": 126.773 [debug] } ```
2018-07-16 06:02:14 +03:00
docsStack.push(doc.contents);
Fix edge cases triggered by newlines in arrow functions (#1217) This one is pretty crazy. In #927, I changed ```js concat(["(", join(concat([", "]), printed), ")"]), ``` into ```js concat(["(", join(concat([", "]), printedLastArgExpanded), ")"]), ``` which makes the example in #1203 look ugly. The crazy thing is that `JSON.stringify(printed) === JSON.stringify(printedLastArgExpanded)`. So they are deep equal but not the same object. In a non-mutative world, this should cause any problem, but we actually mutate those to propagate breaks. In the break propagation, we only looked at the first one in case of a conditional group. But, because the two were the same object then it also applied to the second one and happened to be the correct behavior! When I changed that piece of code to be two distinct objects, it no longer worked by accident and caused a bunch of weird issues where breaks didn't propagate correctly. The solution for this case is to propagate the breaks on all the conditions. I'm pretty sure that this is the expected behavior, we want to deeply recurse in all of them, we don't propagate it up the boundary anyway. The other use case for `traverseInDoc()` is `findInDoc()`, right now it searches for the first conditional group but it seems very arbitrary. I changed it to not search on any and none of the tests are failing, so I think it's safe(tm). If it triggers weird behavior, then it'll at least be expected and not randomly explode at us if we move groups around. I tried to go through all the conditions for `findInDoc()` but it triggers a few failures (the output look bad). I'm not really sure why. https://gist.github.com/vjeux/5fb7566cc3d65974817d512d1ef6abe1 Fix #1203
2017-04-13 19:21:18 +03:00
}
}
}
}
function mapDoc(doc, cb) {
if (doc.type === "concat" || doc.type === "fill") {
const parts = doc.parts.map(part => mapDoc(part, cb));
return cb(Object.assign({}, doc, { parts }));
2017-02-23 20:00:29 +03:00
} else if (doc.type === "if-break") {
const breakContents = doc.breakContents && mapDoc(doc.breakContents, cb);
const flatContents = doc.flatContents && mapDoc(doc.flatContents, cb);
return cb(Object.assign({}, doc, { breakContents, flatContents }));
2017-02-23 20:57:51 +03:00
} else if (doc.contents) {
const contents = mapDoc(doc.contents, cb);
return cb(Object.assign({}, doc, { contents }));
2017-02-23 20:00:29 +03:00
}
return cb(doc);
2017-02-23 20:00:29 +03:00
}
function findInDoc(doc, fn, defaultValue) {
let result = defaultValue;
let hasStopped = false;
fix(perf): convert traverseDoc from recursion to stack (#4776) (#4848) In addition to a tiny performance improvement outlined below, the CPU profile of traverseDoc is now more readable. Also anonymous arrow functions changed to named regular functions so that they are properly displayed in the CPU profile, and moved to outer scope where there's no closure so that they aren't re-created (this change's performance is dependent on JS engine implementation and optimization details). Before (profile): ``` 7129.9 ms 5.43 % 13349.9 ms 10.18 % traverseDocRec 7067.4 ms 5.39 % 11285.5 ms 8.60 % traverseDocRec 31.5 ms 0.02 % 1031.9 ms 0.79 % traverseDoc 23.6 ms 0.02 % 12313.4 ms 9.39 % traverseDoc 2.6 ms 0.00 % 0.3 ms 0.00 % (anonymous) 1.7 ms 0.00 % 1.7 ms 0.00 % call 1.6 ms 0.00 % 1.6 ms 0.00 % call 0.5 ms 0.00 % 0.5 ms 0.00 % conditionalGroup 0.4 ms 0.00 % 0.4 ms 0.00 % printDocToString$1 0.1 ms 0.00 % 0.1 ms 0.00 % printGenerically 0.1 ms 0.00 % 0.1 ms 0.00 % t 0.1 ms 0.00 % 0.1 ms 0.00 % ifBreak 0.1 ms 0.00 % 0.1 ms 0.00 % (anonymous) 0 ms 0 % 0.1 ms 0.00 % forEach ``` After (profile): ``` 6937.9 ms 5.37 % 12872.5 ms 9.97 % traverseDoc 5944.0 ms 4.60 % 11047.3 ms 8.55 % propagateBreaks 735.7 ms 0.57 % 1358.3 ms 1.05 % findInDoc 257.9 ms 0.20 % 466.7 ms 0.36 % findInDoc 0.1 ms 0.00 % 0.1 ms 0.00 % has 0.1 ms 0.00 % 0.1 ms 0.00 % printArgumentsList ``` Before (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/4b52c027-ef62-49d6-8770-179e805a0f43 For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier-2/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.774598830700336, [debug] "ms": 128.624 [debug] } ``` After (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/aa76e134-a68c-44ed-89a8-efb68bc46baa For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.888114977163907, [debug] "ms": 126.773 [debug] } ```
2018-07-16 06:02:14 +03:00
function findInDocOnEnterFn(doc) {
const maybeResult = fn(doc);
if (maybeResult !== undefined) {
hasStopped = true;
result = maybeResult;
}
if (hasStopped) {
return false;
}
fix(perf): convert traverseDoc from recursion to stack (#4776) (#4848) In addition to a tiny performance improvement outlined below, the CPU profile of traverseDoc is now more readable. Also anonymous arrow functions changed to named regular functions so that they are properly displayed in the CPU profile, and moved to outer scope where there's no closure so that they aren't re-created (this change's performance is dependent on JS engine implementation and optimization details). Before (profile): ``` 7129.9 ms 5.43 % 13349.9 ms 10.18 % traverseDocRec 7067.4 ms 5.39 % 11285.5 ms 8.60 % traverseDocRec 31.5 ms 0.02 % 1031.9 ms 0.79 % traverseDoc 23.6 ms 0.02 % 12313.4 ms 9.39 % traverseDoc 2.6 ms 0.00 % 0.3 ms 0.00 % (anonymous) 1.7 ms 0.00 % 1.7 ms 0.00 % call 1.6 ms 0.00 % 1.6 ms 0.00 % call 0.5 ms 0.00 % 0.5 ms 0.00 % conditionalGroup 0.4 ms 0.00 % 0.4 ms 0.00 % printDocToString$1 0.1 ms 0.00 % 0.1 ms 0.00 % printGenerically 0.1 ms 0.00 % 0.1 ms 0.00 % t 0.1 ms 0.00 % 0.1 ms 0.00 % ifBreak 0.1 ms 0.00 % 0.1 ms 0.00 % (anonymous) 0 ms 0 % 0.1 ms 0.00 % forEach ``` After (profile): ``` 6937.9 ms 5.37 % 12872.5 ms 9.97 % traverseDoc 5944.0 ms 4.60 % 11047.3 ms 8.55 % propagateBreaks 735.7 ms 0.57 % 1358.3 ms 1.05 % findInDoc 257.9 ms 0.20 % 466.7 ms 0.36 % findInDoc 0.1 ms 0.00 % 0.1 ms 0.00 % has 0.1 ms 0.00 % 0.1 ms 0.00 % printArgumentsList ``` Before (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/4b52c027-ef62-49d6-8770-179e805a0f43 For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier-2/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.774598830700336, [debug] "ms": 128.624 [debug] } ``` After (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/aa76e134-a68c-44ed-89a8-efb68bc46baa For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.888114977163907, [debug] "ms": 126.773 [debug] } ```
2018-07-16 06:02:14 +03:00
}
traverseDoc(doc, findInDocOnEnterFn);
return result;
}
function isEmpty(n) {
return typeof n === "string" && n.length === 0;
}
fix(perf): convert traverseDoc from recursion to stack (#4776) (#4848) In addition to a tiny performance improvement outlined below, the CPU profile of traverseDoc is now more readable. Also anonymous arrow functions changed to named regular functions so that they are properly displayed in the CPU profile, and moved to outer scope where there's no closure so that they aren't re-created (this change's performance is dependent on JS engine implementation and optimization details). Before (profile): ``` 7129.9 ms 5.43 % 13349.9 ms 10.18 % traverseDocRec 7067.4 ms 5.39 % 11285.5 ms 8.60 % traverseDocRec 31.5 ms 0.02 % 1031.9 ms 0.79 % traverseDoc 23.6 ms 0.02 % 12313.4 ms 9.39 % traverseDoc 2.6 ms 0.00 % 0.3 ms 0.00 % (anonymous) 1.7 ms 0.00 % 1.7 ms 0.00 % call 1.6 ms 0.00 % 1.6 ms 0.00 % call 0.5 ms 0.00 % 0.5 ms 0.00 % conditionalGroup 0.4 ms 0.00 % 0.4 ms 0.00 % printDocToString$1 0.1 ms 0.00 % 0.1 ms 0.00 % printGenerically 0.1 ms 0.00 % 0.1 ms 0.00 % t 0.1 ms 0.00 % 0.1 ms 0.00 % ifBreak 0.1 ms 0.00 % 0.1 ms 0.00 % (anonymous) 0 ms 0 % 0.1 ms 0.00 % forEach ``` After (profile): ``` 6937.9 ms 5.37 % 12872.5 ms 9.97 % traverseDoc 5944.0 ms 4.60 % 11047.3 ms 8.55 % propagateBreaks 735.7 ms 0.57 % 1358.3 ms 1.05 % findInDoc 257.9 ms 0.20 % 466.7 ms 0.36 % findInDoc 0.1 ms 0.00 % 0.1 ms 0.00 % has 0.1 ms 0.00 % 0.1 ms 0.00 % printArgumentsList ``` Before (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/4b52c027-ef62-49d6-8770-179e805a0f43 For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier-2/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.774598830700336, [debug] "ms": 128.624 [debug] } ``` After (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/aa76e134-a68c-44ed-89a8-efb68bc46baa For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.888114977163907, [debug] "ms": 126.773 [debug] } ```
2018-07-16 06:02:14 +03:00
function isLineNextFn(doc) {
if (typeof doc === "string") {
return false;
}
if (doc.type === "line") {
return true;
}
}
function isLineNext(doc) {
fix(perf): convert traverseDoc from recursion to stack (#4776) (#4848) In addition to a tiny performance improvement outlined below, the CPU profile of traverseDoc is now more readable. Also anonymous arrow functions changed to named regular functions so that they are properly displayed in the CPU profile, and moved to outer scope where there's no closure so that they aren't re-created (this change's performance is dependent on JS engine implementation and optimization details). Before (profile): ``` 7129.9 ms 5.43 % 13349.9 ms 10.18 % traverseDocRec 7067.4 ms 5.39 % 11285.5 ms 8.60 % traverseDocRec 31.5 ms 0.02 % 1031.9 ms 0.79 % traverseDoc 23.6 ms 0.02 % 12313.4 ms 9.39 % traverseDoc 2.6 ms 0.00 % 0.3 ms 0.00 % (anonymous) 1.7 ms 0.00 % 1.7 ms 0.00 % call 1.6 ms 0.00 % 1.6 ms 0.00 % call 0.5 ms 0.00 % 0.5 ms 0.00 % conditionalGroup 0.4 ms 0.00 % 0.4 ms 0.00 % printDocToString$1 0.1 ms 0.00 % 0.1 ms 0.00 % printGenerically 0.1 ms 0.00 % 0.1 ms 0.00 % t 0.1 ms 0.00 % 0.1 ms 0.00 % ifBreak 0.1 ms 0.00 % 0.1 ms 0.00 % (anonymous) 0 ms 0 % 0.1 ms 0.00 % forEach ``` After (profile): ``` 6937.9 ms 5.37 % 12872.5 ms 9.97 % traverseDoc 5944.0 ms 4.60 % 11047.3 ms 8.55 % propagateBreaks 735.7 ms 0.57 % 1358.3 ms 1.05 % findInDoc 257.9 ms 0.20 % 466.7 ms 0.36 % findInDoc 0.1 ms 0.00 % 0.1 ms 0.00 % has 0.1 ms 0.00 % 0.1 ms 0.00 % printArgumentsList ``` Before (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/4b52c027-ef62-49d6-8770-179e805a0f43 For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier-2/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.774598830700336, [debug] "ms": 128.624 [debug] } ``` After (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/aa76e134-a68c-44ed-89a8-efb68bc46baa For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.888114977163907, [debug] "ms": 126.773 [debug] } ```
2018-07-16 06:02:14 +03:00
return findInDoc(doc, isLineNextFn, false);
}
function willBreakFn(doc) {
if (doc.type === "group" && doc.break) {
return true;
}
if (doc.type === "line" && doc.hard) {
return true;
}
if (doc.type === "break-parent") {
return true;
}
}
function willBreak(doc) {
fix(perf): convert traverseDoc from recursion to stack (#4776) (#4848) In addition to a tiny performance improvement outlined below, the CPU profile of traverseDoc is now more readable. Also anonymous arrow functions changed to named regular functions so that they are properly displayed in the CPU profile, and moved to outer scope where there's no closure so that they aren't re-created (this change's performance is dependent on JS engine implementation and optimization details). Before (profile): ``` 7129.9 ms 5.43 % 13349.9 ms 10.18 % traverseDocRec 7067.4 ms 5.39 % 11285.5 ms 8.60 % traverseDocRec 31.5 ms 0.02 % 1031.9 ms 0.79 % traverseDoc 23.6 ms 0.02 % 12313.4 ms 9.39 % traverseDoc 2.6 ms 0.00 % 0.3 ms 0.00 % (anonymous) 1.7 ms 0.00 % 1.7 ms 0.00 % call 1.6 ms 0.00 % 1.6 ms 0.00 % call 0.5 ms 0.00 % 0.5 ms 0.00 % conditionalGroup 0.4 ms 0.00 % 0.4 ms 0.00 % printDocToString$1 0.1 ms 0.00 % 0.1 ms 0.00 % printGenerically 0.1 ms 0.00 % 0.1 ms 0.00 % t 0.1 ms 0.00 % 0.1 ms 0.00 % ifBreak 0.1 ms 0.00 % 0.1 ms 0.00 % (anonymous) 0 ms 0 % 0.1 ms 0.00 % forEach ``` After (profile): ``` 6937.9 ms 5.37 % 12872.5 ms 9.97 % traverseDoc 5944.0 ms 4.60 % 11047.3 ms 8.55 % propagateBreaks 735.7 ms 0.57 % 1358.3 ms 1.05 % findInDoc 257.9 ms 0.20 % 466.7 ms 0.36 % findInDoc 0.1 ms 0.00 % 0.1 ms 0.00 % has 0.1 ms 0.00 % 0.1 ms 0.00 % printArgumentsList ``` Before (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/4b52c027-ef62-49d6-8770-179e805a0f43 For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier-2/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.774598830700336, [debug] "ms": 128.624 [debug] } ``` After (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/aa76e134-a68c-44ed-89a8-efb68bc46baa For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.888114977163907, [debug] "ms": 126.773 [debug] } ```
2018-07-16 06:02:14 +03:00
return findInDoc(doc, willBreakFn, false);
}
function breakParentGroup(groupStack) {
2017-01-28 18:50:22 +03:00
if (groupStack.length > 0) {
const parentGroup = groupStack[groupStack.length - 1];
// Breaks are not propagated through conditional groups because
// the user is expected to manually handle what breaks.
2017-01-28 18:50:22 +03:00
if (!parentGroup.expandedStates) {
parentGroup.break = true;
}
}
return null;
}
function propagateBreaks(doc) {
fix(perf): convert traverseDoc from recursion to stack (#4776) (#4848) In addition to a tiny performance improvement outlined below, the CPU profile of traverseDoc is now more readable. Also anonymous arrow functions changed to named regular functions so that they are properly displayed in the CPU profile, and moved to outer scope where there's no closure so that they aren't re-created (this change's performance is dependent on JS engine implementation and optimization details). Before (profile): ``` 7129.9 ms 5.43 % 13349.9 ms 10.18 % traverseDocRec 7067.4 ms 5.39 % 11285.5 ms 8.60 % traverseDocRec 31.5 ms 0.02 % 1031.9 ms 0.79 % traverseDoc 23.6 ms 0.02 % 12313.4 ms 9.39 % traverseDoc 2.6 ms 0.00 % 0.3 ms 0.00 % (anonymous) 1.7 ms 0.00 % 1.7 ms 0.00 % call 1.6 ms 0.00 % 1.6 ms 0.00 % call 0.5 ms 0.00 % 0.5 ms 0.00 % conditionalGroup 0.4 ms 0.00 % 0.4 ms 0.00 % printDocToString$1 0.1 ms 0.00 % 0.1 ms 0.00 % printGenerically 0.1 ms 0.00 % 0.1 ms 0.00 % t 0.1 ms 0.00 % 0.1 ms 0.00 % ifBreak 0.1 ms 0.00 % 0.1 ms 0.00 % (anonymous) 0 ms 0 % 0.1 ms 0.00 % forEach ``` After (profile): ``` 6937.9 ms 5.37 % 12872.5 ms 9.97 % traverseDoc 5944.0 ms 4.60 % 11047.3 ms 8.55 % propagateBreaks 735.7 ms 0.57 % 1358.3 ms 1.05 % findInDoc 257.9 ms 0.20 % 466.7 ms 0.36 % findInDoc 0.1 ms 0.00 % 0.1 ms 0.00 % has 0.1 ms 0.00 % 0.1 ms 0.00 % printArgumentsList ``` Before (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/4b52c027-ef62-49d6-8770-179e805a0f43 For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier-2/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.774598830700336, [debug] "ms": 128.624 [debug] } ``` After (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/aa76e134-a68c-44ed-89a8-efb68bc46baa For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.888114977163907, [debug] "ms": 126.773 [debug] } ```
2018-07-16 06:02:14 +03:00
const alreadyVisitedSet = new Set();
const groupStack = [];
fix(perf): convert traverseDoc from recursion to stack (#4776) (#4848) In addition to a tiny performance improvement outlined below, the CPU profile of traverseDoc is now more readable. Also anonymous arrow functions changed to named regular functions so that they are properly displayed in the CPU profile, and moved to outer scope where there's no closure so that they aren't re-created (this change's performance is dependent on JS engine implementation and optimization details). Before (profile): ``` 7129.9 ms 5.43 % 13349.9 ms 10.18 % traverseDocRec 7067.4 ms 5.39 % 11285.5 ms 8.60 % traverseDocRec 31.5 ms 0.02 % 1031.9 ms 0.79 % traverseDoc 23.6 ms 0.02 % 12313.4 ms 9.39 % traverseDoc 2.6 ms 0.00 % 0.3 ms 0.00 % (anonymous) 1.7 ms 0.00 % 1.7 ms 0.00 % call 1.6 ms 0.00 % 1.6 ms 0.00 % call 0.5 ms 0.00 % 0.5 ms 0.00 % conditionalGroup 0.4 ms 0.00 % 0.4 ms 0.00 % printDocToString$1 0.1 ms 0.00 % 0.1 ms 0.00 % printGenerically 0.1 ms 0.00 % 0.1 ms 0.00 % t 0.1 ms 0.00 % 0.1 ms 0.00 % ifBreak 0.1 ms 0.00 % 0.1 ms 0.00 % (anonymous) 0 ms 0 % 0.1 ms 0.00 % forEach ``` After (profile): ``` 6937.9 ms 5.37 % 12872.5 ms 9.97 % traverseDoc 5944.0 ms 4.60 % 11047.3 ms 8.55 % propagateBreaks 735.7 ms 0.57 % 1358.3 ms 1.05 % findInDoc 257.9 ms 0.20 % 466.7 ms 0.36 % findInDoc 0.1 ms 0.00 % 0.1 ms 0.00 % has 0.1 ms 0.00 % 0.1 ms 0.00 % printArgumentsList ``` Before (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/4b52c027-ef62-49d6-8770-179e805a0f43 For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier-2/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.774598830700336, [debug] "ms": 128.624 [debug] } ``` After (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/aa76e134-a68c-44ed-89a8-efb68bc46baa For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.888114977163907, [debug] "ms": 126.773 [debug] } ```
2018-07-16 06:02:14 +03:00
function propagateBreaksOnEnterFn(doc) {
if (doc.type === "break-parent") {
breakParentGroup(groupStack);
}
if (doc.type === "group") {
groupStack.push(doc);
if (alreadyVisitedSet.has(doc)) {
return false;
}
fix(perf): convert traverseDoc from recursion to stack (#4776) (#4848) In addition to a tiny performance improvement outlined below, the CPU profile of traverseDoc is now more readable. Also anonymous arrow functions changed to named regular functions so that they are properly displayed in the CPU profile, and moved to outer scope where there's no closure so that they aren't re-created (this change's performance is dependent on JS engine implementation and optimization details). Before (profile): ``` 7129.9 ms 5.43 % 13349.9 ms 10.18 % traverseDocRec 7067.4 ms 5.39 % 11285.5 ms 8.60 % traverseDocRec 31.5 ms 0.02 % 1031.9 ms 0.79 % traverseDoc 23.6 ms 0.02 % 12313.4 ms 9.39 % traverseDoc 2.6 ms 0.00 % 0.3 ms 0.00 % (anonymous) 1.7 ms 0.00 % 1.7 ms 0.00 % call 1.6 ms 0.00 % 1.6 ms 0.00 % call 0.5 ms 0.00 % 0.5 ms 0.00 % conditionalGroup 0.4 ms 0.00 % 0.4 ms 0.00 % printDocToString$1 0.1 ms 0.00 % 0.1 ms 0.00 % printGenerically 0.1 ms 0.00 % 0.1 ms 0.00 % t 0.1 ms 0.00 % 0.1 ms 0.00 % ifBreak 0.1 ms 0.00 % 0.1 ms 0.00 % (anonymous) 0 ms 0 % 0.1 ms 0.00 % forEach ``` After (profile): ``` 6937.9 ms 5.37 % 12872.5 ms 9.97 % traverseDoc 5944.0 ms 4.60 % 11047.3 ms 8.55 % propagateBreaks 735.7 ms 0.57 % 1358.3 ms 1.05 % findInDoc 257.9 ms 0.20 % 466.7 ms 0.36 % findInDoc 0.1 ms 0.00 % 0.1 ms 0.00 % has 0.1 ms 0.00 % 0.1 ms 0.00 % printArgumentsList ``` Before (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/4b52c027-ef62-49d6-8770-179e805a0f43 For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier-2/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.774598830700336, [debug] "ms": 128.624 [debug] } ``` After (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/aa76e134-a68c-44ed-89a8-efb68bc46baa For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.888114977163907, [debug] "ms": 126.773 [debug] } ```
2018-07-16 06:02:14 +03:00
alreadyVisitedSet.add(doc);
}
}
function propagateBreaksOnExitFn(doc) {
if (doc.type === "group") {
const group = groupStack.pop();
if (group.break) {
breakParentGroup(groupStack);
}
fix(perf): convert traverseDoc from recursion to stack (#4776) (#4848) In addition to a tiny performance improvement outlined below, the CPU profile of traverseDoc is now more readable. Also anonymous arrow functions changed to named regular functions so that they are properly displayed in the CPU profile, and moved to outer scope where there's no closure so that they aren't re-created (this change's performance is dependent on JS engine implementation and optimization details). Before (profile): ``` 7129.9 ms 5.43 % 13349.9 ms 10.18 % traverseDocRec 7067.4 ms 5.39 % 11285.5 ms 8.60 % traverseDocRec 31.5 ms 0.02 % 1031.9 ms 0.79 % traverseDoc 23.6 ms 0.02 % 12313.4 ms 9.39 % traverseDoc 2.6 ms 0.00 % 0.3 ms 0.00 % (anonymous) 1.7 ms 0.00 % 1.7 ms 0.00 % call 1.6 ms 0.00 % 1.6 ms 0.00 % call 0.5 ms 0.00 % 0.5 ms 0.00 % conditionalGroup 0.4 ms 0.00 % 0.4 ms 0.00 % printDocToString$1 0.1 ms 0.00 % 0.1 ms 0.00 % printGenerically 0.1 ms 0.00 % 0.1 ms 0.00 % t 0.1 ms 0.00 % 0.1 ms 0.00 % ifBreak 0.1 ms 0.00 % 0.1 ms 0.00 % (anonymous) 0 ms 0 % 0.1 ms 0.00 % forEach ``` After (profile): ``` 6937.9 ms 5.37 % 12872.5 ms 9.97 % traverseDoc 5944.0 ms 4.60 % 11047.3 ms 8.55 % propagateBreaks 735.7 ms 0.57 % 1358.3 ms 1.05 % findInDoc 257.9 ms 0.20 % 466.7 ms 0.36 % findInDoc 0.1 ms 0.00 % 0.1 ms 0.00 % has 0.1 ms 0.00 % 0.1 ms 0.00 % printArgumentsList ``` Before (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/4b52c027-ef62-49d6-8770-179e805a0f43 For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier-2/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.774598830700336, [debug] "ms": 128.624 [debug] } ``` After (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/aa76e134-a68c-44ed-89a8-efb68bc46baa For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.888114977163907, [debug] "ms": 126.773 [debug] } ```
2018-07-16 06:02:14 +03:00
}
}
traverseDoc(
doc,
propagateBreaksOnEnterFn,
propagateBreaksOnExitFn,
Fix edge cases triggered by newlines in arrow functions (#1217) This one is pretty crazy. In #927, I changed ```js concat(["(", join(concat([", "]), printed), ")"]), ``` into ```js concat(["(", join(concat([", "]), printedLastArgExpanded), ")"]), ``` which makes the example in #1203 look ugly. The crazy thing is that `JSON.stringify(printed) === JSON.stringify(printedLastArgExpanded)`. So they are deep equal but not the same object. In a non-mutative world, this should cause any problem, but we actually mutate those to propagate breaks. In the break propagation, we only looked at the first one in case of a conditional group. But, because the two were the same object then it also applied to the second one and happened to be the correct behavior! When I changed that piece of code to be two distinct objects, it no longer worked by accident and caused a bunch of weird issues where breaks didn't propagate correctly. The solution for this case is to propagate the breaks on all the conditions. I'm pretty sure that this is the expected behavior, we want to deeply recurse in all of them, we don't propagate it up the boundary anyway. The other use case for `traverseInDoc()` is `findInDoc()`, right now it searches for the first conditional group but it seems very arbitrary. I changed it to not search on any and none of the tests are failing, so I think it's safe(tm). If it triggers weird behavior, then it'll at least be expected and not randomly explode at us if we move groups around. I tried to go through all the conditions for `findInDoc()` but it triggers a few failures (the output look bad). I'm not really sure why. https://gist.github.com/vjeux/5fb7566cc3d65974817d512d1ef6abe1 Fix #1203
2017-04-13 19:21:18 +03:00
/* shouldTraverseConditionalGroups */ true
);
}
fix(perf): convert traverseDoc from recursion to stack (#4776) (#4848) In addition to a tiny performance improvement outlined below, the CPU profile of traverseDoc is now more readable. Also anonymous arrow functions changed to named regular functions so that they are properly displayed in the CPU profile, and moved to outer scope where there's no closure so that they aren't re-created (this change's performance is dependent on JS engine implementation and optimization details). Before (profile): ``` 7129.9 ms 5.43 % 13349.9 ms 10.18 % traverseDocRec 7067.4 ms 5.39 % 11285.5 ms 8.60 % traverseDocRec 31.5 ms 0.02 % 1031.9 ms 0.79 % traverseDoc 23.6 ms 0.02 % 12313.4 ms 9.39 % traverseDoc 2.6 ms 0.00 % 0.3 ms 0.00 % (anonymous) 1.7 ms 0.00 % 1.7 ms 0.00 % call 1.6 ms 0.00 % 1.6 ms 0.00 % call 0.5 ms 0.00 % 0.5 ms 0.00 % conditionalGroup 0.4 ms 0.00 % 0.4 ms 0.00 % printDocToString$1 0.1 ms 0.00 % 0.1 ms 0.00 % printGenerically 0.1 ms 0.00 % 0.1 ms 0.00 % t 0.1 ms 0.00 % 0.1 ms 0.00 % ifBreak 0.1 ms 0.00 % 0.1 ms 0.00 % (anonymous) 0 ms 0 % 0.1 ms 0.00 % forEach ``` After (profile): ``` 6937.9 ms 5.37 % 12872.5 ms 9.97 % traverseDoc 5944.0 ms 4.60 % 11047.3 ms 8.55 % propagateBreaks 735.7 ms 0.57 % 1358.3 ms 1.05 % findInDoc 257.9 ms 0.20 % 466.7 ms 0.36 % findInDoc 0.1 ms 0.00 % 0.1 ms 0.00 % has 0.1 ms 0.00 % 0.1 ms 0.00 % printArgumentsList ``` Before (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/4b52c027-ef62-49d6-8770-179e805a0f43 For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier-2/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.774598830700336, [debug] "ms": 128.624 [debug] } ``` After (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/aa76e134-a68c-44ed-89a8-efb68bc46baa For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.888114977163907, [debug] "ms": 126.773 [debug] } ```
2018-07-16 06:02:14 +03:00
function removeLinesFn(doc) {
// Force this doc into flat mode by statically converting all
// lines into spaces (or soft lines into nothing). Hard lines
// should still output because there's too great of a chance
// of breaking existing assumptions otherwise.
fix(perf): convert traverseDoc from recursion to stack (#4776) (#4848) In addition to a tiny performance improvement outlined below, the CPU profile of traverseDoc is now more readable. Also anonymous arrow functions changed to named regular functions so that they are properly displayed in the CPU profile, and moved to outer scope where there's no closure so that they aren't re-created (this change's performance is dependent on JS engine implementation and optimization details). Before (profile): ``` 7129.9 ms 5.43 % 13349.9 ms 10.18 % traverseDocRec 7067.4 ms 5.39 % 11285.5 ms 8.60 % traverseDocRec 31.5 ms 0.02 % 1031.9 ms 0.79 % traverseDoc 23.6 ms 0.02 % 12313.4 ms 9.39 % traverseDoc 2.6 ms 0.00 % 0.3 ms 0.00 % (anonymous) 1.7 ms 0.00 % 1.7 ms 0.00 % call 1.6 ms 0.00 % 1.6 ms 0.00 % call 0.5 ms 0.00 % 0.5 ms 0.00 % conditionalGroup 0.4 ms 0.00 % 0.4 ms 0.00 % printDocToString$1 0.1 ms 0.00 % 0.1 ms 0.00 % printGenerically 0.1 ms 0.00 % 0.1 ms 0.00 % t 0.1 ms 0.00 % 0.1 ms 0.00 % ifBreak 0.1 ms 0.00 % 0.1 ms 0.00 % (anonymous) 0 ms 0 % 0.1 ms 0.00 % forEach ``` After (profile): ``` 6937.9 ms 5.37 % 12872.5 ms 9.97 % traverseDoc 5944.0 ms 4.60 % 11047.3 ms 8.55 % propagateBreaks 735.7 ms 0.57 % 1358.3 ms 1.05 % findInDoc 257.9 ms 0.20 % 466.7 ms 0.36 % findInDoc 0.1 ms 0.00 % 0.1 ms 0.00 % has 0.1 ms 0.00 % 0.1 ms 0.00 % printArgumentsList ``` Before (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/4b52c027-ef62-49d6-8770-179e805a0f43 For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier-2/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.774598830700336, [debug] "ms": 128.624 [debug] } ``` After (performance): ``` cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null Debugger listening on ws://127.0.0.1:9229/aa76e134-a68c-44ed-89a8-efb68bc46baa For help see https://nodejs.org/en/docs/inspector Debugger attached. [debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]} [debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier/LspLanguageService.js' [debug] loaded options `null` [debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"} [debug] '--debug-repeat' option found, running formatWithCursor 1000 times. [debug] '--debug-repeat' measurements for formatWithCursor: { [debug] "repeat": 1000, [debug] "hz": 7.888114977163907, [debug] "ms": 126.773 [debug] } ```
2018-07-16 06:02:14 +03:00
if (doc.type === "line" && !doc.hard) {
return doc.soft ? "" : " ";
} else if (doc.type === "if-break") {
return doc.flatContents || "";
}
return doc;
}
function removeLines(doc) {
return mapDoc(doc, removeLinesFn);
}
function stripTrailingHardline(doc) {
// HACK remove ending hardline, original PR: #1984
if (doc.type === "concat" && doc.parts.length !== 0) {
const lastPart = doc.parts[doc.parts.length - 1];
if (lastPart.type === "concat") {
if (
lastPart.parts.length === 2 &&
lastPart.parts[0].hard &&
lastPart.parts[1].type === "break-parent"
) {
return { type: "concat", parts: doc.parts.slice(0, -1) };
}
return {
type: "concat",
parts: doc.parts.slice(0, -1).concat(stripTrailingHardline(lastPart))
};
}
}
return doc;
}
module.exports = {
isEmpty,
willBreak,
isLineNext,
traverseDoc,
2019-06-04 18:48:06 +03:00
findInDoc,
2017-02-23 20:00:29 +03:00
mapDoc,
propagateBreaks,
removeLines,
stripTrailingHardline
};