Hands-On High Performance with Go

Book description

Proven methodologies and concurrency techniques that will help you write faster and better code with Go programming

Key Features

  • Explore Go's profiling tools to write faster programs by identifying and fixing bottlenecks
  • Address Go-specific performance issues such as memory allocation and garbage collection
  • Delve into the subtleties of concurrency and discover how to successfully implement it in everyday applications

Book Description

Go is an easy-to-write language that is popular among developers thanks to its features such as concurrency, portability, and ability to reduce complexity. This Golang book will teach you how to construct idiomatic Go code that is reusable and highly performant.

Starting with an introduction to performance concepts, you'll understand the ideology behind Go's performance. You'll then learn how to effectively implement Go data structures and algorithms along with exploring data manipulation and organization to write programs for scalable software. This book covers channels and goroutines for parallelism and concurrency to write high-performance code for distributed systems. As you advance, you'll learn how to manage memory effectively. You'll explore the compute unified device architecture (CUDA) application programming interface (API), use containers to build Go code, and work with the Go build cache for quicker compilation. You'll also get to grips with profiling and tracing Go code for detecting bottlenecks in your system. Finally, you'll evaluate clusters and job queues for performance optimization and monitor the application for performance regression.

By the end of this Go programming book, you'll be able to improve existing code and fulfill customer requirements by writing efficient programs.

What you will learn

  • Organize and manipulate data effectively with clusters and job queues
  • Explore commonly applied Go data structures and algorithms
  • Write anonymous functions in Go to build reusable apps
  • Profile and trace Go apps to reduce bottlenecks and improve efficiency
  • Deploy, monitor, and iterate Go programs with a focus on performance
  • Dive into memory management and CPU and GPU parallelism in Go

Who this book is for

This Golang book is a must for developers and professionals who have an intermediate-to-advanced understanding of Go programming, and are interested in improving their speed of code execution.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On High Performance with Go
  3. Dedication
  4. About Packt
    1. Why subscribe?
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Code in Action
      3. Download the color images
      4. Conventions used
    4. Get in touch
      1. Reviews
  7. Section 1: Learning about Performance in Go
  8. Introduction to Performance in Go
    1. Technical requirements
    2. Understanding performance in computer science
      1. A brief note on Big O notation
      2. Methods to gauge long term performance
        1. Optimization strategies overview
      3. Optimization levels
    3. A brief history of Go
      1. The Go standard library
      2. Go toolset
        1. Benchmarking overview
    4. The ideology behind Go performance
      1. Goroutines – performance from the start
      2. Channels – a typed conduit
      3. C-comparable performance
      4. Large-scale distributed systems
    5. Summary
  9. Data Structures and Algorithms
    1. Understanding benchmarking
      1. Benchmark execution
      2. Real-world benchmarking
    2. Introducing Big O notation
      1. Practical Big O notation example
      2. Data structure operations and time complexity
      3. O(1) – constant time
      4. O(log n) - logarithmic time
      5. O(n) – linear time
      6. O(n log n) – quasilinear time
      7. O(n2) – quadratic time
      8. O(2n) – exponential time
    3. Understanding sort algorithms
      1. Insertion sort
      2. Heap sort
      3. Merge sort
      4. Quick sort
    4. Understanding search algorithms
      1. Linear search
      2. Binary search
    5. Exploring trees
      1. Binary trees
      2. Doubly linked list
    6. Exploring queues
      1. Common queuing functions
      2. Common queuing patterns
    7. Summary
  10. Understanding Concurrency
    1. Understanding closures
      1. Anonymous functions
      2. Anonymous functions with respect to closures
      3. Closures for nesting and deferring work
      4. HTTP handlers with closures
    2. Exploring goroutines
      1. The Go scheduler
      2. Go scheduler goroutine internals
        1. The M struct
        2. The P struct
        3. The G struct
      3. Goroutines in action
    3. Introducing channels
      1. Channel internals
      2. Buffered channels
        1. Ranges over channels
      3. Unbuffered channels
        1. Selects
    4. Introducing semaphores
    5. Understanding WaitGroups
    6. Iterators and the process of iteration
    7. Briefing on generators
    8. Summary
  11. STL Algorithm Equivalents in Go
    1. Understanding algorithms in the STL
      1. Sort
      2. Reverse
      3. Min element and max element
      4. Binary search
    2. Understanding containers
      1. Sequence containers
        1. Array
        2. Vector
        3. Deque
        4. List
        5. Forward list
      2. Container adapters
        1. Queue
        2. Priority queue
        3. Stack
      3. Associative containers
        1. Set
        2. Multiset
        3. Map
        4. Multimap
    3. Understanding function objects
      1. Functors
      2. Iterators
        1. Internal iterators
        2. External iterators
        3. Generators
        4. Implicit Iterators
    4. Summary
  12. Matrix and Vector Computation in Go
    1. Introducing Gonum and the Sparse library
    2. Introducing BLAS
    3. Introducing vectors
      1. Vector computations
    4. Introducing matrices
    5. Matrix operations
      1. Matrix addition
        1. A practical example (matrix subtraction)
      2. Scalar multiplication
      3. Scalar multiplication practical example
      4. Matrix multiplication
      5. Matrix multiplication practical example
      6. Matrix transposition
      7. Matrix transposition practical example
    6. Understanding matrix structures
      1. Dense matrices
      2. Sparse matrices
        1. DOK matrix
        2. LIL matrix
        3. COO matrix
          1. CSR matrix
          2. CSC matrix
    7. Summary
  13. Section 2: Applying Performance Concepts in Go
  14. Composing Readable Go Code
    1. Maintaining simplicity in Go
    2. Maintaining readability in Go
    3. Exploring packaging in Go
      1. Package naming
      2. Packaging layout
      3. Internal packaging
      4. Vendor directory
      5. Go modules
    4. Understanding naming in Go
    5. Understanding formatting in Go
    6. Briefing on interfaces in Go
    7. Comprehending methods in Go
    8. Comprehending inheritance in Go
    9. Exploring reflection in Go
      1. Types
      2. Kinds
      3. Values
    10. Summary
  15. Template Programming in Go
    1. Understanding Go generate
      1. Generated code for protobufs
        1. Protobuf code results
    2. The link toolchain
    3. Introducing Cobra and Viper for configuration programming
      1. Cobra/Viper resulting sets
    4. Text templating
    5. HTML templating
    6. Exploring Sprig
      1. String functions
      2. String slice functions
      3. Default functions
    7. Summary
  16. Memory Management in Go
    1. Understanding Modern Computer Memory - A Primer
    2. Allocating memory
      1. Introducing VSZ and RSS
    3. Understanding memory utilization
      1. Go runtime memory allocation
        1. Memory allocation primer
      2. Memory object allocation
    4. Briefing on limited memory situations
    5. Summary
  17. GPU Parallelization in Go
    1. Cgo – writing C in Go
      1. A simple Cgo example
    2. GPU-accelerated computing – utilizing the hardware
      1. CUDA – utilizing host processes
      2. Docker for GPU-enabled programming
    3. CUDA on GCP
      1. Creating a VM with a GPU
      2. Install the CUDA driver
      3. Install Docker CE on GCP
      4. Installing NVIDIA Docker on GCP
      5. Scripting it all together
    4. CUDA – powering the program
    5. Summary
  18. Compile Time Evaluations in Go
    1. Exploring the Go runtime
      1. GODEBUG
      2. GCTRACE
      3. GOGC
      4. GOMAXPROCS
      5. GOTRACEBACK
      6. Go build cache
      7. Vendoring dependencies
      8. Caching and vendoring improvements
      9. Debug
      10. PProf/race/trace
    2. Understanding functions
      1. KeepAlive
      2. NumCPU
      3. ReadMemStats
    3. Summary
  19. Section 3: Deploying, Monitoring, and Iterating on Go Programs with Performance in Mind
  20. Building and Deploying Go Code
    1. Building Go binaries
    2. Go build – building your Go code
      1. Build flags
      2. Build information
      3. Compiler and linker flags
      4. Build constraints
      5. Filename conventions
    3. Go clean – cleaning your build directory
    4. Retrieving package dependencies with go get and go mod
    5. Go list
    6. Go run – executing your packages
    7. Go install – installing your binaries
    8. Building Go binaries with Docker
    9. Summary
  21. Profiling Go Code
    1. Understanding profiling
    2. Exploring instrumentation methodologies
      1. Implementing profiling with go test
      2. Manually instrumenting profiling in code
      3. Profiling running service code
    3. Briefing on CPU profiling
    4. Briefing on memory profiling
    5. Extended capabilities with upstream pprof
    6. Comparing multiple profiles
    7. Interpreting flame graphs within pprof
    8. Detecting memory leaks in Go
    9. Summary
  22. Tracing Go Code
    1. Implementing tracing instrumentation
    2. Understanding the tracing format
    3. Understanding trace collection
      1. Movement in the tracing window
    4. Exploring pprof-like traces
    5. Go distributed tracing
    6. Implementing OpenCensus for your application
    7. Summary
  23. Clusters and Job Queues
    1. Clustering in Go
      1. K-nearest neighbors
      2. K-means clustering
    2. Exploring job queues in Go
      1. Goroutines as job queues
      2. Buffered channels as job queues
      3. Integrating job queues
        1. Kafka
        2. RabbitMQ
    3. Summary
  24. Comparing Code Quality Across Versions
    1. Go Prometheus exporter – exporting data from your Go application
    2. APM – watching your distributed system performance
      1. Google Cloud environment setup
      2. Google Cloud Trace code
    3. SLIs and SLOs – setting goals
      1. Measuring traffic
      2. Measuring latency
      3. Measuring errors
      4. Measuring saturation
      5. Grafana
    4. Logging – keeping track of your data
    5. Summary
  25. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On High Performance with Go
  • Author(s): Bob Strecansky
  • Release date: March 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781789805789