Concatenative topics
Concatenative meta
Other languages
Meta
Efficient techniques to handle generic methods, aka polymorphic calls, aka dynamic binding.
First, we can conceptually divide this operation into three primitive operations:
This a simple way to generalize single dispatch, subjective dispatch, multiple dispatch and predicate dispatch.
Examples:
[ drop drop class ]
[ [ class ] tri@ ]
[ access get ]
[ 0 > ]
The heart of the dispatch. In essence, this can be conceptualized as lookup in an associative map from signatures to words. In practice, the input domain of signatures can be very large (ex: multiple dispatch, 1000 classes, 3 arguments: 1 billion signatures). However, we can take advantage of the fact that the important mappings (for performance) are a very small subset of the input domain.
note: dispatch is deterministic: As long as there are no reflective changes to the system, the same signature will always map to the same word.
For correctness, we have to handle any input signature. For that purpose, we maintain a procedural dispatch function. It doesn't need to be fast: It might traverse deep hierarchies, do linear searches, even backtrack. In essence all other forms of dispatch are more or less elaborates memoizations of this function.
One of the fastest ways to dispatch calls that are not really polymorphic at runtime. The 'cache' in an inline cache is the caller's call instruction: By redirecting (using self-modifying code) the call to different stubs, we can effectively maintain state in the caller. A given stub ensures that the current signature is identical to the cached one, and then calls the word mapped by this signature. If there is a mismatch, it falls back on base dispatch.
IC stubs can be shared between all call sites. IC stubs can easily be produced from a template (thus there is no intrinsic need for an online compiler). There is no point in producing as many IC stubs as there are potential targets (waste of time and space).
IC can be implemented by a simple set of primitives:
word = iptoword((void(returnaddress(depth) -4)))
(void(returnaddress(depth) -4))) = wordtoip(word)
optimization: The IC stub is a natural target for customization, by inlining the call at the end of the stub (and propagating the signature information for further optimizations).
This is trivial. A few things worth mentioning:
This revision created on Wed, 22 Oct 2008 04:32:33 by prunedtree