Syntactical sugar for common optimization tools/patterns #4582
-
As a relatively new programmer the following proposals might be either farfetched or unreasonable. Various common optimization techniques require advanced knowledge of the C# language, most notably; Inlining functions with MethodImplA new version of C# could include alternative syntaxes for [MethodImpl(MethodImplOptions.AggresiveInlining)]. One possible shorthand could be a new keyword, similar to the inline keyword in c++. Such a keyword would act as a modifier to any functions, static or otherwise, and possibly for property getter or setters which access only public items. Theoretical Example Code public inline static int Bitmask(int bitToMask) => 1 << bitToMask;
public static void Main(string[] args) {
Console.WriteLine(Bitmask(1));
// Would evaluate to "Console.WriteLine(1 << 1);" or
// "int _b = 1; Console.WriteLine(1 << b)" before compilation
} Another possibility would be to simply introduce a new Attribute (or Attribute-like item), simply named Inlined, which is simply syntactical sugar for [MethodImpl(MethodImplOptions.AggresiveInlining)] Example [Inlined]
public static int... // Continue as in other example Bit ManipulationOne of the most "hacky" yet common bit manipulations that can be observed in C# is the conversion of the raw binary of one type to another. The most common way to do this is to use pointer casting; Example float num = 1;
int numRaw;
unsafe {
numRaw = (raw int)num;
}
Console.WriteLine(numRaw); // Prints 1065353216, the integer representation of the bytes behind 1f Thank you so much for reading, hopefully this is at least somewhat well thought-out. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
Given that these are both relatively advanced topics and should be used sparingly by developers it does not seem appropriate that the language should encourage their use through shorthand, especially since both are solved problems. |
Beta Was this translation helpful? Give feedback.
-
The shorthand for inlining is nice - but it should be done by the compiler automatically... The second part you can write today as: using System;
using System.Runtime.CompilerServices;
float num = 1;
var numRaw = Unsafe.As<float, int>(ref num);
Console.WriteLine(numRaw); |
Beta Was this translation helpful? Give feedback.
The shorthand for inlining is nice - but it should be done by the compiler automatically...
The second part you can write today as: