Version française
Home     About     Download     Resources     Contact us    
Browse thread
Execution time of class versus record
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: skaller <skaller@u...>
Subject: Re: [Caml-list] Execution time of class versus record
On Sun, 2007-06-24 at 18:06 +0200, Arnaud Spiwack wrote:
> Which obviously raises the question: What is the motivation for the 
> object to be encoded differently than records? 

Someone with more expertise will assuredly correct me,
but this is my picture:

Ocaml objects can be accessed via a class type.

An object matches a type if it has the required methods,
matched by name and type (structural typing).

For a given class type, objects of different types might match, 
so there has to be a lookup table mapping the method of the call
to the method of the object.

Therefore, the indirection is unavoidable. But why can't
we use an array, indexed by the method number?

This would be possible. However consider a coercion
from a class type to a supertype: the supertype might
have less methods, or methods in a different order.
So the coercion would require dynamically building a new
method array.

On the other hand with a dictionary, the method lookup
is slower, but the same dictionary can be used for the 
supertype, since statically, you can only lookup a method
that is sure to be in the subtype .. the other methods
of the subtype simply won't be found.

The dictionary of an object is known statically at construction
time so it can be created once on program initialisation.

This means: construction and coercion are fast, the dictionary
is already built. Lookup is slower than optimal. And finally,
the dictionary method is easier for the compiler writers:
just store the dictionary in the object.

-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net