caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: Norman Ramsey <nr@eecs.harvard.edu>
Cc: caml-list@inria.fr
Subject: Re: questions about costs of nativeint vs int
Date: Thu, 11 Jan 2001 19:34:07 +0100	[thread overview]
Message-ID: <20010111193407.A4332@pauillac.inria.fr> (raw)
In-Reply-To: <200101101825.NAA17093@labrador.eecs.harvard.edu>; from nr@eecs.harvard.edu on Wed, Jan 10, 2001 at 01:25:27PM -0500

Dear Norman,

> We're designing interfaces for tools that will sometimes have to
> manipulate 32-bit integers, but will often be able to make do with the
> limited precision provided by the int type.  I would love to get some
> information about the relative cost of int vs nativeint in both
> parameter passing and datatype representation.

Values of type "nativeint", "int32" and "int64" are boxed
(heap-allocated and handled through a pointer), while values of type
"int" are unboxed.  So, in terms of space, we have:
        nativeint       1 word for the pointer + 2 words for the heap block
        int32           same
        int64           same on 64-bit machines, add 1 word for a 32-bitter
        int             1 word for the data
In terms of speed, operations on boxed integers are more expensive due
to the extra loads (on operands) and heap allocations (on results).
ocamlopt can avoid loads and allocations that cancel each other in
simple cases, but the fact remains that nativeint arithmetic is more
expensive than int arithmetic.

> For example, what are the relative costs (e.g., memory requirements)
> of these two definitions?
> 
>   type loc = Cell of char * int * width
>            | Slice of ...
> 
>   type loc' = Cell of char * nativeint * width
>             | Slice of ...

A "Cell" requires two more memory words in the latter case.

> As another example, what are the costs of calling these functions:
> 
>   type t
>   val add  : rd:int       -> rs1:int       -> rs2:int       -> t
>   val add' : rd:nativeint -> rs1:nativeint -> rs2:nativeint -> t

Assuming the nativeints are already available, the cost is exactly the
same: a pointer is passed instead of an unboxed integer.  However,
computing the nativeint arguments in the first place can be more expensive
if arithmetic operations are involved.

> To extend the question, what happens if I use int32 or int64 in place
> of nativeint?

No major differences.  On a 32-bit processor, int64 requires one more
memory word than the other two.  And of course 64-bit arithmetic is
slower than 32-bit arithmetic on a 32-bit processor.

Actually, "nativeint" is isomorphic to "int32" on a 32-bit platform
and to "int64" on a 64-bit platform.  The reason for having all three
types is that some applications require 32-bit integers (no more, no
less, regardless of the platform), some require 64-bit integers, and
some just want to get at the natural word size for the architecture.

> Finally, a procedural question: should I be posting these sorts of
> questions to comp.lang.ml instead of sending them to
> caml-list@inria.fr?

For technical questions about the Caml implementation, I think you get
a better chance of a reply here on caml-list.

All the best,

- Xavier



  parent reply	other threads:[~2001-01-12  9:00 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-01-10 18:25 Norman Ramsey
2001-01-11  9:17 ` Cost of polymorphic variants over normal ones Sven LUTHER
2001-01-11 10:40   ` Alain Frisch
2001-01-11 10:44     ` Sven LUTHER
2001-01-11 14:50   ` Jacques Garrigue
2001-01-11 19:14     ` Markus Mottl
2001-01-11 18:34 ` Xavier Leroy [this message]
2001-01-11 22:17   ` questions about costs of nativeint vs int Norman Ramsey
2001-01-12 12:39 Dave Berry
2001-01-14 11:13 ` Xavier Leroy
2001-01-15  2:09   ` John Prevost
2001-01-12 19:53 Brent Fulgham
2001-01-17 13:47 Dave Berry

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20010111193407.A4332@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=nr@eecs.harvard.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).