Idea for ECMA/C# Standard - compile time hash for performance





Many times we have a bunch of enums we have from either different enums or
the same enum that will have various numeric values assigned. Rarely will
there be collisions in numbering between the enums. These enums you might
imagine would be like OrderPrice=27, OrderQuantity=50, OrderSide=62. There
may be a lot of these. So normally what we would have to do is store these
in hash table so hashtable[OrderPrice]=value could be set with the value.
Hashing is very expensive in terms of performance at the level we deal with.
What would be nice is if the C#/C++ compiler would allow me to do something
like this:
Below example is more C++ pseudo code than C# but you get the idea.

Imagine an original enum:

enum OrderInformation {., OrderPrice=27, OrderQuantity=50, OrderSide=62};



Now imagine if at compile time the compiler could do the hash for us like:



enum map LocalOrderInformation { OrderInformation.OrderPrice alias
OrderPrice=0, OrderInformation.OrderQuantity alias OrderQuantity,
OrderInformation.OrderSide alias OrderSide};



So basically this tells the compiler to create me a new enum mapping the old
enumeration to the new mapping to be 0 based or 1 based so that now we can
use a direct array lookup rather than a hashmap.



So now we could do something like:



int OrderInfo[3];



void OrderClass::ProcessOrder(LocalOrderInformation info, int value)

{

OrderInfo[info]=value;

}



So that a caller if they did something like this:



OrderClassInstance.ProcessOrder(OrderInformation.OrderPrice,27);



The compiler would translate the OrderInformatin.OrderPrice enum to our
LocalOrderInformation enum value for that enum to be 0. Now we can do a
direct array entry rather than a hashtable.



I'm sure there are numerous issues here to work out and it is not quite as
simple as the pseudo code above. But if it were and we could get the
compiler to do this mapping for us our runtime code would be much improved.

I'm sure there are technical hurdles and the need for improved syntax, but
what do people think of the idea? I think it would help a lot of stuff I
work on to avoid hashtables. Thoughts, ideas for better syntax, different
variations?

Thanks,

Dave




.



Relevant Pages

  • Re: Idea for ECMA/C# Standard - compile time hash for performance
    ... I agree with you the chance of a compiler change is slim, ... and then delegating to the standard hash for fields accessed less frequently. ... or the array lookup which would require the ... > 64-bit architecture) for each enum value that doesn't map to anything. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problems with enums across a dll interface?
    ... > on compiler settings the size of an enum could change and lead to memory ... the enum size could change. ... You need to be careful when adding enumerators. ...
    (comp.lang.cpp)
  • RE: passing enum value as an argument
    ... > hours after your only previous reference to Option Strict? ... indeed you would have an Enum, and you WOULD have to use the same type on the ... the compiler would force you to ... >> Public sub New ...
    (microsoft.public.dotnet.general)
  • Re: enum?
    ... It being a particular C compiler, it gave an error stating that I would have ... You are right, in all my samples using enum, is shown as you say! ... void doErrorBlink; //Function declaration ... enumeration variable of type ERROR_CODES then it would be usefull, ...
    (microsoft.public.vc.language)
  • Re: Compiler resolves to wrong overloaded constructor or method
    ... > guess would be that the Enum cast doesn't check type, ... As far as I can see, that's the only place the implicit conversion should ... >> compiler implementation, rather than being derivable from the language ... > the spec entirely, so I can't state that the spec doesn't define this. ...
    (microsoft.public.dotnet.languages.csharp)