天天看点

Dynamic dispatch mechanisms

normally, in a typed language, the dispatch mechanism will be performed based on the type of the arguments (most commonly based on the type of the receiver of a message). this might be dubbed 'per-type dynamic dispatch'. languages with weak or no typing systems often carry a dispatch table as part of the object data for each object. this allows instance behaviour as each instance may map a given message to a separate method.

动态分发:选择哪个对象来执行。

in computer science, dynamic dispatch is the process of selecting which implementation of a polymorphic operation (method or function) to call at run time. it is commonly employed in, and considered a prime characteristic of, object-oriented programming (oop) languages and systems.[1]

object-oriented systems model a problem as a set of interacting objects that enact operations referred to by name. polymorphism is the phenomenon wherein somewhat interchangeable objects each expose an operation of the same name but possibly differing in behavior. as an example, a file object and a database object both have a storerecord method that can be used to write a personnel record to storage. their implementations differ. a program holds a reference to an object which may be either a file object or a database object. which it is may have been determined by a run-time setting, and at this stage, the program may not know or care which. when the program calls storerecord on the object, something needs to decide which behavior gets enacted. if one thinks of oop as sending messages to objects, then in this example the program sends a storerecord message to an object of unknown type, leaving it to the run-time support system to dispatch the message to the right object. the object enacts whichever behavior it implements.[2]

a language may be implemented with different dynamic dispatch mechanisms. the choices of the dynamic dispatch mechanism offered by a language to a large extent alter the programming paradigms that are available or are most natural to use within a given language.

some languages offer a hybrid approach.

dynamic dispatch will always incur an overhead so some languages offer static dispatch for particular methods.

c++ uses early binding and offers both dynamic and static dispatch. the default form of dispatch is static. to get dynamic dispatch the programmer must declare a method as virtual.

c++ compilers typically implement dynamic dispatch with a data structure called a virtual table (vtable) that defines the message-to-method mapping for a given class (c++ as such has no notion of a vtable). instances of that type will then store a pointer to this table as part of their instance data. this is complicated when multiple inheritance is used. since c++ does not support late binding, the virtual table in a c++ object cannot be modified at run-time, which limits the potential set of dispatch targets to a finite set chosen at compile time.

type overloading does not produce dynamic dispatch in c++ as the language considers the types of the message parameters part of the formal message name. this means that the message name the programmer sees is not the formal name used for binding.

in go and rust, a more versatile variation of early binding is used. vtable pointers are carried with object references as 'fat pointers' ('interfaces' in go, or 'trait objects' in rust).

this decouples the supported interfaces from the underlying data structures. each compiled library needn't know the full range of interfaces supported in order to correctly use a type, just the specific vtable layout that they require. code can pass around different interfaces to the same piece of data to different functions. this versatility comes at the expense of extra data with each object reference, which is problematic if many such references are stored persistently.

smalltalk uses a type-based message dispatcher. each instance has a single type whose definition contains the methods. when an instance receives a message, the dispatcher looks up the corresponding method in the message-to-method map for the type and then invokes the method.

because a type can have a chain of base types, this look-up can be expensive. a naive implementation of smalltalk's mechanism would seem to have a significantly higher overhead than that of c++ and this overhead would be incurred for each and every message that an object receives.

real smalltalk implementations often use a technique known as inline caching[3] that makes method dispatch very fast. inline caching basically stores the previous destination method address and object class of the call site (or multiple pairs for multi-way caching). the cached method is initialized with the most common target method (or just the cache miss handler), based on the method selector. when the method call site is reached during execution, it just calls the address in the cache. (in a dynamic code generator, this call is a direct call as the direct address is back patched by cache miss logic.) prologue code in the called method then compares the cached class with the actual object class, and if they don't match, execution branches to a cache miss handler to find the correct method in the class. a fast implementation may have multiple cache entries and it often only takes a couple of instructions to get execution to the correct method on an initial cache miss. the common case will be a cached class match, and execution will just continue in the method.

out-of-line caching can also be used in the method invocation logic, using the object class and method selector. in one design, the class and method selector are hashed, and used as an index into a method dispatch cache table.

as smalltalk is a reflective language, many implementations allow mutating individual objects into objects with dynamically generated method lookup tables. this allows altering object behavior on a per object basis. a whole category of languages known as prototype based languages has grown from this, the most famous of which are self and javascript. careful design of the method dispatch caching allows even prototype based languages to have high performance method dispatch.

many other dynamically typed languages, including python, ruby, objective-c and groovy use similar approaches.

https://en.wikipedia.org/wiki/dynamic_dispatch

------------------越是喧嚣的世界,越需要宁静的思考------------------

合抱之木,生于毫末;九层之台,起于垒土;千里之行,始于足下。

积土成山,风雨兴焉;积水成渊,蛟龙生焉;积善成德,而神明自得,圣心备焉。故不积跬步,无以至千里;不积小流,无以成江海。骐骥一跃,不能十步;驽马十驾,功在不舍。锲而舍之,朽木不折;锲而不舍,金石可镂。蚓无爪牙之利,筋骨之强,上食埃土,下饮黄泉,用心一也。蟹六跪而二螯,非蛇鳝之穴无可寄托者,用心躁也。