5 ms·
How is it zero-overhead? The visitor pattern at the very least requires dynamic dispatch, which isn't free.
by ginko 8y ago
How is it zero-overhead? The visitor pattern at the very least requires dynamic dispatch, which isn't free.
- andreareina 8y agoThe zero-overhead here seems to refer to not constructing intermediate representations when chaining visitors.
- mping 8y agoThe visitor pattern is an alternative to dynamic dispatch. Normally instead of using dynamic dispatch you inverse caller and callee roles and pass 'this'. The article uses a variation of the classic visitor pattern.
- saurik 8y agoThe visitor pattern uses dynamic dispatch on the type of the visitor.
- jmartinpetersen 8y agoDid you mix up dynamic dispatch with double dispatch in your comment?
- IloveHN84 8y agoIn JVM languages maybe, in C++ with the CRTP there's zero overhead for sure
- frankzinger 8y agoWhy the CRTP? Plenty of zero-overhead Visitors have been implemented with regular templates. I don't see the connection.
- humanrebar 8y agoIt's moot either way. There is dynamic dispatch based on use provided inputs. There will always be lookups, jumps, and cache misses regardless of the syntax or semantics.
- frankzinger 8y agoIs it moot? With templates, the Visitor type is selected at compile time and needn't even be polymorphic. The member function call(s) can often be inlined. Pretty sure there is no dynamic dispatch or jumps in such a scenario. Cache misses, sure, but I don't think that's important in this case.
- humanrebar 8y agoAlmost always these systems operate on external inputs like source files or JSON blobs. You cannot compile time collapse the branching needed to parse that sort of thing.
- xxs 8y agoPolymorhic call sites can hardly be called 'zero overhead' - they remove any chance for inlining to begin with. So no, JVM is not amongst them
- KirinDave 8y agoSince you're on the hook for a polymorphic call anyways in a rewrite of a runtime runtime structure, I think this is not the "overhead" that anyone was talking about.
- Yoric 8y agoNot in all languages. You can easily implement a visitor pattern with purely static dispatch in, say, Rust or C++. I suspect that any JIT that performs sufficient specialization/inlining will also achieve the same result once warm.
- KirinDave 8y agoIt isn't overhead to have a dynamic dispatch; you're traversing a runtime structure with variable shape. I can always construct a Map that will force you into a the case of dynamic dispatch in JSON. The only way we could remove that overhead is by fully fixing the shape of the tree and all maps at compile time (which has its uses, but in a different context). The fact that you can fuse multiple passes into one alteration (or reconstruction) of the tree is what's a removal of overhead.