-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
TS v2.3.1 runs out of memory compiling FP library #15443
Comments
Here's another repro which involves other modules. Worth noting that with the following example the code completion feature in VSCode with TypeScript v2.2.2 hangs with CPU 100% import * as array from 'fp-ts/lib/Array'
import * as validation from 'fp-ts/lib/Validation'
export function pouet(
xs: number[],
f: (x: number) => validation.Validation<string[], number>
): void {
const x = array.traverse(validation. .getStaticApplicative(array))(f, xs) // Long hanging due to this space
} In the export function traverse<F extends HKT2S>(applicative: StaticApplicative<F>): <L, A, B>(f: (a: A) => HKT2<L, B>[F], ta: Array<A>) => HKT2<L, Array<B>>[F]
export function traverse<F extends HKTS>(applicative: StaticApplicative<F>): <A, B>(f: (a: A) => HKT<B>[F], ta: Array<A>) => HKT<Array<B>>[F]
export function traverse<F extends HKTS>(applicative: StaticApplicative<F>): <A, B>(f: (a: A) => HKT<B>[F], ta: Array<A>) => HKT<Array<B>>[F] {
return (f: any, ta: any) => {
const snocA2 = liftA2(applicative, curriedSnoc)
return reduce((fab, a) => snocA2(fab, f(a)), applicative.of(empty()), ta)
}
} to export function traverse<F extends HKT2S, L, A, B>(applicative: StaticApplicative<F>, f: (a: A) => HKT2<L, B>[F], ta: Array<A>): HKT2<L, Array<B>>[F]
export function traverse<F extends HKTS, A, B>(applicative: StaticApplicative<F>, f: (a: A) => HKT<B>[F], ta: Array<A>): HKT<Array<B>>[F]
export function traverse<F extends HKTS, A, B>(applicative: StaticApplicative<F>, f: (a: A) => HKT<B>[F], ta: Array<A>): HKT<Array<B>>[F] {
const snocA2 = liftA2(applicative, curriedSnoc)
return reduce((fab, a) => snocA2(fab, f(a)), applicative.of(empty()), ta)
} both the compilation crash and the VSCode hanging go away |
@sandersn One repo showing the last issue @gcanti reported (in case it could help) : |
I can't repro this with |
With TypeScript 2.4.0-dev.20170502
|
I can't repro that exact behaviour in VS Code. For me, initial load time is very long and then if I type a dot after Are you using {
"typescript.tsdk": "/media/nathansa/src2/ts/built/local",
"typescript.tsserver.trace": "off",
// ... other config stuff ...
} Yours will be somewhat different since you want to point to |
I have a project that uses fp-ts and compilation still fails with this memory error on
|
@sandersn 👍 That's the exact behaviour I'm experimenting
No, only the Code Helper is unresponsive (I must close and reopen vscode to regain the functionality)
Yes |
@OliverJAsh is your source available, or can you make a miniature repro? @gcanti thanks for clarifying, I just misunderstood your terminology. |
@gcanti Interestingly, emacs with tide still has the long startup time but if you wait then it doesn't stop working afterward. So I suspect that VSCode is somehow still using some 2.3 code even though it is pointed to the latest code from Edit: It's probably because emacs only does buffer-wide error checking when explicitly asked to. VSCode does it in the background when you're not typing. Maybe VSCode is getting overloaded because checking the entire file takes so long, and requests come in repeatedly? |
@sandersn It took me ages to reduce the test case down to something I could share, but I want to help :-). This is as simple as I could get it whilst retaining the memory issue: https://github.com/OliverJAsh/ts-2.4-memory-issue Hope that helps! |
Thanks, |
Any ideas what might be causing the issue, and if there is any workaround? |
From poking at it with the debugger, it looks like type inference starts comparing various failure Monads like Typescript in general has trouble whenever it has to do extensive structural comparison. We tried to help comparisons with lots of overloads with #15519. This was specifically aimed at I'll try to further reduce |
@sandersn do you have any recommandation as how to speed up that inference from a library author perspective? (in our context) |
No recommendations yet, but I discovered the reason that the per-class tags weren't helping, and why Anders' recent change to improve overloads didn't help enough (fp-ts doesn't have many overloads that I noticed, and it doesn't use Array much either). The compiler still gets stuck in a structural comparison of very complex types inside type inference.
I'm not fluent enough with the architecture of type parameter inference to see any avenues for optimisation yet. I'll consult with @ahejlsberg in the next few days to find out if there's something we can do. |
Here is one workaround: Always provide type parameters. This avoids type inference, but is tremendously inconvenient. So I don't think it's actually feasible. |
Reducing the inference-too-deep cap to 3 nested occurrences of the same type makes compilation complete in 3.3 seconds. Reducing it to 4 makes compilation complete in 10 seconds. |
@sandersn how can one configure that inference-too-deep cap ? |
@sledorze the inference-too-deep cap isn't configurable right now. Setting it too low will start to create bogus inferences of Node crashes don't give much of the stack, as you can see. I debugged using the following: $ node --inspect --debug_brk ~/ts/built/local/tsc.js Then I opened Chrome to the provided URL and let the compiler run for a while. Each time I did this there were many recursive calls in inference on the stack, so I guessed that inference was causing the out-of-memory crash. (You can substitute any copy of |
Update from talking to @ahejlsberg. The inference-too-deep cap should maybe be removed entirely — it was a mistake from when we believed that inference should behave similarly to assignability checking. @ahejlsberg is working on a fix that just quits when inference sees an instantiation of a type that it has seen before. That should fix this bug. I'll also add fp-ts to our real-world code test suite after the bug is fixed so that we can catch compiler bugs in this library and similar FP/monad libraries. |
fp-ts is now part of our RWC corpus so we should notice any performance regressions in the future. |
This fixed the issue for https://github.com/OliverJAsh/ts-2.4-memory-issue, but I ran into another scenario where the memory issue pops up again. I've created a small test case in https://github.com/OliverJAsh/ts-2.4-memory-issue/tree/2.4.0-dev.20170519. |
@sandersn Do you have any idea what is causing the above? |
@sandersn note that the problem happens with fp-ts when there's a type error in the program, not compiling an actually well typed program (this is from my observations). Also considering this problem, the worst case is not when one changes some code and have the compiler getting lost in inference, the real issue is when one upgrade a library and it breaks the compiler without any clue where the type error is and how to fix it. |
@sandersn it should be reopened IMHO |
@sledorze I opened a new bug based on @OliverJAsh's repro: #16029. If you have another repro please link to it there. I'm keeping this bug closed just to make bookkeeping simpler. And it's unlikely that the compiler is getting stuck in inference again (or at least, for the same reason). |
I'm not sure whether or not the pain point I had today was caused by this issue, but I also was running into the problem of Node running out of space on the heap. I reverted to 2.3.2, and was able to successfully compile. If you guys can nail down this bug, it would be greatly appreciated! |
@bryanerayner there are, unfortunately, many reasons for typescript to run out of memory while compiling fp-ts. The root cause of this issue is definitely fixed: type inference no longer recursively examines types that it has already seen. But (for example) assignability checking still does recursively examine types. It's likely that the issue you're hitting is different. Can you open a new issue and give some detail about your code there? |
TypeScript Version:: 2.3.1
Repo: https://github.com/gcanti/fp-ts
After upgrading typescript from 2.2.2 to 2.3.1 the compilation (
npm run build
) fails with the following error:Somehow seems related to the modules
Tuple.ts
andApply.ts
In the module
Tuple.ts
if I comment the following linesthe compilation doesn't crash.
Alternatively if I don't touch
Tuples.ts
but I comment out all theliftA*
functions inApply.ts
the compilation doesn't crash.Expected behavior:
Compilation success
Actual behavior:
Compilation fail
The text was updated successfully, but these errors were encountered: