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

Rust High Performance

Book Description

Find bottlenecks, identify the proper algorithm to use, optimize performance, and create really efficient Rust applications

About This Book

  • Understand common performance pitfalls and improve the performance of your applications.
  • Get to grips with parallel programming and multithreading with Rust.
  • Learn metaprogramming in Rust.

Who This Book Is For

This book is for Rust developers keen to improve the speed of their code or simply to take their skills to the next level.

What You Will Learn

  • Master tips and tricks to make your code faster.
  • Learn how to identify bottlenecks in your Rust applications
  • Discover how to profile your Rust software.
  • Understand the type system to create compile-time optimizations.
  • Master the borrow checker .
  • Learn metaprogramming in Rust to avoid boilerplate code.
  • Discover multithreading and work stealing in Rust.
  • Understand asynchronous programming in Rust.

In Detail

At times, it is difficult to get the best performance out of Rust. This book teaches you to optimize the speed of your Rust code to the level of languages such as C/C++. You'll understand and fix common pitfalls, learn how to improve your productivity by using metaprogramming, and speed up your code by concurrently executing parts of it safely and easily. You will master the features of the language which will make you stand out and use them to really improve the efficiency of your algorithms

The book begins with a gentle introduction to help you identify bottlenecks when programming in Rust. We highlight common performance pitfalls, along with strategies to detect and resolve these issues early. We move on to mastering Rust's type system, which will enable us to create impressive optimizations in both performance and safety at compile time. You will then learn how to effectively manage memory in Rust, mastering the borrow checker. We move on to measuring performance and you will see how this affects the way you write code. Moving ahead, you will perform metaprogramming in Rust to boost the performance of your code and your productivity. You will finally learn parallel programming in Rust, which enables efficient and faster execution by using multithreading and asynchronous programming.

Style and approach

You'll embark on a learning journey that will teach about you deep-core concepts in the Rust language. Learning those concepts—such as understanding the borrow checker—will make you competent to write more efficient Rust code. To learn those core concepts, you'll perform practical work and see for yourself how specific patterns improve the performance of your code.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Rust High Performance
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  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. Conventions used
    4. Get in touch
      1. Reviews
  7. Common Performance Pitfalls
    1. Asking the Rust compiler about performance
      1. Optimizations
      2. Build configuration
        1. Optimization level
        2. Debug information
        3. Link-time optimizations
        4. Debug assertions
        5. Panic behavior
        6. Runtime library paths
    2. Translation issues
      1. Indexing degradations
      2. Using iterators
        1. Iterator adaptors
          1. Real-life example
        2. Specialized adaptors
        3. Interaction between adaptors
      3. Itertools
      4. Borrowing degradations
      5. Cyclomatic complexity
    3. Summary
  8. Extra Performance Enhancements
    1. Compile-time checks
      1. Sequential state machines
      2. Complex state machines
      3. Real-life type system check example
    2. Extra performance tips
      1. Using closures to avoid runtime evaluation
      2. Unstable sorting
      3. Map hashing
        1. Perfect hash functions
    3. Standard library collections
      1. Sequences
      2. Maps
      3. Sets
    4. Summary
  9. Memory Management in Rust
    1. Mastering the borrow checker
      1. Allocations
      2. Mutability, borrowing, and owning
      3. Lifetimes
    2. Memory representation
      1. Alignment
      2. Complex enumerations
      3. Unions
    3. Shared pointers
      1. The cell module
        1. Cells
        2. RefCell
      2. The rc module
    4. Summary
  10. Lints and Clippy
    1. Using Rust compiler lints
      1. Lints
        1. Avoiding anonymous parameters
        2. Avoiding heap allocated box pointers
        3. Avoiding missing implementations
        4. Enforcing documentation
        5. Pointing out trivial casts
        6. Linting unsafe code blocks
        7. Unused lints
        8. Variant size differences
      2. Lint groups
    2. Clippy
      1. Installation
      2. Configuration
      3. Lints
        1. Casting
        2. Bad practice
        3. Performance lints
        4. Unwraps
        5. Shadowing
        6. Integer overflow
      4. Lint groups
    3. Summary
  11. Profiling Your Rust Application
    1. Understanding the hardware
      1. Understanding how the CPU works
      2. Speeding up memory access with the cache
        1. Cache misses
          1. How can you fix it?
        2. Cache invalidation
      3. CPU pipeline
        1. Branch prediction
          1. The relevance of branch prediction for our code
    2. Profiling tools
      1. Valgrind
        1. Callgrind
        2. Cachegrind
      2. OProfile
    3. Summary
  12. Benchmarking
    1. Selecting what to benchmark
    2. Benchmarking in nightly Rust
    3. Benchmarking in stable Rust
    4. Continuous integration for benchmarks
      1. Travis-CI integration
      2. Benchmark statistics with Criterion
    5. Summary
  13. Built-in Macros and Configuration Items
    1. Understanding attributes
      1. Trait derivations
      2. Crate features
      3. Configuration attributes
    2. Macros
      1. Console printing
      2. String formatting
      3. Compilation environment
      4. Loading byte arrays and strings at compile time
      5. Code paths
      6. Checking preconditions and postconditions
      7. Others
    3. Nightly Rust
      1. Conservative trait return
      2. Constant functions
      3. Inline assembly and naked functions
      4. Using bigger integers
      5. Single instruction multiple data
      6. Allocation API
      7. Compiler plugins
    4. Summary
  14. Must-Have Macro Crates
    1. Working with external data
      1. Data serialization and deserialization
      2. Serializing and deserializing complex structures
      3. Parsing byte streams
    2. Learning about useful small crates
      1. Creating lazily evaluated statics
      2. Avoiding boilerplate code for the builder pattern
      3. Managing errors
      4. Logging efficiently in Rust
      5. Creating command-line interfaces
    3. Using Rust for web development
      1. Creating extremely efficient templates
      2. Connecting with a database
      3. Creating a complete web server
    4. Summary
  15. Creating Your Own Macros
    1. Creating your own standard macros
      1. Macro variants
      2. Complex macros
    2. Creating procedural macros
      1. Implementing a simple trait
      2. Implementing complex derivations
        1. Implementing getters
        2. Implementing setters
    3. Metaprogramming in nightly Rust
      1. Understanding compiler plugins
      2. Declarative macros
    4. Summary
  16. Multithreading
    1. Concurrency in Rust
      1. Understanding the Send and Sync traits
        1. The Send trait
        2. The Sync trait
      2. Other types of concurrency in Rust
    2. Understanding multithreading
      1. Creating threads
      2. Panicking in Rust
    3. Moving data between threads
      1. The move keyword
      2. Sharing data between threads
      3. Channels between threads
    4. Multithreading crates
      1. Non-blocking data structures
        1. Scoped threads
      2. Thread pooling
      3. Parallel iterators
    5. Summary
  17. Asynchronous Programming
    1. Introduction to asynchronous programming
      1. Understanding I/O in the CPU
      2. Getting the kernel to control the I/O
      3. Asynchronous programming from the programmer's perspective
    2. Understanding futures
      1. Future combinators
    3. Asynchronous I/O in Rust
      1. Creating Tokio codecs
      2. WebSockets in Rust
    4. Understanding the new Generators
    5. Summary
  18. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think