Search code examples
c#performanceenumsstatebitvector

Most effecient way to keep status flags (under 32 items) in c#


Consider we are defining a class that:

  1. Many instances of that class will be created
  2. We must store under 32 flags in each instance that keeps states or some options, etc.
  3. Defining flags count is fixed and we no-need to keep it in an enumerable variable in runtime. (say we can define separate bool variables, rather than one bool array)
  4. Some properties (from each instance) is depended on our flags (or options) And flags states Will be used (read/write) in a hot call path in our application.

Note: Performance is important for us in that Application.

And As assumptions #1 and #4 dictated, we must care about both speed and memory-load in balance


Obviously we can implement our class in several ways. For example defining a flags Enum field, Or using a BitVector, Or defining separate (bool or Enum or int ...) variables, Or else defining uint variable and using bit-masks, to keep the state in each instance. But:

  • Which is The Most Efficient way to keep status flags for this situation?

  • Is it ( = the most efficient way) deeply depends on current in-using tools such as Compiler or even Runtime (CLR)?


Solution

  • As no body answered my question, and I performed some tests and researches, I will answer it myself and I hope to be usable for others:

    Which is The Most Efficient way to keep status flags for this situation?

    Because the computer will align data in memory according to the processor architecture ,Even in C# (as a high level language), still It is generally a good advise to avoid separate boolean fields in classes.

    • Using bit-mask based solutions (same as flags Enum or BitVector32 or manual bit-mask operations) is preferable. For two or more boolean values, it’s a better solution in memory-load and is fast. But when we have a single boolean state var, this is useless.

    Generally we can say if we choose flags Enum or else BitVector32 as solution, it should be almost as fast as we expect for a manual bit-masked operations in C# in most cases.

    • When we need to use various small numeric ranges in addition to boolean values as state, BitVector32 is helpful as an existing util that helps us to keep our states in one variable and saving memory-load.

    • We may prefer to use flags Enum to make our code more maintainable and clear.

    Also we can say about the 2'nd part of the question

    Is it ( = the most efficient way) deeply depends on current in-using tools such as Compiler or even Runtime (CLR)?

    Partially Yes. When we choose each one of mentioned solutions (rather than manual bitwise operations), the performance is depended on compiler optimization that will do (for example in method calls we made when we were using BitVector32 or Enum and or enum operations, etc). So optimizations will boost up our code, and it seems this is common in C#, but for every solution rather than manual bitwise operations, with tools rather than .net official, it is better to be tested in that case.