6 Replies - 4479 Views - Last Post: 23 December 2013 - 02:28 PM

#1 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 783
  • Joined: 27-October 11

Creating custom value types with specific memory length

Posted 23 December 2013 - 11:32 AM

I just read jhouns's tutorial Saving settings using OR, and I completely agree with the guy about bool being inefficient. The .NET framework doesn't even have a bit type. I've searched the web and couldn't find any documentation about creating new value types and manually allocating memory for them. Is this even possible? I mean, is it even possible to manipulate the .NET framework in such a way that you can actually manually manipulate physical memory?

I'm guessing a bool has the size of a byte, because it's easier to manipulate with a byte than to manipulate with a bit, but I'm guessing it's quite possible, there are bitwise operators after all, that manipulate individual bits, right? But it's not really important, I'm not asking why does the bool have the size of a byte, nor am I discussing the difficulty of manipulating chunks of memory of small sizes. I'm asking if it's possible to extend the .NET framework by adding a new value type, with the memory size of a bit?

This post has been edited by RexGrammer: 23 December 2013 - 11:38 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Creating custom value types with specific memory length

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3729
  • View blog
  • Posts: 13,017
  • Joined: 12-December 12

Re: Creating custom value types with specific memory length

Posted 23 December 2013 - 11:58 AM

Would this be suitable?

[Flags]
public enum Talents
{
    Singing = 1,
    Dancing = 2,
    Juggling = 4,
    JokeTelling = 8,
    DoingMagic = 16,
    RollingTongue = 32,
    StiltWalking = 64,
    DoingSplits = 128
};

http://csharp.2000th...um-of-32-flags/

Unless you want to dip into unmanaged data:

http://social.msdn.m...=csharplanguage
http://msdn.microsof...y/4ca6d5z7.aspx
Was This Post Helpful? 0
  • +
  • -

#3 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 783
  • Joined: 27-October 11

Re: Creating custom value types with specific memory length

Posted 23 December 2013 - 12:37 PM

Well, yeah, that's a suitable solution. And it's quite acceptable, BUT I wasn't talking about that. I was wondering if it's possible to define a brand new value type with it's own custom memory footprint (of one bit), not resorting to some other ways of efficiently using memory. And yeah, I was aware that this is impossible to do without "dipping into" (:D) unmanaged code.
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Creating custom value types with specific memory length

Posted 23 December 2013 - 12:57 PM

I highly doubt it. The whole point of the framework is that it abstracts away all the underlying low level detail that most developers don't need to care about these days. It therefore doesn't make sense to allow the creation of custom types.

Besides I really don't see many use cases where this is useful anyways.
Was This Post Helpful? 0
  • +
  • -

#5 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2538
  • View blog
  • Posts: 4,641
  • Joined: 24-September 10

Re: Creating custom value types with specific memory length

Posted 23 December 2013 - 01:31 PM

*
POPULAR

Nope, because you can't have anything with a memory foot print of 1-bit. Where is this one bit going? What section of memory stores only 1-bit specifically?

Let's explain what I mean.

For example your processor has a word size defined for it. Let's say it's 32-bits... you feed ANY piece of data into that processor, it's going to look at it in the word size. So your small little 1-bit will still be fat in the processor. So no real gains there. Even the byte and short fatten up in the processor. The more compact types just are smaller with their memory foot-print.

So you may be able to save some memory by going to this 1-bit. But does the memory even store in that small a size? No, it doesn't support it. In .Net the minimum is going to be 1-byte for the data (8-bits, which is actually a machine specific thing, not a language thing).

Note that also smaller sizes than the word size actually can impact efficiency as it isn't aligned to the word size of the system. But it is useful for packing memory in.

If you want to pack booleans in, and have multiple of them, use the suggested 'flagging' technique. I've even seen some people implement a 1-bit boolean collection. Basically just a class that stored boolean values in an array of integers. Note though, because the array of integers also required an integer to store the length. You're not going to gain memory efficiency from it over a bool array until the length is over 8 (a bool array stores bools in a byte, 2 32-bit ints is 8 bytes... even an int and a byte and your's at 5 bytes, so length 5 before efficient).

But then again, this bit-wise bool collection comes with processing overhead to read and write those boolean values into it. You have to perform those bit-wise operations on bytes/shorts/integers. Are you storing that many boolean values in one place and then creating tons of them? What do you have a struct with 8 boolean flags on it, which then get created thousands at a time and filling up your stack? Use a flagging enum... the answer is sitting right there. The scenario for a massively large bit-stored boolean array (the only place it is even useful) is so slim that the people NEEDING it will roll their own version of such a collection just for the use.

Really though... do you think Microsoft (or even the guys who ported .Net to opensource as Mono) didn't think they could do this? I suspect they probably had good reason to keep it at the memory width it is.

This post has been edited by lordofduct: 23 December 2013 - 01:42 PM

Was This Post Helpful? 5
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Creating custom value types with specific memory length

Posted 23 December 2013 - 02:06 PM

.Net also has the System.BitArray type.

Dim b As New BitArray(128)


Was This Post Helpful? 0
  • +
  • -

#7 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 783
  • Joined: 27-October 11

Re: Creating custom value types with specific memory length

Posted 23 December 2013 - 02:28 PM

Thanks lordofduct! You're a bag full of awesome. I suspected something like that is happening behind the curtains, but had to ask to be sure. Your explanation was very clear and kneat.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1