Proposal: Compile Time Function Execution #2379
Replies: 11 comments 6 replies
-
Related to:
which references:
|
Beta Was this translation helpful? Give feedback.
-
This would be great, however what if the strings to place into the format strings is only known at runtime based on user input data? I feel with the user input data which would only be known at runtime would eliminate the benefits of this only in those places. The same can also be true with application settings reading that then control what a value will be like for example the program loads an settings.json file which in turn tells it which icon to use at runtime when loading the program based on an icon index number hard coded into said program (used by some gui programs too). |
Beta Was this translation helpful? Give feedback.
-
Reading the latest blog post about Performance Improvements in .NET 6, this feature seems largly obsolete, as it is mostly covered by the constant folding approach. |
Beta Was this translation helpful? Give feedback.
-
We still need this |
Beta Was this translation helpful? Give feedback.
-
I still think this is very useful, even despite the .NET 6 improvements. Clearly, others think so, too - what with the UTF-8 string literals being added to the language. |
Beta Was this translation helpful? Give feedback.
-
This is still needed |
Beta Was this translation helpful? Give feedback.
-
Having compile time execution would be really great. Especially when having to deal with attributes. |
Beta Was this translation helpful? Give feedback.
This comment was marked as off-topic.
This comment was marked as off-topic.
-
This would be great. |
Beta Was this translation helpful? Give feedback.
-
Is this being tracked ? |
Beta Was this translation helpful? Give feedback.
-
I would love to see something like this. It would be great if the compiler took a multi-pass approach.
I use "serialize" lightly because the serialization should return C# code that can generate the object. |
Beta Was this translation helpful? Give feedback.
-
@nikeee commented on Wed Mar 09 2016
Let's take this code:
This works as expected, therefore, the value of
AnotherConstString
will be set to"FooBar"
by the compiler. The IL does not contain anything likeAConstString + "Bar"
. Instead, any use of the constant values above will be inlined to its constant value.Now take a look at this code:
This already works as of today. The difference here is that
AnotherConstString
is clearly a constant value that can be determined at compile time. So what ends up in the IL is a reference to the static fieldAnotherConstString
whileCreateAnotherString
will be invoked by the static constructor during runtime.The code:
Compiles to this IL (with optimizations enabled):
So, if the compiler knew that
CreateAnotherString
has no side effects and its return value is entirely based on its parameters and the parameters are compile time constants, the compiler itself could evaluate the function.The function is required to be a pure function. There already is a proposal for pure functions, since they may have additional benefits.
This could also affect cases like these:
Currently, what ends up in the IL will be something like this (translated back to C#):
If
string.Format
is a pure function, all parameters are compile time constants, so the compiler could actually invoke string.Format and emit the result instead:Of course, if a function is pure, it can remain in the compiled assembly, in case someone invokes it with non-constant parameters.
Other thoughts:
const Vector2 upLeft = Vector2.Up + Vector2.Left
would be a constant expression. This is actually the same thing as in the code ofSomeClass
above, just using Vector2 instead of string.string.Format
,Convert.*
,string.Join
) without losing compatibility."FooBar"
is a compile time constant, so"FooBar".Length
or"FooBar".get_Length()
may return constant values. Imagine using it in a scenario likestr.Remove(str.Length - "RemoveMe".Length)
, while the code remains maintainable because"RemoveMe".Length
is more self-explanatory than just8
. Ifstr
is a constant andRemove
is a pure function, the entire expression may be computed by the compiler.This feature would be pretty similar to other languages supporting CTFE, like D or C++11 with constexpr.
Also, CTFE doesn't have to be implemented in the compiler that compiles to IL. It may be also implemented in the JIT or other AOT compilers.
@svick commented on Wed Mar 09 2016
In the case of
static readonly
field, what is the point of optimizing the IL? Under normal circumstances (JIT compilation), it will behave as a constant during JIT anyway (i.e. the CLR executes the field initializers and only then JIT compiles the method, knowing the field can't change).In the case of interpolated strings,
string.Format()
is not a pure function (it depends on the current culture), so this also wouldn't help (see #9212).The
constant.Length
optimization sounds like something the JIT compiler should be able to do (but as far as I can tell, doesn't currently do).Beta Was this translation helpful? Give feedback.
All reactions