-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Closed
Labels
Area-CompilersCode Gen QualityRoom for improvement in the quality of the compiler's generated codeRoom for improvement in the quality of the compiler's generated code
Milestone
Description
I'm not sure of the right repo for this as it is a suggestion for C# optimization, not language.
I was talking with @tannergooding about the implementation of CompareTo in the BCL, which today is this: (likewise for Int64 etc.)
public int CompareTo(int value) {
// Need to use compare because subtraction will wrap
// to positive for very large neg numbers, etc.
if m_value < value) return -1;
if m_value > value) return 1;
return 0;
}He suggested that the following would be faster as long as the (x > y) ? 1 : 0 and (x < y) ? 1 : 0 are emitted as the .NET instructions cgt and clt.
int tmp1 = (x > y) ? 1 : 0;
int tmp2 = (x < y) ? 1 : 0;
return tmp1 - tmp2;We think it is reasonable for the C# compiler to emit these sequences like this when optimization is on.
Context: dotnet/fsharp#13187
kindermannhubert, ma3yta, adelarsq, pentp, omariom and 3 moreadelarsq and l3m
Metadata
Metadata
Assignees
Labels
Area-CompilersCode Gen QualityRoom for improvement in the quality of the compiler's generated codeRoom for improvement in the quality of the compiler's generated code
Type
Projects
Status
Done