英文:
Structural typing implementation of OCaml, Scala, and Go
问题
在研究结构类型时,我发现了以下帖子,描述了Go中接口如何在运行时转换为方法查找表。该帖子中描述的过程似乎与Scala的可选结构类型系统和Java语言的White Oak扩展中描述的反射和生成技术大不相同。
是否有任何深入的资源讨论了OCaml中结构类型的实现方式?我特别感兴趣的是任何关于优化或运行时性能与名义类型系统的比较的讨论。
英文:
While researching about structural typing I found the following post describing how interfaces in Go are translated to method lookup tables at runtime. The process described in the post seems vastly different than the reflective and generative techniques described for Scala's optional structural type system and for the White Oak extension of the Java language.
Are there any in-depth resources that discuss how structural typing is implemented in OCaml? I am particularly interested in any discussion of optimizations or run-time performance comparisons with nominal type systems.
答案1
得分: 8
你可以在Jake Donham的这篇博客文章中找到OCaml对象内部的相当详细的描述。其要点是对象支持主要是作为一个内部库实现的,编译器本身只有一小部分逻辑(当然还有类型系统中的对象类型逻辑),主要涉及高效的消息分发。
我对语言的这部分不是很了解,但经过初步检查,看起来OCaml依赖于在排序的方法类型中进行方法查找(解析为方法表中的槽位),并对最后调用的方法进行缓存,并优化静态已知的调用,特别是方法实现内部的self调用。最后,一些常用的函数(例如实例变量的获取器和设置器)被识别并特别编码(在内部OO库中的type impl),以提高性能,可能更重要的是减少代码大小。
英文:
You can find a rather detailed description of OCaml object internals in this blog post by Jake Donham. The gist of it is that object support is mostly implemented as an internal library, with only a bit of logic in the compiler itself (and of course object typing logic in the type system), mostly around efficient message dispatch.
I'm not an expert on this part of the language, but after a cursory inspection, it looks like OCaml relies on method lookups in a sorted method type (resolved to slots in the method table), with caching for the method called last, and optimization of statically known calls, in particular self calls inside method implementations. Finally, some commonly used functions (for example instance variable getters and setters) are recognized and encoded specifically (type impl in the internal OO library), to improve performances and, probably more importantly, reduce code size.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论