[Mono-list] Int32

Jonathan Pryor jonpryor@vt.edu
Fri, 29 Oct 2004 18:23:10 -0400


On Fri, 2004-10-29 at 09:58, Pedro Santos wrote:
> In 64 bits processor arquitectures(64BA) will Int32 still have 32 bits?

Probably.  I know AMD64 still uses 32-bit ints -- it reduces memory
traffic, keeping performance sane.

> Would that be a performance issue? 

Yes.  It will *improve* performance, as you can copy 2x the number of
32-bit ints as 64-bit ints. :-)

> And if we have 512BA?

If we have a 512-bit architecture, int's will continue to be 32-bits. 
We'll also likely have Int128, Int256, and Int512, as well as their
unsigned counterparts.

> My issue is: wouldn't be better to have a C/C++ style int

No.  This "works" (see below) for C/C++ because they aim for source-code
portability.  .NET and Java aim for binary portability (that is, you can
take your x86-compiled program and run it unchanged on a PowerMac G5).

If the size of int varied from platform to platform, you would have:

  - Inconsistent behavior between platforms (when an overflow occurs 
    would change, depending on sizeof(int)).
  - Increased memory traffic on large-integer platforms (since each int
    is twice as large, at least, you have 2x the memory traffic, and
    memory is already slow compared to the processor)
  - An inability to interoperate with native code (via Platform Invoke),
    as you wouldn't know how big your ints are, nor how big the platform
    ints are.

Developers hate flexible-sized integers.  Really.  Flexible sizes make
it impossible to maintain portable a binary file/memory/network format,
since it's possible that no platforms will agree on sizes.

Furthermore, developers frequently assume the size of various data
types, even if it isn't portable, resulting in numerous portability and
compatibility problems.  (Why do you think Windows 64 is a P64 model,
where ints and longs are 32-bits?  It's because an LP64 model would
break too much existing code, because developers suck and make too many
assumptions.)

To further emphasize the point, see the Linux/GLibc/GLib source, all of
which contain explicit size-based integer typedefs.  C99 has
standardized a set in <stdint.h>, such as int32_t and int64_t (both of
which are technically optional, but will be present on most platforms).

> Like IntPtr in the CLI? What are the benefits of a fixed 32 bit integer?

The benefits were touched on above:

  - Better memory efficiency, since you only use in memory what you need
  - Ability to use existing native code, as that will use fixed-size
    integer types
  - Portability between differing platforms
  - Developer sanity
  - Developer expectations

 - Jon