O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Clojure High Performance Programming

Book Description

Written for intermediate Clojure developers, this compact guide will raise your expertise several notches. It tackles all the fundamentals of analyzing and optimizing performance in clear, logical chapters.

  • See how the hardware and the JVM impact performance
  • Learn which Java features to use with Clojure, and how
  • Deep dive into Clojure's concurrency and state primitives
  • Discover how to design Clojure programs for performance

In Detail

Clojure is a young, dynamic, functional programming language that runs on the Java Virtual Machine. It is built with performance, pragmatism, and simplicity in mind. Like most general purpose languages, Clojure’s features have different performance characteristics that one should know in order to write high performance code.

Clojure High Performance Programming is a practical, to-the-point guide that shows you how to evaluate the performance implications of different Clojure abstractions, learn about their underpinnings, and apply the right approach for optimum performance in real-world programs.

This book discusses the Clojure language in the light of performance factors that you can exploit in your own code.

You will also learn about hardware and JVM internals that also impact Clojure’s performance. Key features include performance vocabulary, performance analysis, optimization techniques, and how to apply these to your programs. You will also find detailed information on Clojure's concurrency, state-management, and parallelization primitives.

This book is your key to writing high performance Clojure code using the right abstraction, in the right place, using the right technique.

Table of Contents

  1. Clojure High Performance Programming
    1. Table of Contents
    2. Clojure High Performance Programming
    3. Credits
    4. About the Author
    5. Acknowledgments
    6. About the Reviewers
    7. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
    8. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Errata
        2. Piracy
        3. Questions
    9. 1. Performance by Design
      1. Use case classification
        1. User-facing software
        2. Computational and data-processing tasks
          1. CPU bound
          2. Memory bound
          3. Cache bound
          4. Input/Output (I/O) bound
        3. Online transaction processing (OLTP)
        4. Online analytical processing (OLAP)
        5. Batch processing
        6. Structured approach for performance
      2. Performance vocabulary
        1. Latency
        2. Throughput
        3. Bandwidth
        4. Baseline and benchmark
        5. Profiling
        6. Performance optimization
        7. Concurrency and parallelism
        8. Resource utilization
        9. Workload
      3. Latency numbers every programmer should know
      4. Summary
    10. 2. Clojure Abstractions
      1. Non-numeric scalars and interning
      2. Identity, value, and epochal time model
        1. Variables and mutation
        2. Collection types
      3. Persistent data structures
        1. Constructing less-used data structures
        2. Complexity guarantee
        3. Concatenation of persistent data structures
      4. Sequences and laziness
        1. Laziness
          1. Laziness in data structure operations
          2. Constructing lazy sequences
            1. Custom chunking
            2. Macros and closures
      5. Transients
        1. Fast repetition
      6. Performance miscellanea
        1. Disabling assertions in production
        2. Destructuring
        3. Recursion and tail-call optimization (TCO)
          1. Premature end in reduce
        4. Multimethods versus protocols
        5. Inlining
      7. Summary
    11. 3. Leaning on Java
      1. Inspect the equivalent Java source for Clojure code
        1. Create a new project
        2. Compile Clojure sources into Java bytecode
        3. Decompile the .class files into Java source
      2. Numerics, boxing, and primitives
      3. Arrays
      4. Reflection and type hints
        1. Array of primitives
        2. Primitives
        3. Macros and metadata
        4. Miscellaneous
      5. Using array/numeric libraries for efficiency
        1. HipHip
        2. primitive-math
      6. Resorting to Java and native code
        1. Proteus – mutable locals in Clojure
      7. Summary
    12. 4. Host Performance
      1. The hardware
        1. Processors
          1. Branch prediction
          2. Instruction scheduling
          3. Threads and cores
        2. Memory systems
          1. Cache
          2. Interconnect
        3. Storage and networking
      2. The Java Virtual Machine
        1. The just-in-time (JIT) compiler
        2. Memory organization
        3. HotSpot heap and garbage collection
        4. Measuring memory (heap/stack) usage
      3. Measuring latency with Criterium
        1. Criterium and Leiningen
      4. Summary
    13. 5. Concurrency
      1. Low-level concurrency
        1. Hardware memory barrier instructions
        2. Java support and its Clojure equivalent
      2. Atomic updates and state
        1. Atomic updates in Java
        2. Clojure's support for atomic updates
      3. Asynchronous agents and state
        1. Asynchrony, queuing, and error handling
        2. Advantages of agents
        3. Nesting
      4. Coordinated transactional ref and state
        1. Ref characteristics
        2. Ref history and intransaction deref operations
        3. Transaction retries and barging
        4. Upping transaction consistency with ensure
        5. Fewer transaction retries with commutative operations
        6. Agents can participate in transactions
        7. Nested transactions
        8. Performance considerations
      5. Dynamic var binding and state
      6. Validating and watching the reference types
      7. Java concurrent data structures
        1. Concurrent maps
        2. Concurrent queues
        3. Clojure support for concurrent queues
      8. Concurrency with threads

        1. JVM support for threads
        2. Thread pools in the JVM
        3. Clojure concurrency support
          1. Asynchronous execution with Futures
          2. Anticipated asynchronous execution result with promises
      9. Clojure parallelization and the JVM
        1. Moore's law
        2. Amdahl's law
        3. Clojure support for parallelization
          1. pmap
          2. pcalls
          3. pvalues
        4. Java 7's fork/join framework
      10. Parallelism with reducers
        1. Reducible, reducer function, reduction transformation
        2. Realizing reducible collections
        3. Foldable collections and parallelism
      11. Summary
    14. 6. Optimizing Performance
      1. A tiny statistics terminology primer
        1. Median, first quartile, and third quartile
        2. Percentile
        3. Variance and standard deviation
        4. Understanding criterium output
        5. Guided performance objectives
      2. Performance testing
        1. Test environment
        2. What to test
        3. Measuring latency
        4. Measuring throughput
        5. Load, stress, and endurance tests
      3. Performance monitoring
        1. Introspection
          1. JVM instrumentation via JMX
      4. Profiling
        1. OS and CPU-cache-level profiling
        2. I/O profiling
      5. Performance tuning
        1. JVM tuning
        2. I/O tuning and backpressure
      6. Summary
    15. 7. Application Performance
      1. Data sizing
        1. Reduced serialization
        2. Chunking to reduce memory pressure
          1. Sizing for file/network operations
          2. Sizing for JDBC query results
      2. Resource pooling
        1. JDBC resource pooling
      3. I/O batching and throttling
        1. JDBC batch operations
        2. Batch support at API level
        3. Throttling requests to services
      4. Precomputing and caching
      5. Concurrent pipelines
        1. Distributed pipelines
      6. Applying back pressure
        1. Thread pool queues
        2. Servlet containers like Tomcat and Jetty
        3. HTTP Kit
      7. Performance and queuing theory
        1. Little's Law
      8. Summary
    16. Index