Search ProofOfProgress Blog

Saturday, February 13, 2016

Use structs when 16 bytes or under

Source: https://msdn.microsoft.com/en-us/library/ms229017%28v=vs.110%29.aspx
✓ CONSIDER defining a struct instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.
X AVOID defining a struct unless the type has all of the following characteristics:
  • It logically represents a single value, similar to primitive types (int, double, etc.).
  • It has an instance size under 16 bytes.
  • It is immutable.
  • It will not have to be boxed frequently.
Also:
I think I'll name my structs with lowercase letters.
Constant class... But things like "float", "unit" and other built-in  primitive value types
have lowercase letters. So I'll name my structs with lowercase letters.

How much is 16 byes?
1BYTE == 8BITS.
ARGB pixel = 32bits. Each channel is 8Bits. AKA: 32 bit color is 4Byte color.
How many pixels in 16 bytes?
Four. (4+4+4+4 == 16)
So, think of a struct as 4 pixels worth of data or less.

No comments:

Post a Comment