𝔖 Scriptorium
✦   LIBER   ✦

πŸ“

C++ high performance: boost and optimize the performance of your C++ 17 code

✍ Scribed by Andrist, Bjârn; Garney, Ben; Sehr, Viktor


Publisher
Packt Publishing Limited
Year
2017;2018
Tongue
English
Leaves
362
Category
Library

⬇  Acquire This Volume

No coin nor oath required. For personal study only.

✦ Synopsis


C++ is a highly portable language and can be used to write both large-scale applications and performance-critical code. It has evolved over the last few years to become a modern and expressive language. This book will guide you through optimizing the performance of your C++ apps by allowing them to run faster and consume fewer resources on the device they're running on without compromising the readability of your code base.

The book begins by helping you measure and identify bottlenecks in a C++ code base. It then moves on by teaching you how to use modern C++ constructs and techniques. You'll see how this affects the way you write code. Next, you'll see the importance of data structure optimization and memory management, and how it can be used efficiently with respect to CPU caches. After that, you'll see how STL algorithm and composable Range V3 should be used to both achieve faster execution and more readable code, followed by how to use STL containers and how to write your own specialized iterators.

Moving on, you'll get hands-on experience in making use of modern C++ metaprogramming and reflection to reduce boilerplate code as well as in working with proxy objects to perform optimizations under the hood. After that, you'll learn concurrent programming and understand lock-free data structures. The book ends with an overview of parallel algorithms using STL execution policies, Boost Compute, and OpenCL to utilize both the CPU and the GPU.

✦ Table of Contents


Cover......Page 1
Title Page......Page 2
Copyright and Credits......Page 3
Packt Upsell......Page 4
Foreword......Page 5
Contributors......Page 7
Table of Contents......Page 9
Preface......Page 18
Zero-cost abstractions......Page 24
Programming languages and machine code abstractions......Page 25
Robustness......Page 27
C++ compared with other languages......Page 28
Competing languages and performance......Page 29
Value semantics......Page 32
Const correctness......Page 34
Avoiding null objects using C++ references......Page 35
Drawbacks of C++......Page 37
Strict class interfaces......Page 38
Error handling and resource acquisition......Page 39
Preserving the valid state......Page 40
Resource acquisition......Page 41
Libraries used in this book......Page 42
Summary......Page 43
Using auto in function signatures......Page 44
Using auto for variables......Page 45
Const reference......Page 46
Conclusion......Page 47
Basic syntax of a C++ lambda function......Page 48
The capture block......Page 49
Capture by reference versus capture by value......Page 50
Similarities between a Lambda and a class......Page 51
Mutating lambda member variables......Page 52
Capture all......Page 54
Lambdas and std::function......Page 56
Implementing a simple Button class with std::function......Page 57
Invoking an std::function requires a few more operations than a lambda......Page 59
The polymorphic lambda......Page 60
Creating reusable polymorphic lambdas......Page 61
Const propagation for pointers......Page 63
Copy-constructing an object......Page 65
Swapping two objects......Page 66
Resource acquisition and the rule of three......Page 67
Implementing the rule of three......Page 68
Constructor......Page 69
Limitations of the rule of three......Page 70
Introducing move semantics......Page 71
Named variables and r-values......Page 73
Accept arguments by move when applicable......Page 74
Default move semantics and the rule of zero......Page 75
Rule of zero in a real code base......Page 76
A note on empty destructors......Page 77
A common pitfall - moving non-resources......Page 78
Optional return values......Page 80
Optional member variables......Page 81
Sorting and comparing std::optional......Page 82
Performance of std::any......Page 83
Summary......Page 84
Chapter 3: Measuring Performance......Page 85
Asymptotic complexity and big O notation......Page 86
Growth rates......Page 91
Amortized time complexity......Page 92
What to measure?......Page 94
Performance properties......Page 96
Knowing your code and hot spots......Page 97
Profilers......Page 98
Instrumentation profilers......Page 99
Sampling profilers......Page 101
Summary......Page 103
Properties of computer memory......Page 104
STL containers......Page 107
Vector and array......Page 108
List and forward_list......Page 111
Associative containers......Page 112
Ordered sets and maps......Page 113
Unordered sets and maps......Page 114
Hash and equals......Page 115
Container adaptors......Page 117
Priority queues......Page 118
Parallel arrays......Page 120
Summary......Page 128
The iterator concept......Page 129
Iterator categories......Page 130
Pointer-mimickingΒ syntax......Page 131
Iterators as generators......Page 133
Iterator traits......Page 134
Implementing a function using iterator categories......Page 135
Practical example – iterating floating point values within a range......Page 136
Illustrated usage examples......Page 137
Utility functions......Page 138
How to construct a linear range iterator......Page 139
Iterator usage example......Page 140
The make_linear_range convenience function......Page 141
Linear range usage examples......Page 142
Summary......Page 143
STL algorithm concepts......Page 144
Implementing a generic algorithm that can be used with any container......Page 145
Algorithms do not change the size of the container......Page 147
Algorithms with output require allocated data......Page 148
Algorithms use operator== and operator< by default......Page 149
General-purpose predicates......Page 150
Algorithms require move operators not to throw......Page 151
Algorithms have complexity guarantees......Page 152
STL algorithms versus handcrafted for-loops......Page 153
Real-world code base example......Page 154
Usage examples of STL algorithms versus handcrafted for-loops......Page 155
Example 1 – Unfortunate exceptions and performance problems......Page 156
Example 2 – STL has subtle optimizations even in simple algorithms......Page 158
Sorting only for the data you need to retrieve......Page 160
Use cases......Page 161
Performance evaluation......Page 162
Limitations of the iterators in STL......Page 163
Introduction to the ranges library......Page 166
Composability and pipeability......Page 168
Actions, views, and algorithms......Page 169
Actions......Page 170
Views......Page 171
Summary......Page 173
Chapter 7: Memory Management......Page 174
Memory pages......Page 175
Process memory......Page 177
Stack memory......Page 178
Heap memory......Page 181
Creating and deleting objects......Page 182
Placement new......Page 183
The new and delete operators......Page 184
Memory alignment......Page 186
Padding......Page 188
Memory ownership......Page 190
Handling resources implicitly......Page 191
Unique pointer......Page 193
Shared pointer......Page 194
Weak pointer......Page 195
Small size optimization......Page 196
Custom memory management......Page 199
Building an arena......Page 200
A custom memory allocator......Page 204
Summary......Page 209
Introduction to template metaprogramming......Page 210
Using integers as template parameters......Page 212
How the compiler handles a templateΒ function......Page 213
Type trait categories......Page 214
Using type traits......Page 215
Receiving the type of a variable with decltype......Page 216
Conditionally enable functions based on types with std::enable_if_t......Page 217
Introspecting class members with std::is_detected......Page 219
Usage example of is_detected and enable_if_t combined......Page 220
The constexpr keyword......Page 222
Verify compile-time computation using std::integral_constant......Page 223
The if constexpr statement......Page 224
Comparison with runtime polymorphism......Page 225
Example of generic modulus function using if constexpr......Page 226
The std::tuple container......Page 227
Accessing the members of a tuple......Page 228
Iterating std::tuple......Page 229
Unrolling the tuple......Page 230
Implementing other algorithms for tuples......Page 231
Accessing tuple elements......Page 232
Structured bindings......Page 233
An example of a function with variadic number of arguments......Page 234
How to construct a variadic parameter pack......Page 235
Using std::any as the base for a dynamic-size heterogenous container......Page 236
The std::variant......Page 238
Visiting variants......Page 239
Heterogenous container of variants......Page 240
Accessing the values in our variant container......Page 241
Example 1 – Reflection......Page 242
Using the reflection......Page 243
Evaluating the assembler output of the reflection......Page 244
Conditionally overloading global functions......Page 245
Testing reflection capabilities......Page 247
Example 2 – Creating a generic safe cast function......Page 249
Example 3 – Hash strings at compile time......Page 252
The advantages of compile-time hash sum calculation......Page 253
Constructing a PrehashedString class......Page 254
Evaluating PrehashedString......Page 256
Evaluating get_bitmap_resource() with PrehashedString......Page 257
Summary......Page 258
Lazy versus eager evaluation......Page 259
Comparing concatenated strings using a proxy......Page 261
Implementing the proxy......Page 262
The r-value modifier......Page 264
A simple two-dimensional point class......Page 265
The underlying mathematics......Page 266
Implementing the DistProxy object......Page 268
Expanding DistProxy to something more useful......Page 270
Calculating distances with DistProxy......Page 271
Preventing the misuse of DistProxy......Page 272
Creative operator overloading and proxy objects......Page 273
The pipe operator......Page 274
The infix operator......Page 275
Summary......Page 277
Chapter 10: Concurrency......Page 278
What makes concurrent programming hard?......Page 279
Concurrency and parallelism......Page 280
Time slicing......Page 281
Shared memory......Page 282
Data races......Page 284
Mutex......Page 285
Deadlock......Page 287
Synchronous and asynchronous tasks......Page 288
Threads......Page 289
Thread states......Page 292
Protecting critical sections......Page 293
Avoiding deadlocks......Page 295
Condition variables......Page 296
Returning data and handling errors......Page 298
Tasks......Page 299
Atomic support in C++......Page 301
Using shared_ptr in a multithreaded environment......Page 302
C++ memory model......Page 305
Instruction reordering......Page 306
Atomics and memory orders......Page 307
Lock-free programming......Page 309
Lock-free queue example......Page 310
Avoid contention......Page 312
Number of threads/CPU cores......Page 313
Thread affinity......Page 314
False sharing......Page 315
Summary......Page 316
Chapter 11: Parallel STL......Page 317
Parallel algorithms......Page 318
Naive implementation......Page 319
Performance evaluation......Page 320
Shortcomings of the naive implementation......Page 321
Divide and conquer......Page 323
Performance evaluation......Page 324
Implementing parallel std::count_if......Page 326
Implementing parallel std::copy_if......Page 327
Inner function......Page 328
Approach two – Split algorithm into two parts......Page 329
Part one – Copy elements in parallel into the destination range......Page 330
Part two – Move the sparse range sequentially into a continuous range......Page 331
Performance evaluation......Page 332
Execution policies......Page 333
Parallel policy......Page 334
std::accumulate and std::reduce......Page 335
std::for_each......Page 337
Parallelizing an index-based for-loop......Page 338
Simplifying construction via a wrapper......Page 339
Programmable GPUs......Page 340
Boost Compute......Page 341
Initializing Boost Compute......Page 342
Transfer a simple transform-reduce algorithm to Boost Compute......Page 343
Adapting the circle struct for use with Boost Compute......Page 344
Converting circle_area_cpu to Boost Compute......Page 345
Implementing the transform-reduction algorithm on the GPU......Page 346
Using predicates with Boost Compute......Page 347
Using a custom kernel in Boost Compute......Page 348
Box filter......Page 349
Parallelizing for two dimensions......Page 350
Summary......Page 352
Other Books You May Enjoy......Page 354
Leave a review - let other readers know what you think......Page 356
Index......Page 357

✦ Subjects


Computer Science;Programming


πŸ“œ SIMILAR VOLUMES


C++ High Performance: Master the art of
✍ BjΓΆrn Andrist, Viktor Sehr πŸ“‚ Library πŸ“… 2020 πŸ› Packt Publishing 🌐 English

Code <p><b>A comprehensive guide to help aspiring and professional C++ developers elevate the performance of their apps by allowing them to run faster and consume fewer resources</b></p><h4>Key Features</h4><ul><li>Updated to C++20 with completely revised code and more content on error handling, be

High performance iOS apps: optimize your
✍ Vaish, Gaurav πŸ“‚ Library πŸ“… 2016 πŸ› O'Reilly Media 🌐 English

Part 1. Getting started. Performance in mobile apps -- Part 2. Core optimizations. Memory management -- Energy -- Concurrent programming -- Part 3. iOS performance. Application lifecycle -- User interface -- Network -- Data sharing -- Security -- Part 4. Beyond code. Testing and release -- Tools --

High Performance iOS Apps: Optimize Your
✍ Vaish, Gaurav πŸ“‚ Library πŸ“… 2016 πŸ› O'Reilly Media 🌐 English

Ready to build mobile apps that out-perform the rest? If youre an iOS developer with app-building experience, this practical guide provides tips and best practices to help you solve many common performance issues. Youll learn how to design and optimize iOS apps that deliver a smooth experience even

High Performance iOS Apps: Optimize your
✍ Gaurav Vaish πŸ“‚ Library πŸ“… 2014 πŸ› O'Reilly Media 🌐 English

Now that more people spend more time interacting with mobile apps than with their desktop counterparts, you need to think about your iOS app's performance the moment you write your first line of code. This practical hands-on guide shows you how. Through specific and concise tips for designing and o

High Performance iOS Apps Optimize Your
✍ Gaurav Vaish πŸ“‚ Library πŸ“… 2016 πŸ› O'Reilly Media 🌐 English

You may already have an amazing iOS app, or be developing one. Everything looks great except for some kinks that prohibit users from giving the app that final fifth star or prevent you from releasing it.<br>Issues like a jitter when the user goes to the nth item in table view or the app hogging the