Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Clojure High Performance Programming, Second Edition
Clojure High Performance Programming, Second Edition

Clojure High Performance Programming, Second Edition: Become an expert at writing fast and high performant code in Clojure 1.7.0 , Second Edition

eBook
R$80 R$173.99
Paperback
R$217.99
Subscription
Free Trial
Renews at R$50p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Clojure High Performance Programming, Second Edition

Chapter 2. Clojure Abstractions

Clojure has four founding ideas. Firstly, it was set up to be a functional language. It is not pure (as in purely functional), but emphasizes immutability. Secondly, it is a dialect of Lisp; Clojure is malleable enough that users can extend the language without waiting for the language implementers to add new features and constructs. Thirdly, it was built to leverage concurrency for the new generation challenges. Lastly, it was designed to be a hosted language. As of today, Clojure implementations exist for the JVM, CLR, JavaScript, Python, Ruby, and Scheme. Clojure blends seamlessly with its host language.

Clojure is rich in abstractions. Though the syntax itself is very minimal, the abstractions are finely grained, mostly composable, and designed to tackle a wide variety of concerns in the least complicated way. In this chapter, we will discuss the following topics:

  • Performance characteristics of non-numeric scalars
  • Immutability and epochal time...

Non-numeric scalars and interning

Strings and characters in Clojure are the same as in Java. The string literals are implicitly interned. Interning is a way of storing only the unique values in the heap and sharing the reference everywhere it is required. Depending on the JVM vendor and the version of Java you use, the interned data may be stored in a string pool, Permgen, ordinary heap, or some special area in the heap marked for interned data. Interned data is subject to garbage collection when not in use, just like ordinary objects. Take a look at the following code:

user=> (identical? "foo" "foo")  ; literals are automatically interned
true
user=> (identical? (String. "foo") (String. "foo"))  ; created string is not interned
false
user=> (identical? (.intern (String. "foo")) (.intern (String. "foo")))
true
user=> (identical? (str "f" "oo") (str "f" "oo"))  ; str creates string...

Identity, value, and epochal time model

One of the principal virtues of Clojure is its simple design that results in malleable, beautiful composability. Using symbols in place of pointers is a programming practice that has existed for several decades now. It has found widespread adoption in several imperative languages. Clojure dissects that notion in order to uncover the core concerns that need to be addressed. The following subsections illustrate this aspect of Clojure.

We program using logical entities to represent values. For example, a value of 30 means nothing unless it is associated with a logical entity, let's say age. The logical entity age is the identity here. Now, even though age represents a value, the value may change with time; this brings us to the notion of state, which represents the value of the identity at a certain time. Hence, state is a function of time and is causally related to what we do in the program. Clojure's power lies in binding an identity with...

Persistent data structures

As we've noticed in the previous section, Clojure's data structures are not only immutable, but can produce new values without impacting the old version. Operations produce these new values in such a way that old values remain accessible; the new version is produced in compliance with the complexity guarantees of that data structure, and both the old and new versions continue to meet the complexity guarantees. The operations can be recursively applied and can still meet the complexity guarantees. Such immutable data structures as the ones provided by Clojure are called persistent data structures. They are "persistent", as in, when a new version is created, both the old and new versions "persist" in terms of both the value and complexity guarantee. They have nothing to do with storage or durability of data. Making changes to the old version doesn't impede working with the new version and vice versa. Both versions persist in a...

Sequences and laziness

 

"A seq is like a logical cursor."

 
 --Rich Hickey

Sequences (commonly known as seqs) are a way to sequentially consume a succession of data. As with iterators, they let a user begin consuming elements from the head and proceed realizing one element after another. However, unlike iterators, sequences are immutable. Also, since sequences are only a view of the underlying data, they do not modify the storage structure of the data.

What makes sequences stand apart is they are not data structures per se; rather, they are a data abstraction over a stream of data. The data may be produced by an algorithm or a data source connected to an I/O operation. For example, the resultset-seq function accepts a java.sql.ResultSet JDBC instance as an argument and produces lazily realized rows of data as seq.

Clojure data structures can be turned into sequences using the seq function. For example, (seq [:a :b :c :d]) returns a sequence. Calling seq over an empty...

Transducers

Clojure 1.7 introduced a new abstraction called transducers for "composable algorithmic transformations", commonly used to apply a series of transformations over collections. The idea of transducers follows from the reducing function, which accepts arguments of the form (result, input) and returns result. A reducing function is what we typically use with reduce. A transducer accepts a reducing function, wraps/composes over its functionality to provide something extra, and returns another reducing function.

The functions in clojure.core that deal with collections have acquired an arity-1 variant, which returns a transducer, namely map, cat, mapcat, filter, remove, take, take-while, take-nth, drop, drop-while, replace, partition-by, partition-all, keep, keep-indexed, dedupe and random-sample.

Consider the following few examples, all of which do the same thing:

user=> (reduce ((filter odd?) +) [1 2 3 4 5])
9
user=> (transduce (filter odd?) + [1 2 3 4 5])
9
user=&gt...

Non-numeric scalars and interning


Strings and characters in Clojure are the same as in Java. The string literals are implicitly interned. Interning is a way of storing only the unique values in the heap and sharing the reference everywhere it is required. Depending on the JVM vendor and the version of Java you use, the interned data may be stored in a string pool, Permgen, ordinary heap, or some special area in the heap marked for interned data. Interned data is subject to garbage collection when not in use, just like ordinary objects. Take a look at the following code:

user=> (identical? "foo" "foo")  ; literals are automatically interned
true
user=> (identical? (String. "foo") (String. "foo"))  ; created string is not interned
false
user=> (identical? (.intern (String. "foo")) (.intern (String. "foo")))
true
user=> (identical? (str "f" "oo") (str "f" "oo"))  ; str creates string
false
user=> (identical? (str "foo") (str "foo"))  ; str does not create string for 1 arg
true...

Identity, value, and epochal time model


One of the principal virtues of Clojure is its simple design that results in malleable, beautiful composability. Using symbols in place of pointers is a programming practice that has existed for several decades now. It has found widespread adoption in several imperative languages. Clojure dissects that notion in order to uncover the core concerns that need to be addressed. The following subsections illustrate this aspect of Clojure.

We program using logical entities to represent values. For example, a value of 30 means nothing unless it is associated with a logical entity, let's say age. The logical entity age is the identity here. Now, even though age represents a value, the value may change with time; this brings us to the notion of state, which represents the value of the identity at a certain time. Hence, state is a function of time and is causally related to what we do in the program. Clojure's power lies in binding an identity with its value that...

Persistent data structures


As we've noticed in the previous section, Clojure's data structures are not only immutable, but can produce new values without impacting the old version. Operations produce these new values in such a way that old values remain accessible; the new version is produced in compliance with the complexity guarantees of that data structure, and both the old and new versions continue to meet the complexity guarantees. The operations can be recursively applied and can still meet the complexity guarantees. Such immutable data structures as the ones provided by Clojure are called persistent data structures. They are "persistent", as in, when a new version is created, both the old and new versions "persist" in terms of both the value and complexity guarantee. They have nothing to do with storage or durability of data. Making changes to the old version doesn't impede working with the new version and vice versa. Both versions persist in a similar way.

Among the publications that...

Left arrow icon Right arrow icon

Description

Clojure treats code as data and has a macro system. It focuses on programming with immutable values and explicit progression-of-time constructs, which are intended to facilitate the development of more robust programs, particularly multithreaded ones. It is built with performance, pragmatism, and simplicity in mind. Like most general purpose languages, various Clojure features have different performance characteristics that one should know in order to write high performance code. This book shows you how to evaluate the performance implications of various Clojure abstractions, discover their underpinnings, and apply the right approach for optimum performance in real-world programs. It starts by helping you classify various use cases and the need for them with respect to performance and analysis of various performance aspects. You will also learn the performance vocabulary that experts use throughout the world and discover various Clojure data structures, abstractions, and their performance characteristics. Further, the book will guide you through enhancing performance by using Java interoperability and JVM-specific features from Clojure. It also highlights the importance of using the right concurrent data structure and Java concurrency abstractions. This book also sheds light on performance metrics for measuring, how to measure, and how to visualize and monitor the collected data. At the end of the book, you will learn to run a performance profiler, identify bottlenecks, tune performance, and refactor code to get a better performance.

Who is this book for?

This book is intended for intermediate Clojure developers who are looking to get a good grip on achieving optimum performance. Having a basic knowledge of Java would be helpful.

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Sep 29, 2015
Length: 198 pages
Edition : 2nd
Language : English
ISBN-13 : 9781785287671
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Sep 29, 2015
Length: 198 pages
Edition : 2nd
Language : English
ISBN-13 : 9781785287671
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
R$50 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
R$500 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just R$25 each
Feature tick icon Exclusive print discounts
R$800 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just R$25 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total R$ 797.97
Clojure for Data Science
R$306.99
Clojure High Performance Programming, Second Edition
R$217.99
Clojure Reactive Programming
R$272.99
Total R$ 797.97 Stars icon

Table of Contents

9 Chapters
1. Performance by Design Chevron down icon Chevron up icon
2. Clojure Abstractions Chevron down icon Chevron up icon
3. Leaning on Java Chevron down icon Chevron up icon
4. Host Performance Chevron down icon Chevron up icon
5. Concurrency Chevron down icon Chevron up icon
6. Measuring Performance Chevron down icon Chevron up icon
7. Performance Optimization Chevron down icon Chevron up icon
8. Application Performance Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.4
(5 Ratings)
5 star 60%
4 star 20%
3 star 20%
2 star 0%
1 star 0%
Aaron B. Iba Nov 29, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I've been programming in Clojure for 5 years and recently took on a very performance-critical project. I found this book to be extremely helpful and practical for writing high-performance Clojure code. As other reviewers have noted, the book covers a lot of different topics, but I just skipped the ones I was already familiar with and studied the stuff that was new to me -- and there was a lot of great new stuff. Overall I highly recommend this book and I hope the author comes out with new editions for new versions of Clojure.
Amazon Verified review Amazon
Lucas Medeiros Reis Oct 25, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I appreciate technical books that present not only theory, but useful practical tips as well. A lot of books fall somewhere in the middle, presenting little or no theory, and techniques that do not fit in real world projects. This book rises far above this threshold. My main highlights are:Chapter one, "Performance by Design". Made me think differently about performance no matter which language I'll be using.Chapter four, "Host Performance". Learned a lot about the JVM.Chapter six, "Measuring Performance". Measuring is maybe the most important part of performance tuning, and this chapter has both theory and practical Clojure tips.
Amazon Verified review Amazon
Oleg Okun Nov 13, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is for Clojure professionals thinking of practical applications of their code. It covers a broad range of topics, including use case classification according to performance requirements (CPU, memory, cache, input/output, online and offline data processing), performance characteristics (latency, bandwidth, throughput), data structures and how Clojure stores different types of objects, computational complexity of operations involving these types, abstractions and how to make decisions about which abstraction to use depending on performance use cases, Java Virtual Machine (JVM) on which Clojure runs (by default, Clojure may not produce optimized JVM bytecode!; hence, understanding of how Clojure interacts with Java is of importance), influence of host (hardware + JVM) characteristics on performance, concurrency and concurrency related data structures, parallelization support, measuring and optimizing performance (code profiling, performance statistics).
Amazon Verified review Amazon
Blake Watson Oct 25, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
This is a big, big topic crammed into a smallish (160+, excluding TOC, index, etc.) page book, but for all that, if you use it right it can be very helpful. This is not a beginner's book: it describes the nitty-gritty of how Clojure deals with various situations, which are things you don't want to concern yourself with until you have to. Laziness is covered, of course, as is chunking, transducers, mutability, transients, looping/recursion, and so on, and that's just Chapter 2! (Chapter 1 is mostly nomenclature.)Now, frankly, if I'm writing a 160 page book on optimization, I would probably take all 160 pages to talk about the material in Chapter 2. These are the underpinnings of the high level aspects of Clojure, but Kumar goes on to spend a chapter on Java bytecode, hardware optimizations. It's a lot of material, and it's only touched on here, by-and-large.I didn't find that to be a bad thing, however. It's perhaps less useful than it might be if you had a giant book that had your exact problem in it, I suppose, but what I've done is to read a chapter through quickly, and then look at my code and see if I can apply what I learned there. This not only has the potential to make the code faster, but to give you a deeper understanding of what's going on under the covers.You want to know going in, though. There's not a lot of hand-holding. I'm still pondering over the section on Agents, which I'm pretty sure I've heard Rich Hickey say he never uses. I kind of wanted the "Measuring Performance" (chapter 6) up front since that's what we're trying to do. Maybe Chapter 7, too.Anyway, this approach with this material worked for me, and I liked the way it removed some of the mystery of Clojure for me, or at least help me resolve some of the mystery by pointing me in the right direction.
Amazon Verified review Amazon
Mr C M Fleming Dec 08, 2015
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
Overall, I thought this book was a good read. It provides a good overview of many essential concepts - throughput, bandwidth and latency, concurrency concepts, touches lightly on algorithmic complexity and also covers the essential statistical concepts required to understand Criterium's output and reason about performance. It's coverage of the Clojure data structures and especially the concurrency constructs is very detailed.However I came away feeling that there was a lack of actionable information in the book. As an experienced Clojure developer, I learned several things that I didn't know previously, but only as brief pointers to external information. The book spent a long time on topics which are undoubtedly interesting, but provided little guidance on what the information means for someone trying to improve the performance of their programs. For example, the discussion of modern processor and memory architecture was interesting and detailed, but how should I apply that knowledge to my projects, especially in a very high-level language such as Clojure? Subjects such as OS level profiling are mentioned, but these are highly specialised topics which have no place in a book this short.In the end I think this book treads an uneasy path - it's very detailed in places, but is too short to contain enough information to make many of the topics worth while. In the end, it tries to address an enormous topic in a short book, and I think it would have been better to have remained more focused on how Clojure developers specifically can improve their programs' performance. It will be useful to someone without a lot of experience in JVM or Clojure performance topics, but more experienced users will probably find it comes up short.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.