-
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
Compiler calculated values support #720
Comments
Are you aware of any existing languages that behave like this? The two parts of your request are fairly distinct from one another and both large, fundamental pillars of a type system. I'm not sure of any languages that do the first suggestion (maybe some techniques with C++ template metaprogramming) while the latter is a core feature in some functional languages but which have huge ramifications for the language as a whole. It's hard to imagine us doing either of these things given our current goals and constraints. |
constant propagation or constant folding is a common compiler optimization. I would see us doing something like that if we support minification. |
We might do constant propagation, but I don't think there's any chance of evaluating user functions at compile-time. What if |
I agree that it is two separate requests, C++ currently support both - and this gives a lot of power to C++. The first part, pre-calculation of constants is very easy to perform, but i think it is best done in the context of the second one, and not part of minification. Analysis of what function/variable is accessible from outside and what not is very easy to do (the compiler already check for accessibility when you try to reference a symbol) If a symbol is not accessible from outside - and not from a code that is references from outside - it should be a natural candidate to be optimized out (call it elimination of simple case of dead code) - i think everyone will appreciate a compiler warning if something like this is found (and here we have another feature request in this thread) Finding out whether a function has side-effects is just as easy:
I agree that opening this up will offer a completely new way to program in TypeScript that is not avaliable in JS - TypeScript meta programming. But i see that as a great thing, not as a problem. |
@RyanCavanaugh I disagree on both points: if there is a bug in my code - i prefer my code to hang during compilation (timeout can server as a way to handle this) and not at run-time. In fact every error i can move over to the compile time is a benefit in my mind (this is the major advantage of Type safety over contract libraries) Writing code that does not access global values but accept the necessary values as parameters is considered good coding practice and is encouraged almost everywhere - that does not mean that the values do not come from the global world, it just means that the function does not access the global world aside from the parameters it gains. We do have one problem more complication i did not addressed with executing code at compile time - and that is function objects and instance objects - those will have to be replaced by code that generates them by the compiler, this can lead to a slightly simplified code, but still code. |
I'd love to see a compile time option that does optimization techniques. I don't think it should be the default (I love that typescript is like 95% the same as the generated javascript), but I do think it should be available.
I don't think there's any need to. You just need dead code elimination, constant propagation and an inliner. It wouldn't necessarily handle this specific case with the fib code, but it could optimize away a lot of similar functions. (In theory it could handle this case through repeated inlining, but it probably shouldn't handle recursive functions like this, could cause the compiler to take minutes just optimizing fib(200)). |
I agree that it should be controlled by compiler options, maintaning the While we talk about compiler options, it will be nice to get the
|
You probably meant @mirhagk. |
Probably, from my cellphone email i have limited view of the gh names
|
I'm not sure why this would be something you'd need the compiler to do. This sort of constant propagation is most appropriately handled by the actual runtime (which has all the real data to do this accurately). |
Yes this is true that the runtime does all of this. Perhaps if an optimizing compiler is to be considered, a different code gen philosophy could be considered. Perhaps TypeScript could generate asm.js as a compiler target. With that compile time optimizations would likely be appreciated. |
See #375 re: asm.js. |
Yeah so basically no shot. Better off with compiling to something like LLVM then, which is a LOT more work than the transpiling that typescript currently does. |
When i write typescript i write it to run on the JS runtime in the node.js or browser envirenment i intend for it to run. I use it to ease my code by using a better syntax (TypeScript & ES6 over ES5/3) I use linting (another compiler) to help me generate more readable code if that compiler phase can take one more step and optimize my code it will be great. This is not the same as compile to asm.js or LLVM since i still want it to run on the same runtime environment i intended for it to run in the first place - with the same rules about what can be done to it. With the same ability to mold the resolting JS objects using other (including non typescript) code. I am looking over some code written by others in the last few days, it is filled with utility functions that are only accessible from a certain code segment, and are only used when the code load (static initialization) - most of this code can be optimized out. I do not want to do that by hand, since the separation of that code to its own functions makes sense to me in terms of code design and readability. but why maintain all that code in the runtime? those are functions that are memory leak in their nature. they are defined inside a specific scope, and can not be accessed from outside, they are called once and only once at the initialization of that scope, and then those function objects just sit around with their code until the runtime dismiss their scope (possibly never). Will something like this create a new different language - it sure will, currently TypeScript is good for people that want to write JS with types (C with classes comes to mind) but the compiler offer a power that can lift TypeScript to higher places, and still maintain it JS compatible. |
I forked TypeScript to see if i can start working on this on my own, not sure how slowly that will take me, but if anyone is willing to help, just send a relevant PR once some milestone is reached, i will send a PR back to here |
@lee-elenbaas dead code elimination and constant propagation get you the majority of what you want here. The The google closure compiler actually optimizes javascript like this (in fact doing all of the optimizations except the It's possibly worth investing time to see if there are certain optimizations that closure could make if it had access to the type information, but offhand I'm unaware of any serious optimizations it could make (there are certainly many that the runtime could make, but closure still compiles to javascript) |
@mirhagk After diving in a bit to closure, i think your are right |
I would love to see support for compiler calculated constants:
Things like:
i like them to be translated to something like this:
This will allow writing a more readable code, without paying the computational price at run-time.
This idea can be expanded to also support pre-calculation of functional only code:
can be translated to:
this will force the compiler to mark as part of its type information. whether something is functional or has side effects (accessing anything outside its internal scope), and where code is usable from, and whether or not it is ever used from within any function that already exists.
The text was updated successfully, but these errors were encountered: