Performance Gotcha
The performance of Clojure protocols is a bit nuanced. A Clojure protocol generates a Java interface, a protocol, and some protocol functions. The protocol functions will dispatch through the Java interface if the type of the first argument implements the interface; otherwise, it will dispatch through the protocol's dispatch table.
There are two ways to implement a protocol in Clojure:
1Â (defrecord S3Storage
2Â Â Â Â Â [access-key secret-key]
3Â Â Â proto/IStorage
4Â Â Â ...)
Or:
1Â (defrecord S3Storage
2Â Â Â Â Â [access-key secret-key])
3
4Â (extend-protocol IStorage
5Â Â Â S3Storage
6Â Â Â ...)
Conceptually, these seem as though they are equivalent, but they are not. The first example, extending the protocol directly in the defrecord form, will be around 20 times faster! When you extend a protocol inside of a defrecord, the generated class will implement the protocol's...