Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Clojure Polymorphism

You're reading from   Clojure Polymorphism Leverage Clojure's polymorphic tools to develop your applications

Arrow left icon
Product type Paperback
Published in Nov 2019
Publisher
ISBN-13 9781838982362
Length 56 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Paul Stadig Paul Stadig
Author Profile Icon Paul Stadig
Paul Stadig
Arrow right icon
View More author details
Toc

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...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime