-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Optimizing Compiler #1151
Comments
I'm still curious about this bit of the original conversation:
I'm not seeing what the problem is with actually using the specialized tools out there that already do a good job in this space for that purpose. |
Much of what you'd be asking for (and the difficulties associated with that) is covered in some of the comments in #8. |
@danquirk, It is not about minifing the output. This minification can be done by other tools and I don't see much advantage on doing this at the compiler. I'm talking about optimizing for speed, like function inlining, loop invariant motion, constant propagation, etc. @CyrusNajmabadi, I agree there is other tools capable of doing the job, like Closure Compiler. But the TypeScript compiler would be able to optimize more and better because it has one thing that the Closure doesn't: Type information. Right now most of the time information is just lost after compilation. One simple example of optimization that only this compiler can do:
Could output:
The point is that this optimization is impossible to do without the knowledge that Another simple idea if the type
Could output:
Thus: The TypeScript compiler would have advantage over any other tool out there. |
hey @GuilhermeBernal , great examples. However, i have to say personally, that i'm very put off by them. A strong goal for me about typescript is that it is a true erasing compiler. The type information has absolutely no impact ever on your final code. Indeed, the way we represent typing issues are really as 'warnings' and not as 'errors'. That's why you can have typing warnings, and we will still generate javascript. Furthermore, we try not to touch your code at all. The only places we really do any manipulation are when you're using an ES6 construct, and compiling down to ES5. If, for example, you were doing straight TypeScript-to-ES6, then all i would want us to do is remove the type annotations, and not touch your code at all. Moving away from that is not something i would want to do without some very very strong reasons. And simplistic constant folding simply doesn't fit the bill for me :-) As for ASM.js transformations. I think that's definitely something that could be considered in the future. But i see that more at the point of a plugin that would affect compilation, and not something built into the language itself. But the discussion is certainly worth having. |
I realize this issue isn't specifically about minification, but it shares a similar theme of 'the TypeScript compiler knows about types therefore it should mess with my emitted JavaScript emit by doing x,y,z' when there're an incredibly large number of circumstances some people are and are not willing to tolerate those emit differences (and what exactly those differences would be). Some other issues with comments related to constant propagation and asm.js: #375, #720. Cyrus has already covered some of our general philosophy here and how important preserving idiomatic JavaScript is to us (because of how important it is to so many of our users). See https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals, particular non-goals 2 and 5. |
Ok, I understand. I see why a lot of people would prefer a simple readable and mostly unchanged output. It all have already been well thought and I'll close this issue. I've just started to have a look at TS and I'll learn more of it for now. Anyway, I want to leave here the idea. Maybe it is too soon to support this but: introduce compilation flags! Things like "enable fatal errors instead of just warnings" or "enable optimizations" or maybe "enable minification". All disabled by default, but all available. It would add a lot more power to the compiler, but yes, maintenance cost increases a lot. |
Why was this issue closed? Personally, I don't see much of a benefit of using TypeScript or Flow instead of JavaScript. Especially now that the ES6 standard also added class-based object-oriented programming to the language, all you'd really get from adopting TypeScript or Flow today would be the addition of static typing... and interfaces. Besides making your code look more like eg. Java code, I really don't see the benefit at all... except when using static typing to optimize the JavaScript code it produces... which is where asm.js comes in. In environments where high performance is essential (eg. 3D rendering in the browser for browser games or GIS web apps), the current approach is to write the core code (that does the heavy lifting) in C/C++ (compiled to asm.js) and everything else in JavaScript. That means you need a really good understanding of both JavaScript (which I do) and C/C++ (which I don't) to be able to write proper code or maintain existing code for such apps, both inside and outside of the the core code. How great would it be to be able to use one - TypeScript - codebase for such projects and compile to asm where possible and to regular JavaScript where not possible? Or to put it differently : how great would it be for a JavaScript programmer to be able to program in highly readable superset of JavaScript and have it compiled - where possible - to a highly optimized subset of JavaScript? It seems like a win-win for everyone, really, especially if you leave it optional whether or not to compile to asm.js for developers who don't work in high performance environments and prefer readability to performance. IMO, compilation of (a subset of) TypeScript to asm.js would be major improvement over TypeScript as it exists today. For me, it would totally be a game changer and make the difference between supporting and not supporting the adoptation of TypeScript - or Flow for that matter - at the company I work for... While I understand that TypeScript is not intented to be compiled to asm.js, C/C++ or WebAssembly (and I expect that), the ability to export a language like Flow or TypeScript to typed AST in some way or another would be a good first start. With the TypeScript core devs not interested in this feature, it would at least open up the possibility for other teams to implement TypeScript to WebAssembly, TypeScript to C/C++ or TypeScript to asm.js conversion wherever possible. |
The person who raised it, closed it almost 3 years ago. That is why it is closed. |
@kitsonk I noticed. That question was actually specifically directed toward @lbguilherme, although I guess that wasn't obvious. Those also interested in converting staticly typed variations of JavaScript to asm.js, WebAssembly or C might want to take a look at ThinScript, TurboScript or AssemblyScript. ThinScript compiles to JavaScript, WebAssembly, and C. TurboScript compiles only to JavaScript and WebAssembly. AssemblyScript compiles to WebAssembly only. Each of these languages have been inspired by TypeScript, and the latter is a subset of TypeScript. I'd like to thank @RReverser for pointing me in the direction of these languages. |
See: https://typescript.codeplex.com/workitem/1542 by danieljsinclair
The text was updated successfully, but these errors were encountered: