Delphi High Performance. - Second Edition

Book description

Master Siemens TIA toolbox tools for efficient PLC and HMI code writing. Gain insights into algorithm performance, Delphi language intricacies, and program optimizations to enhance your skills effectively.

Key Features

  • Discover external programming libraries that will speed up your programming and code
  • Learn to integrate external libraries into Delphi programs
  • Build fast Delphi applications using concurrency, parallel programming, and memory management

Book Description

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.

What you will learn

  • Get to grips with algorithmic complexity and learn how to recognize it
  • Use tools to determine program runtime behavior
  • Speed up programs by doing less instead of more
  • Discover the internal workings of Delphi data structures
  • Gain an understanding of Delphi’s memory manager
  • Find out how to write low-level parallel programs with TThread
  • Use parallel patterns from the PPL and OTL libraries to write fast code
  • Include external code, written in C or C++, in Delphi programs

Who this book is for

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.

Table of contents

  1. Delphi High Performance
  2. Contributors
  3. About the author
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
    9. Download a free PDF copy of this book
  6. Chapter 1: About Performance
    1. Technical requirements
    2. What is performance?
      1. Different types of speed
    3. Algorithm complexity
      1. Big O and Delphi data structures
      2. Data structures in practice
      3. Mr. Smith’s first program
      4. Looking at code through Big O eyes
    4. Summary
  7. Chapter 2: Profiling the Code
    1. Technical requirements
    2. Don’t guess, measure!
      1. Profiling with TStopwatch
      2. Profilers
    3. Summary
  8. Chapter 3: Fixing the Algorithm
    1. Technical requirements
    2. Writing responsive user interfaces
      1. Updating a progress bar
      2. Bulk updates
      3. Virtual display
    3. Caching
      1. Memoization
      2. Dynamic cache
    4. Speeding up SlowCode
    5. Summary
  9. Chapter 4: Don’t Reinvent, Reuse
    1. Technical requirements
    2. Spring for Delphi
    3. Enumerations, collections, and lists
      1. IEnumerable<T>
      2. ICollection<T>
      3. IList<T>
      4. Other interfaces
      5. TEnumerable
    4. Stacks and queues
    5. Trees
      1. Red-black trees
    6. Sets and dictionaries
      1. ISet<T>
      2. IMultiSet<T>
    7. IDictionary<TKey, TValue>
      1. IBiDiDictionary<TKey, TValue>
    8. Summary
  10. Chapter 5: Fine-Tuning the Code
    1. Technical requirements
    2. Delphi compiler settings
      1. Code inlining control
      2. Optimization
      3. Record field alignment
      4. Assertions
      5. Overflow checking
      6. Range checking
    3. Extracting common expressions
    4. The helpful CPU window
    5. Behind the scenes
      1. A plethora of types
      2. Simple types
      3. Strings
      4. Arrays
      5. Records
      6. Classes
      7. Interfaces
    6. Optimizing method calls
      1. Parameter passing
      2. Method inlining
    7. The magic of pointers
    8. Going the assembler way
    9. Returning to SlowCode
    10. Summary
  11. Chapter 6: Memory Management
    1. Technical requirements
    2. Optimizing strings and array allocations
    3. Memory management functions
    4. Dynamic record allocation
    5. FastMM4 internals
    6. Memory allocation in a parallel world
    7. Replacing the default memory manager
      1. Logging memory manager
      2. FastMM4 with release stack
      3. FastMM5
      4. TBBMalloc
      5. Comparing memory managers
      6. There is no silver bullet
    8. Fine-tuning SlowCode
    9. Summary
  12. Chapter 7: Getting Started with the Parallel World
    1. Technical requirements
    2. Processes and threads
      1. Multithreading
      2. Multitasking
    3. When to parallelize code
    4. The most common problems
      1. Never access the UI from a background thread
      2. Simultaneous reading and writing
      3. Sharing a variable
      4. Hidden behavior
    5. Synchronization
      1. Critical sections
      2. Other locking mechanisms
      3. A short note on coding style
      4. Shared data with built-in locking
      5. Interlocked operations
      6. Object life cycle
    6. Communication
      1. Windows messages
      2. Synchronize and Queue
      3. Polling
      4. Performance
    7. Third-party libraries
    8. Summary
  13. Chapter 8: Working with Parallel Tools
    1. Technical requirements
    2. TThread
      1. Automatic life cycle management
      2. Advanced TThread
    3. Setting up a communication channel
      1. Sending messages from a thread
      2. Using TCommThread
      3. Implementing a timer
    4. Synchronizing with multiple workers
      1. WaitForMultipleObjects
      2. Condition variables
      3. Comparing both approaches
    5. Summary
  14. Chapter 9: Exploring Parallel Practices
    1. Technical requirements
    2. Tasks and patterns
    3. Variable capturing
    4. Tasks
      1. Exceptions in tasks
      2. Parallelizing a loop
      3. Thread pooling
    5. Async/Await
    6. Join
    7. Join/Await
    8. Future
    9. Parallel for
    10. Pipelines
      1. Web spider
      2. The filter stage
      3. The downloader stage
      4. The parser stage
    11. Summary
  15. Chapter 10: More Parallel Patterns
    1. Technical requirements
    2. Using OmniThreadLibrary
    3. Blocking collections
      1. Using blocking collections with TThread-based threads
    4. Async/Await
    5. Join
    6. Future
    7. Parallel Task
    8. Background Worker
      1. Initial query
    9. Pipeline
      1. Creating the pipeline
      2. Stages
      3. Displaying the result and shutting down
    10. Map
    11. Timed Task
    12. Summary
  16. Chapter 11: Using External Libraries
    1. Technical requirements
    2. Linking with object files
      1. Object file formats
      2. Object file linking in practice
    3. Using C++ libraries
      1. Writing exported functions
      2. Using a proxy DLL in Delphi
    4. Summary
  17. Chapter 12: Best Practices
    1. About performance
    2. Profiling the code
    3. Fixing the algorithm
    4. Don’t reinvent, reuse
    5. Fine-tuning the code
    6. Memory management
    7. Getting started with the parallel world
    8. Working with parallel tools
    9. Exploring parallel practices
    10. More parallel patterns
    11. Using external libraries
    12. Final words
  18. Index
    1. Why subscribe?
  19. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Delphi High Performance. - Second Edition
  • Author(s): Primož Gabrijelčič
  • Release date: June 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781805125877