-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Proposal: Compile time expressions #12238
Comments
You mean it's not already converted to constant? |
JIT compiler might optimize it but I want the language compiler to do it. In the generated IL code the string is constant but the value of its length is not. |
I gave string as example. I want to be able to use more expressions to evaluated at compile time. Something like |
What is the reason? Is it about performance? Or something else? |
The reason is simple. If we know that the value of an expression at run time is exactly same as at compile time, there is no point in deferring evaluation to run time. |
@nvmkpk : I do quite like the idea, I do however see the following problem: Imagine, that one is writing a (memory) profiler. The profiler would not register the function call My second question/concern: To which degree do you want this "optimization"? Should it apply to each completely deterministic function(-chain) with only constant input? EDIT: or should one use a pseudo-attribute like |
@Unknown6656 That already happens, in the JIT compiled machine code, there is no call to And you can't put attributes on expressions, at least not without #6671. |
@svick : Interesting, I did not know the fact about Concerning the usage of attributes on expressions: During the composition of my previous comment I assumed, that issue #6671 will be implemented in the future C# version, as it has been marked as |
Hopefully in Release build, all of the optimizations.
Where-ever compiler can statically determine static or read-only or const-ness and exploit it 👯 |
That is fine for optimization part but I would like the developer to also call for compile time evaluation by using something like |
@choikwa (, @nvmkpk ): So, would the following expression be optimized (assuming, that a char a = (new string[] { "test", "string" })[1].Reverse().ToArray()[3]) - 'B';
// a now stores the character `0` My point is: I really do love your proposal, but does too much optimization not take too much time on a big scale? Are arrays defined at compile time also a constant, if they remain unchanged? If anyone is interested, how the character is calculated in the example above: string[] arr = new string[] { "test", "string" };
char[] elem = arr[1].Reverse().ToArray(); // [g,n,i,r,s,t]
char r = elem[3]; // 'r' (0x72)
char a = r - 'B'; // 0x72 - 0x42 == 0x30 = '0'
// a now stores the character `0` |
A compiler worth its salt would optimize. I am hoping CoreCLR is to be such one. Maybe you can help make that happen 😄
C++ already does this with constexpr and template meta. A saving grace is that you only do this once during compilation. In C# context, maybe this makes more sense in AOT compilation or more expensive ReJIT (don't we all want tiered JIT?). PS. a lot of compilers already neuter optimization if method size is too large, to the detriment of performance just because compilation time would be excessive. Would not surprise me if CoreCLR had to do something similar since it's a VM. |
@choikwa I think that you have me convinced on that point! 😄
I will try as much as I can 😉 |
FYI this very issue is open in https://github.com/dotnet/coreclr/issues/3633 |
That is for optimzation. This proposal is about letting developer write general compile time expressions not just for string length. |
Sorry for hijacking the issue. You are correct, and I agree that this should have some dependence on success of https://github.com/dotnet/roslyn/issues/6671 |
On the syntax -- I suggested |
@nvmkpk May you move this to csharplang repo? |
Issue moved to dotnet/csharplang #1028 via ZenHub |
It would be useful to specify something like
"Hello World".Length
in some special syntax that gets evaluated at compile time and replaced with 11 (becoming a constant) in the compiled binaries.The text was updated successfully, but these errors were encountered: