caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: gje.hennequin@gmail.com
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] mixed functional / object style
Date: Mon, 20 Apr 2009 14:34:40 +0900 (JST)	[thread overview]
Message-ID: <20090420.143440.83528006.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <d8e32b760904180748h25b5dc06t3e204fbdc8caafde@mail.gmail.com>

From: Guillaume Hennequin <gje.hennequin@gmail.com>
> Dear list,
> 
> this is a somewhat naive question
> let's define
> 
> class a = object
>  val mutable v = ...
>  method v = v
>  method m = something that uses v
> end ;;
> 
> now assume that I want to create a lot of those a objects, so many that I
> may encounter memory problems.
> 
> I thought the following would be better, memory wise, but when I test it
> doesn't seem to be the case
> 
> class a = object
>  val mutable v = ...
>  method v = v
> end ;;
> 
> and instead of each object having its own method m, I define it separately
> let m x = something that calls x#v
> 
> This question is somewhat equivalent to: what is the memory consumption of a
> simple method
> method m = let _ = self#v in () ??

A better answer is that objects have a two-level structure.
They are represented by a record containing their fields, an object id
(use for comparison) and a pointer to a shared method table (share by
all objects generate from the same class).
So adding a method to a class does not change the memory consumption
for generated objects.

However, beware that the number of fields in an object is not only the
number of val in its class definition. Constructor arguments or
intermediate values generated by let statements are automatically
converted into implicit fields if they are used by methods or
initializers.

class c x y z =
  let t = y + z in
  object
    method x = x
    method t = t
  end
The above class has just two implicit fields, x and t.

Jacques Garrigue


  reply	other threads:[~2009-04-20  5:35 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-04-18 14:48 Guillaume Hennequin
2009-04-20  5:34 ` Jacques Garrigue [this message]
2009-04-20  6:14 ` [Caml-list] " Goswin von Brederlow

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=20090420.143440.83528006.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@yquem.inria.fr \
    --cc=gje.hennequin@gmail.com \
    /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).