You are previewing Java™ Performance and Scalability, Volume 1: Server-Side Programming Techniques.
O'Reilly logo
Java™ Performance and Scalability, Volume 1: Server-Side Programming Techniques

Book Description

This book was written with one goal in mind: to provide Java programmers with the expertise needed to build efficient, scalable Java code. The author shares his experience in server-side performance tuning through measured performance assessments, called optimizations. Each optimization discusses techniques to improve the performance and scalability of your code. Every claim is substantiated with hard numbers and an experience-based evaluation. Java™ Performance and Scalability, Volume 1, provides invaluable advice that you will, no doubt, find useful in your coding.

Presented in 48 concise lessons that target the most common and critical performance pitfalls, this book offers a plethora of practical tips and solutions for boosting the performance of your programs. These lessons cover performance-critical areas such as memory management, garbage collection, caching, and multithreading.

Specific lessons include:

  • Reserving StringBuffer capacity

  • Avoiding premature object creation

  • Creating an efficient vector class

  • Designing caching into your API

  • The cost of synchronization

  • Parallel subtasks

  • JNI efficiency

  • Varying the server workload and RMI network plumbing

  • Using ServletOutputStream

  • Caching JDBC™ connections

  • In addition to providing hard numbers that quantify the optimizations, the author concludes the book with an application demonstrating the effectiveness of the performance optimizations. The exercise takes a typical program and increases its performance fourfold through a series of steps that tie together the lessons learned throughout the book. He offers both the means and the proof to better coding.



    0201704293B04062001

    Table of Contents

    1. Copyright
      1. Dedication
    2. List of Figures
      1. Chapter 1
      2. Chapter 2
      3. Chapter 3
      4. Chapter 5
      5. Chapter 6
      6. Chapter 7
      7. Chapter 8
      8. Chapter 9
      9. Chapter 10
      10. Chapter 11
    3. Preface
    4. Introduction
      1. Java Speed
      2. The Test Environment
      3. Before You Optimize: A Word of Caution
      4. Organization of This Book
      5. Acknowledgments
    5. 1. Java Strings
      1. Optimization 1: String Concatenation
      2. Optimization 2: Fight Object Overpopulation
      3. Optimization 3: String Equality
      4. Optimization 4: Characters to Bytes and Back
      5. Optimization 5: StringTokenizer
      6. Optimization 6: charAt()
      7. Optimization 7: Prefer charAt() to startsWith()
      8. Optimization 8: Reserve StringBuffer Capacity
      9. Key Points
    6. 2. Pure Overhead
      1. Optimization 9: Useless Computations
      2. Optimization 10: Don't Trace Trivial Methods
      3. Optimization 11: Making Overhead Vanish
      4. Optimization 12: Premature Object Creation
      5. Optimization 13: Don't Initialize Twice
      6. Optimization 14: Do You Really Need to Zero-Out a Buffer?
      7. Optimization 15: The 80–20 Principle
      8. Optimization 16: Purge Obsolete Code
      9. Key Points
    7. 3. Vectors and Hashtables
      1. Optimization 17: Vector Add and Remove
      2. Optimization 18: Vector Capacity
      3. Optimization 19: Vector Enumeration
      4. Optimization 20: Efficient Vector Class
      5. Optimization 21: Using the API Wisely
      6. Optimization 22: Hashtable Parameters
      7. Optimization 23: Speed Up hashCode()
      8. Optimization 24: Speed Up equals()
      9. Key Points
    8. 4. Caching
      1. Optimization 25: Cache File Contents
      2. Optimization 26: Design Caching into Your API
      3. Optimization 27: Precompute
      4. Optimization 28: Relax Granularity
      5. Optimization 29: Inheritance
      6. Key Points
    9. 5. I/O Streams
      1. Example I/O Code
      2. Optimization 30: Output Buffering
      3. Optimization 31: Don't Flush Prematurely
      4. Optimization 32: Prefer Byte Stream to Unicode
      5. Optimization 33: Input Buffering
      6. Optimization 34: Byte-to-Char Conversions
      7. Optimization 35: Binary I/O
      8. Key Points
    10. 6. Recycling
      1. Optimization 36: Object Recycling
      2. Optimization 37: Object Pools
      3. Optimization 38: Last In, First Out
      4. Key Points
    11. 7. Scalability and Multithreading
      1. Scalability Basics
      2. The SMP Architecture
      3. Amdahl's Law
      4. Multithreaded and Synchronization Terminology
      5. The Cost of Synchronization
      6. Optimization 39: Parallel Subtasks
      7. Optimization 40: Synchronization False Sharing
      8. Optimization 41: Lock Fusion
      9. Optimization 42: Code Motion
      10. Optimization 43: Share Nothing
      11. Optimization 44: Partial Sharing
      12. Optimization 45: Read/Write Locks
      13. Key Points
    12. 8. JNI
      1. Optimization 46: JNI Surprise
      2. Optimization 47: JNI to the Rescue
      3. Optimization 48: Prefer System.arraycopy()
      4. Key Points
    13. 9. RMI
      1. The RMI Network Plumbing
      2. Varying the Server Workload
      3. RMI Server on Remote Machine
      4. Serializing Heavyweight Objects
      5. Key Points
    14. 10. Servlets
      1. Servlets Using PrintWriter
      2. Servlets Using ServletOutputStream
      3. Precomputing Message Bytes
      4. Pumping More Data
      5. PingServlet—A Performance Upper Bound
      6. Extending PingServlet
      7. Session State
      8. PingJdbc
      9. Caching JDBC Connections
      10. JSP—Java Server Pages
      11. RMI
      12. Key Points
    15. 11. A Java Web Server
      1. Version 1: The Original Server
      2. Version 2
        1. Cache the Date Response Header
        2. Cache the File's Last-Modified Attribute
      3. Version 3
        1. Exploit ASCII-to-Byte Conversion for HTTP Headers
        2. Use ASCII for Logging
        3. Eliminate Needless Reset
        4. Reuse Threads in LIFO Order
      4. Version 4
        1. Cache the Response Headers
        2. Eliminate Explicit Flushing
        3. Increase Parallelism
      5. Version 5
        1. Cache HTML Documents
        2. Cache Client IP Addresses
      6. Version 6
        1. Reuse What You Already Know
        2. Prefer Byte Arrays to String Objects
        3. Replace a PrintStream with an OutputStream
        4. Buffer Output Data
      7. Key Point
    16. A. Caching JDBC Connections
      1. CacheDriver.java
      2. CacheConnection.java
      3. CacheStatement.java
      4. CacheCallableStatement.java
      5. CachePreparedStatement.java
      6. CacheDriver.properties
    17. B. Simple Java Web Server
      1. Server.java
      2. FileInfo.java
      3. UrlCache.java
      4. LazyDate.java
      5. MyGetBytes.java
      6. server.properties
    18. Bibliography