vtable的替代品 [英] Alternatives to vtable

查看:131
本文介绍了vtable的替代品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

您知道某些不使用vtables的语言实现吗?

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

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

Are there are free online pages which discuss the alternatives?

推荐答案

是的,有很多选择!

仅当两个条件成立时,Vtables才可用.

Vtables are only possible when two conditions hold.

  1. 所有方法调用都可以静态确定.如果您可以通过字符串名称来调用函数,或者您没有关于要在其上调用方法的对象的类型信息,则不能使用vtables,因为您不能将每个方法映射到某个表中的索引.同样,如果可以在运行时将函数添加到类中,则不能在vtable中为所有方法静态分配索引.
  2. 继承可以静态确定.如果您使用原型继承或无法静态告诉继承结构的其他继承方案,则无法预先计算每种方法的索引表中的内容或某个特定类的方法在插槽中的位置.
  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.

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

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

另一种选择是使用跟踪,在这种情况下,编译器会发出猜测对象类型是什么的代码,然后对要调用的方法进行硬编码. Mozilla Firefox在其JavaScript解释器中使用了此功能,因为无法为每个对象构建vtable.

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.

希望这会有所帮助!

这篇关于vtable的替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆