On Tue, Dec 1, 2009 at 2:59 PM, Krukow <karl.kru...@gmail.com> wrote: > > > On Dec 1, 2:42 am, Rich Hickey <richhic...@gmail.com> wrote: >> I have done a lot of work on performance, and refined the design. The >> big news is that you can now directly implement a protocol inside a >> deftype, and you can also reify protocols. This cements protocols as >> the superior way to model the things for which they are suitable, >> since they can match the performance of interfaces without their >> limitations. > > First of all, I think this is a wonderful addition to the language. > I've tried what is in the "new" branch on a small but real example, > and I am quite happy with it: So thanks! >
You're welcome. > Could you go into more detail about how protocols and datatypes are > actually implemented, and the performance improvements you've recently > made? (probably I'm not the only one interested :-) > > What is generated when I define a protocol, datatype and extend the > type to the protocol? > A protocol is a data structure that contains a set of signatures, and a set of implementations, which are maps of fn-name to fn, supplied by explicit extenders. A protocol also generates a corresponding interface, and protocol fns special-case that interface. There are 2 ways to make a deftype reach a protocol. First, you can implement the protocol directly in the deftype/reify, supplying the protocol where you do interfaces, and the methods of the protocol as methods of the type. The type will be made to implement the protocol's interface. The second way, for types you don't control, is to use extend-type/class/protocol, which will create method maps and register them with the protocol. Note that these do not differ in their support for dynamic use (i.e. neither requires AOT), just that the former is type-intrusive (you have to 'own' the type you are modifying) while the latter is not. > As I understand the performance of calling a protocol function matches > the performance of calling an interface method in Java. How is it > possible to achieve this in combination with the dynamic extensibility > of extend? > Different methods of implementing the protocol have different performance. Implementing directly in deftype or reify is as fast as a direct interface call. Using extend-* is not quite as fast, but still fast. Both methods have direct support in callsites, so a call to a protocol fn has support both for using the interface and caching lookup results. The most important thing is, writing to protocols gives you a dynamic, open, extensible system not tied to derivation, and is fast, so a great way to architect the polymorphic part of your designs (when single-dispatch is appropriate). Rich -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en