caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* mixed functional / object style
@ 2009-04-18 14:48 Guillaume Hennequin
  2009-04-20  5:34 ` [Caml-list] " Jacques Garrigue
  2009-04-20  6:14 ` Goswin von Brederlow
  0 siblings, 2 replies; 3+ messages in thread
From: Guillaume Hennequin @ 2009-04-18 14:48 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 684 bytes --]

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 () ??

Thanks a lot
Guillaume.

[-- Attachment #2: Type: text/html, Size: 786 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] mixed functional / object style
  2009-04-18 14:48 mixed functional / object style Guillaume Hennequin
@ 2009-04-20  5:34 ` Jacques Garrigue
  2009-04-20  6:14 ` Goswin von Brederlow
  1 sibling, 0 replies; 3+ messages in thread
From: Jacques Garrigue @ 2009-04-20  5:34 UTC (permalink / raw)
  To: gje.hennequin; +Cc: caml-list

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] mixed functional / object style
  2009-04-18 14:48 mixed functional / object style Guillaume Hennequin
  2009-04-20  5:34 ` [Caml-list] " Jacques Garrigue
@ 2009-04-20  6:14 ` Goswin von Brederlow
  1 sibling, 0 replies; 3+ messages in thread
From: Goswin von Brederlow @ 2009-04-20  6:14 UTC (permalink / raw)
  To: Guillaume Hennequin; +Cc: caml-list

Guillaume Hennequin <gje.hennequin@gmail.com> writes:

> 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 () ??
> Thanks a lot
> Guillaume.

A class is a reference to the virtual table of a class and its
instance variables. There is exactly one virtual table for every class
you declrare and all instances of that class share the same virtual
table. All the methods are listed in that one virtual table.

So a method will cost you 4 or 8 byte (or maybe more, don't know how
big the entry for a method is) but it will be something small and
const no matter how many instances you have.

MfG
        Goswin


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-04-20  6:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-04-18 14:48 mixed functional / object style Guillaume Hennequin
2009-04-20  5:34 ` [Caml-list] " Jacques Garrigue
2009-04-20  6:14 ` Goswin von Brederlow

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).