Prev: Using Delegates?
Next: Drag & Drop text
From: Tony Johansson on 28 May 2010 03:34 Hi! Is it anyone that might have a good explanation why the designor of .NET made a bool 4 bytes. I mean it's just a wast of memory. //Tony
From: Peter Duniho on 28 May 2010 03:58 Tony Johansson wrote: > Hi! > > Is it anyone that might have a good explanation why the designor of .NET > made a bool 4 bytes. > I mean it's just a wast of memory. For a variety of reasons. I would guess that the most important is that there's no compelling reason for it _not_ to be 4 bytes, and making it 4 bytes keeps it consistent with other data types. While 4 bytes obviously takes up more room than 1 byte, the fact is that large arrays of booleans aren't really all that common, and there's already a 1 byte type anyway (it's called 'byte'�duh :) ). On the other hand, people generally do care about efficient access to booleans, as well as preserving atomic semantics in multi-threaded scenarios. On current hardware architectures, it's more efficient to move 32 bits around than just 8. And because of that, there's the possibility that writing to a 1-byte boolean would require first reading 4 bytes, modifying a single byte within those 4 bytes, and then writing the 4 bytes back. Obviously that's harder for .NET to make atomic than just writing to a single 32-bit word, and of course the efficiency aspect alone is a decent enough reason. When you think about it, you might as well ask why a boolean isn't just 1 _bit_ in size. The issues are actually quite similar. Pete
From: Patrice on 28 May 2010 07:24 Hi, > I mean it's just a wast of memory. As most design decision, this is a tradeoff that is you usually buy memory at the price of speed or speed at the price of memory. Here they likely considered how data are aligned in memory. From http://en.wikipedia.org/wiki/Data_structure_alignment : "Data alignment means putting the data at a memory offset equal to some multiple of the word size, which increases the system's performance due to the way the CPU handles memory. To align the data, it may be necessary to insert some meaningless bytes between the end of the last data structure and the start of the next, which is data structure padding." If needed you have specialized classes allowing to save some space likely reducing the speed such as http://msdn.microsoft.com/en-us/library/system.collections.bitarray.aspx or http://msdn.microsoft.com/en-us/library/system.collections.specialized.bitvector32_members.aspx. -- Patrice
From: Arne Vajhøj on 3 Jun 2010 22:11 On 28-05-2010 03:34, Tony Johansson wrote: > Is it anyone that might have a good explanation why the designor of .NET > made a bool 4 bytes. > I mean it's just a wast of memory. Somebody at Microsoft made a decision. One potential explanation could be that BOOL in traditional Win32 API programming is typedef'ed to int (4 bytes), so making bool 4 bytes will make it more Win32 compatible. It uses more space, but huge bool arrays are very rare. Arne
From: Arne Vajhøj on 3 Jun 2010 22:29
On 28-05-2010 03:58, Peter Duniho wrote: > On current hardware architectures, it's more efficient to move 32 bits > around than just 8. And because of that, there's the possibility that > writing to a 1-byte boolean would require first reading 4 bytes, > modifying a single byte within those 4 bytes, and then writing the 4 > bytes back. Obviously that's harder for .NET to make atomic than just > writing to a single 32-bit word, and of course the efficiency aspect > alone is a decent enough reason. But the old architecture x86 and x86-64 is actually killing the modern architecture IA-64. > When you think about it, you might as well ask why a boolean isn't just > 1 _bit_ in size. The issues are actually quite similar. In Pascal packed array of boolean usually is a single bit per boolean. Arne |