Cover....1
Title Page....2
Copyright and Credits....2
Contributors....4
Table of Contents....6
Preface....12
Chapter 1: About Performance....20
Technical requirements....20
What is performance?....21
Different types of speed....21
Algorithm complexity....22
Big O and Delphi data structures....26
Data structures in practice....30
Mr. Smith’s first program....35
Looking at code through Big O eyes....38
Summary....41
Chapter 2: Profiling the Code....42
Technical requirements....42
Don’t guess, measure!....43
Profiling with TStopwatch....43
Profilers....50
Summary....65
Chapter .3: Fixing the Algorithm....66
Technical requirements....67
Writing responsive user interfaces....67
Updating a progress bar....68
Bulk updates....71
Virtual display....74
Caching....80
Memoization....81
Dynamic cache....83
Speeding up SlowCode....88
Summary....91
Chapter 4: Don’t Reinvent, Reuse....92
Technical requirements....93
Spring for Delphi....93
Enumerations, collections, and lists....94
IEnumerable....96
ICollection....105
IList....106
Other interfaces....109
TEnumerable....109
Stacks and queues....111
Trees....120
Red-black trees....123
Sets and dictionaries....131
ISet....132
IMultiSet....134
IDictionary....136
IBiDiDictionary....139
Summary....143
Chapter 5: Fine-Tuning the Code....144
Technical requirements....145
Delphi compiler settings....145
Code inlining control....146
Optimization....147
Record field alignment....148
Assertions....151
Overflow checking....151
Range checking....152
Extracting common expressions....153
The helpful CPU window....155
Behind the scenes....157
A plethora of types....158
Simple types....159
Strings....159
Arrays....162
Records....165
Classes....176
Interfaces....177
Optimizing method calls....178
Parameter passing....178
Method inlining....186
The magic of pointers....189
Going the assembler way....193
Returning to SlowCode....196
Summary....197
Chapter 6: Memory Management....198
Technical requirements....199
Optimizing strings and array allocations....199
Memory management functions....203
Dynamic record allocation....205
FastMM4 internals....210
Memory allocation in a parallel world....213
Replacing the default memory manager....216
Logging memory manager....217
FastMM4 with release stack....222
FastMM5....225
TBBMalloc....225
Comparing memory managers....227
There is no silver bullet....228
Fine-tuning SlowCode....230
Summary....232
Chapter 7: Getting Started with the Parallel World....234
Technical requirements....235
Processes and threads....235
Multithreading....235
Multitasking....236
When to parallelize code....237
The most common problems....237
Never access the UI from a background thread....238
Simultaneous reading and writing....241
Sharing a variable....243
Hidden behavior....247
Synchronization....249
Critical sections....250
Other locking mechanisms....256
A short note on coding style....262
Shared data with built-in locking....263
Interlocked operations....264
Object life cycle....268
Communication....271
Windows messages....272
Synchronize and Queue....276
Polling....277
Performance....279
Third-party libraries....280
Summary....281
Chapter 8: Working with Parallel Tools....284
Technical requirements....284
TThread....285
Automatic life cycle management....288
Advanced TThread....290
Setting up a communication channel....292
Sending messages from a thread....296
Using TCommThread....298
Implementing a timer....300
Synchronizing with multiple workers....304
WaitForMultipleObjects....306
Condition variables....310
Comparing both approaches....315
Summary....315
Chapter 9: Exploring Parallel Practices....318
Technical requirements....319
Tasks and patterns....319
Variable capturing....320
Tasks....322
Exceptions in tasks....324
Parallelizing a loop....327
Thread pooling....330
Async/Await....332
Join....335
Join/Await....337
Future....342
Parallel for....344
Pipelines....349
Web spider....350
The filter stage....356
The downloader stage....358
The parser stage....359
Summary....362
Chapter 10: More Parallel Patterns....364
Technical requirements....364
Using OmniThreadLibrary....365
Blocking collections....366
Using blocking collections with TThread-based threads....368
Async/Await....371
Join....372
Future....376
Parallel Task....377
Background Worker....381
Initial query....384
Pipeline....389
Creating the pipeline....391
Stages....393
Displaying the result and shutting down....397
Map....398
Timed Task....400
Summary....404
Chapter 11: Using External Libraries....406
Technical requirements....406
Linking with object files....407
Object file formats....409
Object file linking in practice....410
Using C++ libraries....415
Writing exported functions....418
Using a proxy DLL in Delphi....420
Summary....424
Chapter 12: Best Practices....426
About performance....427
Profiling the code....427
Fixing the algorithm....428
Don’t reinvent, reuse....429
Fine-tuning the code....430
Memory management....431
Getting started with the parallel world....432
Working with parallel tools....435
Exploring parallel practices....436
More parallel patterns....437
Using external libraries....438
Final words....439
Index....440
Other Books You May Enjoy....448
Performance matters! Users hate to use programs that are not responsive to interactions or run too slow to be useful. While becoming a programmer is simple enough, you require dedication and hard work to achieve an advanced level of programming proficiency where you know how to write fast code.
This book begins by helping you explore algorithms and algorithmic complexity and continues by describing tools that can help you find slow parts of your code. Subsequent chapters will provide you with practical ideas about optimizing code by doing less work or doing it in a smarter way. The book also teaches you how to use optimized data structures from the Spring4D library, along with exploring data structures that are not part of the standard Delphi runtime library.
The second part of the book talks about parallel programming. You'll learn about the problems that only occur in multithreaded code and explore various approaches to fixing them effectively. The concluding chapters provide instructions on writing parallel code in different ways - by using basic threading support or focusing on advanced concepts such as tasks and parallel patterns.
By the end of this book, you'll have learned to look at your programs from a totally different perspective and will be equipped to effortlessly make your code faster than it is now.
This book is for all Delphi programmers. Whether you're a beginner or an accomplished programmer, you will find something interesting. Even though the focus is on the latest Delphi release, the code uses only standard Delphi syntax without syntactic additions from the latest releases, and most of it should compile and run in any Delphi from XE7 onward. If you're using an older version of Delphi, don't despair! Most of the concepts in this book do not depend on a specific Delphi version and will be useful for everyone.