<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2003/12/014c876011e3783dfeaee4df9a9dca72"
  from="Brian Hurt &lt;bhurt@s...&gt;"
  author="Brian Hurt"
  date="2003-12-07T17:22:59"
  subject="Re: [Caml-list] Object-oriented access bottleneck"
  prev="2003/12/53de1d81a54124f71e2d3471665fb012"
  next="2003/12/89629aaea615ed0841e41fd89b63c3e1"
  prev-in-thread="2003/12/2fcdfadf2d12989ce7318b8a57465953"
  next-in-thread="2003/12/874d67ec980f1675d71aae30479e5186"
  prev-thread="2003/12/ecaddc19e541a9809560b5db8b55c48d"
  next-thread="2003/12/53de1d81a54124f71e2d3471665fb012"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] Object-oriented access bottleneck">
<msg 
  url="2003/12/0405d2d0e46776c18d8187cc1e8db0ce"
  from="Nuutti Kotivuori &lt;naked+caml@n...&gt;"
  author="Nuutti Kotivuori"
  date="2003-12-07T02:39:16"
  subject="[Caml-list] Object-oriented access bottleneck">
<msg 
  url="2003/12/82447d9c9830bab626cd33d41137715f"
  from="Nicolas Cannasse &lt;warplayer@f...&gt;"
  author="Nicolas Cannasse"
  date="2003-12-07T02:59:23"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
<msg 
  url="2003/12/3bef043c3c8b1d024a522b7968c1655e"
  from="Benjamin Geer &lt;ben@s...&gt;"
  author="Benjamin Geer"
  date="2003-12-07T11:22:07"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
<msg 
  url="2003/12/068928e7f339a45342f573b34df00436"
  from="Nicolas Cannasse &lt;warplayer@f...&gt;"
  author="Nicolas Cannasse"
  date="2003-12-07T14:12:24"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
</msg>
</msg>
<msg 
  url="2003/12/89629aaea615ed0841e41fd89b63c3e1"
  from="Nuutti Kotivuori &lt;naked+caml@n...&gt;"
  author="Nuutti Kotivuori"
  date="2003-12-07T18:04:33"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
</msg>
</msg>
<msg 
  url="2003/12/11c4c5386a3bce4a54d51a2e9841dca0"
  from="Jacques Garrigue &lt;garrigue@k...&gt;"
  author="Jacques Garrigue"
  date="2003-12-07T10:26:19"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
<msg 
  url="2003/12/b9082d28c180dae2adcc644d330d3622"
  from="Nuutti Kotivuori &lt;naked+caml@n...&gt;"
  author="Nuutti Kotivuori"
  date="2003-12-07T19:46:25"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
<msg 
  url="2003/12/aea7690c5ae1dc3cf7f1ac93954dd44f"
  from="Jacques Garrigue &lt;garrigue@k...&gt;"
  author="Jacques Garrigue"
  date="2003-12-08T01:06:47"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
<msg 
  url="2003/12/cb58878528d6c6c4c8bc1f70e307263c"
  from="Nuutti Kotivuori &lt;naked+caml@n...&gt;"
  author="Nuutti Kotivuori"
  date="2003-12-08T15:08:38"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
<msg 
  url="2003/12/25c64c31d216a39f7c1a9b7fc10393dc"
  from="Richard Jones &lt;rich@a...&gt;"
  author="Richard Jones"
  date="2003-12-08T15:42:47"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
<msg 
  url="2003/12/24448eaff6f7e9447b75f2bb0fae05c2"
  from="Nicolas Cannasse &lt;warplayer@f...&gt;"
  author="Nicolas Cannasse"
  date="2003-12-09T00:27:02"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
<msg 
  url="2003/12/817e19365ada8715c95560fe9bc8c459"
  from="Nuutti Kotivuori &lt;naked+caml@n...&gt;"
  author="Nuutti Kotivuori"
  date="2003-12-09T12:10:12"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
<msg 
  url="2003/12/3e08ab91fe4e71aa79503c510e5bc9ce"
  from="Olivier Andrieu &lt;andrieu@i...&gt;"
  author="Olivier Andrieu"
  date="2003-12-09T13:17:42"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
<msg 
  url="2003/12/53ed62c2429ea4f8799420334d0ea457"
  from="Nuutti Kotivuori &lt;naked+caml@n...&gt;"
  author="Nuutti Kotivuori"
  date="2003-12-09T13:53:56"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
</msg>
</msg>
</msg>
</msg>
</msg>
</msg>
<msg 
  url="2003/12/70919ec4e1a6ec0e9e44915831c57798"
  from="Brian Hurt &lt;bhurt@s...&gt;"
  author="Brian Hurt"
  date="2003-12-08T16:51:11"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
<msg 
  url="2003/12/66f13ce473016d9835148ed914297636"
  from="brogoff@s..."
  author="brogoff@s..."
  date="2003-12-08T18:20:12"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
<msg 
  url="2003/12/4ed04b10c434786dfabeb1ac5b6a933d"
  from="Brian Hurt &lt;bhurt@s...&gt;"
  author="Brian Hurt"
  date="2003-12-08T19:09:21"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
</msg>
</msg>
<msg 
  url="2003/12/e9b284a0003439f67600366eea36ef34"
  from="Xavier Leroy &lt;xavier.leroy@i...&gt;"
  author="Xavier Leroy"
  date="2003-12-08T19:02:34"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
<msg 
  url="2003/12/83cb0be3da0d55eea55c4913d5dc051a"
  from="Brian Hurt &lt;bhurt@s...&gt;"
  author="Brian Hurt"
  date="2003-12-08T20:36:46"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
<msg 
  url="2003/12/30111f7048f7f046073a7036d8df76d4"
  from="Nuutti Kotivuori &lt;naked+caml@n...&gt;"
  author="Nuutti Kotivuori"
  date="2003-12-08T21:06:18"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
</msg>
<msg 
  url="2003/12/2fcdfadf2d12989ce7318b8a57465953"
  from="malc &lt;malc@p...&gt;"
  author="malc"
  date="2003-12-08T22:28:35"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
</msg>
</msg>
</msg>
</msg>
</msg>
</msg>
</msg>
<msg 
  url="2003/12/014c876011e3783dfeaee4df9a9dca72"
  from="Brian Hurt &lt;bhurt@s...&gt;"
  author="Brian Hurt"
  date="2003-12-07T17:22:59"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
<msg 
  url="2003/12/874d67ec980f1675d71aae30479e5186"
  from="Nuutti Kotivuori &lt;naked+caml@n...&gt;"
  author="Nuutti Kotivuori"
  date="2003-12-07T18:14:37"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
<msg 
  url="2003/12/81f82f284df0b1282309ba984d8511b3"
  from="Brian Hurt &lt;bhurt@s...&gt;"
  author="Brian Hurt"
  date="2003-12-07T18:30:19"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
<msg 
  url="2003/12/dc39ff38e190a026acbb9b4591886f68"
  from="Abdulaziz Ghuloum &lt;aghuloum@c...&gt;"
  author="Abdulaziz Ghuloum"
  date="2003-12-07T23:50:19"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
<msg 
  url="2003/12/d82586dd21630548daff76e5154d68f3"
  from="Brian Hurt &lt;bhurt@s...&gt;"
  author="Brian Hurt"
  date="2003-12-08T16:28:43"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
<msg 
  url="2003/12/b336119b9845b86570c36f5e200c22c7"
  from="Nuutti Kotivuori &lt;naked+caml@n...&gt;"
  author="Nuutti Kotivuori"
  date="2003-12-08T18:48:21"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
</msg>
</msg>
</msg>
<msg 
  url="2003/12/794dd4e65d8ed369f6a8bf00a7fc3953"
  from="Nuutti Kotivuori &lt;naked+caml@n...&gt;"
  author="Nuutti Kotivuori"
  date="2003-12-08T10:18:02"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
</msg>
<msg 
  url="2003/12/afbc265d71c1874f337802c94c8e954e"
  from="skaller &lt;skaller@o...&gt;"
  author="skaller"
  date="2003-12-08T20:51:58"
  subject="Re: [Caml-list] Object-oriented access bottleneck">
</msg>
</msg>
</msg>
</msg>
</msg>
</thread>

<contents>
On Sun, 7 Dec 2003, Nuutti Kotivuori wrote:

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

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

let f c = c#foo 3 ;;

In O'caml, this has type:
&lt; foo : int -&gt; 'a; .. &gt; -&gt; '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 &amp; vft_p-&gt;mask;

    while (vft_p-&gt;table[i].hashval != foo_hash) {
        i = (i + 1) &amp; vft_p-&gt;mask;
    }
    return vft_p-&gt;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

</contents>

</message>

