❔ Will I get a performance hit if I use the Enum type as a key value for a Dictionary?
See Code below:
Does this also create a hashmap? Or does the compiler do something unexpected here which costs performance?
22 Replies
What?
Since Tee1.None and Tee2.None are both underlying int with the value 0, how does the compiler know the difference between Tee1.None and Tee2.None?
I could also do 2 Dictionary with Tee1 as a key and in the other Tee2, my questing was just if I do it the way with Enum type as key , do i Get a performence hit
Presume it's down to an override of Equals
In that case it's the same as any other dictionary
Not really following your question. If you have two items with the same hash, they will go in the same bucket
But this whole question smacks of premature optimisation
It depends if
Enum
overrides GetHashCode
to just return the integer value. If it does, then yes, they'll end up in the same bucket. You could make a wrapper struct for the key type with that Enum
field that does the default behavior of GetHashCode
, so (int) _enum xor _enum.GetType().GetHashCode()
if you happen to need that.
(can't type caret on phone lol)yes, using Enum means that you have to box the values (aka put them on the heap)
when it's boxed, it is boxed with the information that the type of the value is Tee1
that is how it knows the difference
in theory they shouldn't box if they are used as constants
what
well
no
(Enum)Tee1.None
is a boxing conversionat least they should reuse the same reference
I don't think they do
Windows10CE#8553
REPL Result: Success
Result: bool
Compile: 449.481ms | Execution: 29.987ms | React with ❌ to remove this embed.
they do not
hm alright
uh, I'm not sure what this is meant to show
You should be able to click "Expand" otherwise, you will only see the comment at the top
I mean yes you can make a dictionary of the types
but that doesn't help you when it comes to values
Windows10CE#8553
REPL Result: Success
Result: string
Compile: 500.192ms | Execution: 34.903ms | React with ❌ to remove this embed.
see how both E.A and E.B get the same value
because they both have the type
E
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.