caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Brian Hurt <bhurt@spnz.org>
To: Nuutti Kotivuori <naked+caml@naked.iki.fi>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Object-oriented access bottleneck
Date: Sun, 7 Dec 2003 12:23:20 -0600 (CST)	[thread overview]
Message-ID: <Pine.LNX.4.44.0312071153380.5009-100000@localhost.localdomain> (raw)
In-Reply-To: <871xrhe4hb.fsf@iki.fi>

On Sun, 7 Dec 2003, Nuutti Kotivuori wrote:

> This message identical to the post I made to comp.lang.ml a while back
> - but I think it will have a better audience over here, now that I
> decided to join the list.
> 
> ---
> 
> Lately I've been having a bit of a dilemma caused by a bottleneck from
> object-oriented access in Ocaml. The problem derives from the
> implementation of method calls through lazy binding.
> 
> Compiled languages which offer an object oriented system usually
> provide a way for methods to short-circuit the lazy binding (or
> virtual function table) system. In C++, non-virtual functions do this,
> in Java, declaring a method final gives the compiler a hint about
> this. In this case, the compiler can inline the method into the
> caller.

I don't think there can be.  Consider the function:

let f c = c#foo 3 ;;

In O'caml, this has type:
< foo : int -> 'a; .. > -> 'a
which basically means it accepts any object with a foo member function.

So what happens if we define two classes, which don't relate to each other 
except each has a foo member function.  Only in one class foo is a virtual 
function, and in the second class foo is a non-virtual function.  How 
would you implement f in this case?

A better alternative would be, I think, to spend a little time optimizing 
virtual function calls, so that they are faster.

I like the idea of implementing virtual function tables as hash tables.
Require that every VFT is a power of two elements in size, and you could 
implement f above in C something like:

typedef unsigned long word_t;

typedef struct {
    word_t mask;
    struct {
        word_t hashval;
        word_t (*fun)();
    } table[1];
} vft_t;

word_t f(word_t c) {
    word_t * c_p = (word_t *) c; /* C is a pointer to an object */
    vft_t * vft_p = (word_t *) (c_p[-1]); /* VFT is at offset -1 */
    static word_t foo_hash = hash_value("foo"); /* never changes */
    word_t i = foo_hash & vft_p->mask;

    while (vft_p->table[i].hashval != foo_hash) {
        i = (i + 1) & vft_p->mask;
    }
    return vft_p->table[i].fun((word_t) 3);
}

Note that the type checker gaurentees the while loop exits- better than
half the time the while loop body won't execute at all.  The most likely
cost then is two loads, one branch, and one indirect call.  Biggest cost 
is likely to be the cache misses on the loads, followed by mispredicted 
branchs.  Virtual function calls won't be as fast as normal function 
calls, but they'll be close enough to stop worrying about them.

-- 
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
                                - Gene Spafford 
Brian

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  parent reply	other threads:[~2003-12-07 17:22 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-12-07  2:39 Nuutti Kotivuori
2003-12-07  2:59 ` Nicolas Cannasse
2003-12-07 11:22   ` Benjamin Geer
2003-12-07 14:12     ` Nicolas Cannasse
2003-12-07 18:04   ` Nuutti Kotivuori
2003-12-07 10:27 ` Jacques Garrigue
2003-12-07 19:46   ` Nuutti Kotivuori
2003-12-08  1:07     ` Jacques Garrigue
2003-12-08 15:08       ` Nuutti Kotivuori
2003-12-08 15:42         ` Richard Jones
2003-12-09  0:26           ` Nicolas Cannasse
2003-12-09 12:10             ` Nuutti Kotivuori
2003-12-09 13:17               ` Olivier Andrieu
2003-12-09 13:53                 ` Nuutti Kotivuori
2003-12-08 17:51       ` Brian Hurt
2003-12-08 18:19         ` brogoff
2003-12-08 20:09           ` Brian Hurt
2003-12-08 19:02         ` Xavier Leroy
2003-12-08 21:37           ` Brian Hurt
2003-12-08 21:06             ` Nuutti Kotivuori
2003-12-08 22:30             ` malc
2003-12-07 18:23 ` Brian Hurt [this message]
2003-12-07 18:14   ` Nuutti Kotivuori
2003-12-07 19:30     ` Brian Hurt
2003-12-07 23:50       ` Abdulaziz Ghuloum
2003-12-08 17:29         ` Brian Hurt
2003-12-08 18:48           ` Nuutti Kotivuori
2003-12-08 10:17       ` Nuutti Kotivuori
2003-12-08 19:51       ` skaller

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=Pine.LNX.4.44.0312071153380.5009-100000@localhost.localdomain \
    --to=bhurt@spnz.org \
    --cc=caml-list@inria.fr \
    --cc=naked+caml@naked.iki.fi \
    /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).