ebook img

Rust high performance: learn to skyrocket the performance of your Rust applications PDF

265 Pages·2018·3.213 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Rust high performance: learn to skyrocket the performance of your Rust applications

Rust High Performance Learn to skyrocket the performance of your Rust applications Iban Eguia Moraza BIRMINGHAM - MUMBAI Rust High Performance Copyright © 2018 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Commissioning Editor: Merint Mathew Acquisition Editor: Sandeep Mishra Content Development Editor: Akshada Iyer Technical Editor: Abhishek Sharma Copy Editor: Safis Editing Project Coordinator: Prajakta Naik Proofreader: Safis Editing Indexer: Mariammal Chettiyar Graphics: Jisha Chirayil Production Coordinator: Arvindkumar Gupta First published: March 2018 Production reference: 1270318 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78839-948-7 www.packtpub.com To my father, Manu, and to the memory of my mother, Arantza, for giving me all the opportunities I had in my life, and for being there when I most needed it. mapt.io Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Mapt is fully searchable Copy and paste, print, and bookmark content PacktPub.com Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. Contributors About the author Iban Eguia Moraza is a passionate Rust developer. He has a bachelor's degree in computer engineering and a master's degree in information and communication security. He has over 10 years of experience in web development, and since 2015, he has been developing Rust applications. Iban loves space exploration and the latest technologies. In this regard, he has developed open source software for stratospheric balloons from the ground up, and he now works at the CERN particle physics laboratory. He likes to travel to learn from the most experienced people. This book would not have been possible without the moral help of my dad and coworkers. They were really motivating when I was struggling to find enough time to write the book. I also have to thank the Rust community for always being helpful in fixing all my issues and questions. Without doubt, it's the most friendly and welcoming community I have ever encountered in software development. About the reviewer Daniel Durante is an avid coffee drinker/roaster, motorcyclist, archer, welder, and carpenter whenever he isn't programming. From the age of 12, he has been involved with web and embedded programming with PHP, Node.js, Golang, Rust, and C. He has worked on text-based browser games that have reached over 1,000,000 active players, created bin-packing software for CNC machines, embedded programming with cortex-m and PIC circuits, high-frequency trading applications, and helped contribute to one of the oldest ORMs of Node.js (SequelizeJS). He has also reviewed the following books for Packt: PostgresSQL Developer's Guide PostgreSQL 9.0 High Performance Rust Programming By Example I would like to thank my parents, my brother, my mentors, and friends who've all put up with my insanity of sitting in front of a computer day in and day out. I would not be here today if it wasn't for their patience, guidance, and love. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. Table of Contents Preface 1 Chapter 1: Common Performance Pitfalls 7 Asking the Rust compiler about performance 8 Optimizations 8 Build configuration 10 Optimization level 10 Debug information 10 Link-time optimizations 11 Debug assertions 13 Panic behavior 14 Runtime library paths 14 Translation issues 15 Indexing degradations 15 Using iterators 17 Iterator adaptors 18 Real-life example 22 Specialized adaptors 22 Interaction between adaptors 23 Itertools 24 Borrowing degradations 26 Cyclomatic complexity 27 Summary 28 Chapter 2: Extra Performance Enhancements 29 Compile-time checks 29 Sequential state machines 32 Complex state machines 34 Real-life type system check example 36 Extra performance tips 38 Using closures to avoid runtime evaluation 39 Unstable sorting 40 Map hashing 41 Perfect hash functions 43 Standard library collections 45 Sequences 45 Maps 46 Sets 47 Summary 47 Chapter 3: Memory Management in Rust 48 Table of Contents Mastering the borrow checker 48 Allocations 49 Mutability, borrowing, and owning 51 Lifetimes 53 Memory representation 55 Alignment 55 Complex enumerations 56 Unions 58 Shared pointers 60 The cell module 60 Cells 60 RefCell 62 The rc module 63 Summary 64 Chapter 4: Lints and Clippy 66 Using Rust compiler lints 66 Lints 67 Avoiding anonymous parameters 67 Avoiding heap allocated box pointers 67 Avoiding missing implementations 68 Enforcing documentation 69 Pointing out trivial casts 69 Linting unsafe code blocks 70 Unused lints 71 Variant size differences 73 Lint groups 73 Clippy 74 Installation 74 Configuration 75 Lints 75 Casting 76 Bad practice 76 Performance lints 78 Unwraps 79 Shadowing 79 Integer overflow 79 Lint groups 79 Summary 80 Chapter 5: Profiling Your Rust Application 81 Understanding the hardware 81 Understanding how the CPU works 82 Speeding up memory access with the cache 83 Cache misses 84 How can you fix it? 84 Cache invalidation 85 [ ii ] Table of Contents CPU pipeline 85 Branch prediction 87 The relevance of branch prediction for our code 89 Profiling tools 90 Valgrind 90 Callgrind 91 Cachegrind 93 OProfile 94 Summary 96 Chapter 6: Benchmarking 97 Selecting what to benchmark 97 Benchmarking in nightly Rust 99 Benchmarking in stable Rust 104 Continuous integration for benchmarks 106 Travis-CI integration 106 Benchmark statistics with Criterion 109 Summary 112 Chapter 7: Built-in Macros and Configuration Items 113 Understanding attributes 113 Trait derivations 114 Crate features 122 Configuration attributes 123 Macros 125 Console printing 125 String formatting 125 Compilation environment 126 Loading byte arrays and strings at compile time 126 Code paths 126 Checking preconditions and postconditions 127 Others 127 Nightly Rust 128 Conservative trait return 128 Constant functions 129 Inline assembly and naked functions 130 Using bigger integers 131 Single instruction multiple data 131 Allocation API 132 Compiler plugins 132 Summary 133 Chapter 8: Must-Have Macro Crates 134 Working with external data 134 Data serialization and deserialization 135 Serializing and deserializing complex structures 139 [ iii ]

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.