Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug in compiler causing it to get 100% cpu and never ends! #10697

Closed
nisimjoseph opened this issue Sep 3, 2016 · 7 comments
Closed

bug in compiler causing it to get 100% cpu and never ends! #10697

nisimjoseph opened this issue Sep 3, 2016 · 7 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@nisimjoseph
Copy link

TypeScript Version: nightly (2.0.0-dev.20160903)
The compiler continues on 100% CPU and never end the operation.
this is caused by using very long IF statement like the one exists in Device Detection in the following file URL:
https://github.com/hgoebl/mobile-detect.js/blob/master/mobile-detect.js

you have here a long if, in the "mobileGrade" function, this IF statement stuck the compiler when you take that JS file and use "allowJs" compiler option.
in addition, when it ported that file to TS and kept all content as is, just changed some things, it stuck as well.
after cutting the file in different areas I found it that IF causing ​hat issue. no matter what IFs parts you cut, it just passes a number if IF parts and stuck.

Code
https://github.com/hgoebl/mobile-detect.js/blob/master/mobile-detect.js
with allowJs compiler.

// A *self-contained* demonstration of the problem follows...

Expected behavior:
finish the compilation with/without errors
Actual behavior:
just stuck on 100% CPU and never ends.

@vladima
Copy link
Contributor

vladima commented Sep 4, 2016

can be reproduced without allowJs, gist with an extracted snipped that demonstrates the problem: https://gist.github.com/vladima/86e528ee00cf64c8d3132fef57252a07

@vladima
Copy link
Contributor

vladima commented Sep 4, 2016

at glance it seems that culprit is the narrowTypeByTypePredicate since i.e. in order to get resolved signature of t.os('iOS') we need to get the type of t which in turn requires to do more narrowing. Effectively then deeper we do the right hand side of complicated binary expression in the condition of if statement then more repeated nested calls do narrowTypeByTypePredicate we do which quickly turns exponential. As a test I've converted type predicate like calls into logical expressions (effectively replacing t.os('iOS') with ts.os('iOS') === true) - with this change in original .js file it can be compiled in ~1.5s.

@vladima
Copy link
Contributor

vladima commented Sep 4, 2016

also with naive cache in narrowTypeByTypePredicate using key below unchanged orignal .js file can be compiled in ~1.6s

const key = `${getNodeId(reference)}|${declaredType.id}|${assumeInitialized}|${flowContainer ? getNodeId(flowContainer) : 0}|${getNodeId(callExpression)}|${type.id}|${assumeTrue}`;

// CC: @ahejlsberg

@ahejlsberg
Copy link
Member

Fix available in #10703.

ahejlsberg added a commit that referenced this issue Sep 4, 2016
@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label Sep 4, 2016
@ahejlsberg ahejlsberg added this to the TypeScript 2.0.3 milestone Sep 4, 2016
@ahejlsberg ahejlsberg self-assigned this Sep 4, 2016
@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Sep 4, 2016
@ahejlsberg
Copy link
Member

@nisimjoseph Fix will be in the next nightly build.

@nisimjoseph
Copy link
Author

wonderful!

mhegazy pushed a commit that referenced this issue Sep 6, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Sep 6, 2016

Ported to release-2.0 in #10726

ghost pushed a commit that referenced this issue Sep 6, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants