vtable 的替代方案

发布于 2024-11-29 07:01:21 字数 142 浏览 6 评论 0原文

Vtables 在大多数 OO 实现中无处不在,但是它们有替代方案吗? vtables 的 wiki 页面有一个简短的简介,但实际上并没有太多信息(和存根链接)。

你知道一些不使用虚函数表的语言实现吗?

是否有讨论替代方案的免费在线页面?

Vtables are ubiquitous in most OO implementations, but do they have alternatives? The wiki page for vtables has a short blurb, but not really to much info (and stubbed links).

Do you know of some language implementation which does not use vtables?

Are there are free online pages which discuss the alternatives?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弃爱 2024-12-06 07:01:21

是的,有很多选择!

仅当满足两个条件时,虚拟表才有可能。

  1. 所有方法调用都可以静态确定。如果您可以通过字符串名称调用函数,或者如果您没有关于正在调用方法的对象的类型信息,则不能使用 vtable,因为您不能t 将每个方法映射到某个表中的索引。同样,如果您可以在运行时向类添加函数,则无法为所有方法静态分配 vtable 中的索引。
  2. 继承可以静态确定。如果您使用原型继承或其他无法静态判断继承结构的继承方案,则无法预先计算表中每个方法的索引或者什么特定类的方法放在一个槽中。

通常,继承是通过使用基于字符串的表将函数名称映射到其实现以及允许每个类查找其基类的指针来实现的。然后通过遍历该结构寻找实现该方法的接收者对象的类或之上的最低类来实现方法调度。为了加快执行速度,经常使用内联缓存等技术,其中调用站点根据对象的类型存储应调用哪个方法的猜测,以避免花费时间遍历整个结构。 Self 编程语言使用了这个想法,然后将其合并到 HotSpot JVM 中来处理接口(标准继承仍然使用 vtable)。

另一种选择是使用跟踪,其中编译器发出代码来猜测对象的类型,然后将要调用的方法硬编码到跟踪中。 Mozilla Firefox 在其 JavaScript 解释器中使用它,因为没有办法为每个对象构建 vtable。

我刚刚教授完编译器课程,我的其中一堂讲座是关于各种编程语言中对象的实现以及相关的权衡。如果您愿意,可以在此处查看幻灯片

希望这有帮助!

Yes, there are many alternatives!

Vtables are only possible when two conditions hold.

  1. All method calls can be determined statically. If you can call functions by string name, or if you have no type information about what objects you are calling methods on, you can't use vtables because you can't map each method to the index in some table. Similarly, if you can add functions to a class at runtime, you can't assign all methods an index in the vtable statically.
  2. Inheritance can be determined statically. If you use prototypal inheritance, or another inheritance scheme where you can't tell statically what the inheritance structure looks like, you can't precompute the index of each method in the table or what particular class's method goes in a slot.

Commonly, inheritance is implemented by having a string-based table mapping names of functions to their implementations, along with pointers allowing each class to look up its base class. Method dispatch is then implemented by walking this structure looking for the lowest class at or above the class of the receiver object that implements the method. To speed up execution, techniques like inline caching are often used, where call sites store a guess of which method should be invoked based on the type of the object to avoid spending time traversing this whole structure. The Self programming language used this idea, which was then incorporates into the HotSpot JVM to handle interfaces (standard inheritance still uses vtables).

Another option is to use tracing, where the compiler emits code that guesses what the type of the object is and then hardcodes the method to call into the trace. Mozilla Firefox uses this in its JavaScript interpreter, since there isn't a way to build vtables for every object.

I just finished teaching a compilers course and one of my lectures was on implementations of objects in various programming languages and the associated tradeoffs. If you'd like, you can check out the slides here.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文