In object-oriented languages, method visibility modifiers hold a key role in separating internal methods from the public API. Protected visibility modifiers offer a way to hide methods from external objects while authorizing internal use and overriding in subclasses. While present in main statically-typed languages, visibility modifiers are not as common or mature in dynamically-typed languages. In this article, we present ProtDyn, a self-send-based visibility model calculated at compile time for dynamically-typed languages relying on name-mangling and syntactic differentiation of self vs non self sends. We present #Pharo, a ProtDyn implementation of this model that is backwards compatible with existing programs, and its port to Python. Using these implementations we study the performance impact of ProtDyn on the method lookup, in the presence of global lookup caches and polymorphic inline caches. We show that our name mangling and double method registration technique has a very low impact on performance and keeps the benefits from the global lookup cache and polymorphic inline cache. We also show that the memory overhead on a real use case is between 2% and 13% in the worst-case scenario. Protected modifier semantics enforces encapsulation such as private but allow developers to still extend the class in subclasses. ProtDyn offers a VM-agnostic and backwards-compatible design to introduce protected semantics in dynamically-typed languages.
翻译:在面向对象语言中,方法可见性修饰符在区分内部方法与公共API方面发挥着关键作用。受保护可见性修饰符提供了一种将方法对外部对象隐藏,同时授权内部使用和子类覆写的机制。虽然这一特性广泛存在于主流静态类型语言中,但在动态类型语言中,可见性修饰符并未得到同样普遍或成熟的应用。本文提出了ProtDyn——一种基于自身发送的可见性模型,该模型在编译时通过名称修饰与自身/非自身发送的语法区分,为动态类型语言设计实现。我们展示了#Pharo(该模型的一种与现有程序向后兼容的实现)及其到Python的移植。基于这些实现,我们研究了在全局查找缓存和多态内联缓存存在的情况下,ProtDyn对方法查找性能的影响。实验表明,我们的名称修饰与双方法注册技术对性能影响极低,且能保留全局查找缓存与多态内联缓存的性能优势。此外,在最坏情况下,实际应用场景的内存开销介于2%至13%之间。受保护修饰符的语义在保持私有性封装约束的同时,允许开发者通过子类扩展功能。ProtDyn提供了一种虚拟机无关且向后兼容的设计方案,为动态类型语言引入受保护语义。