Download presentation
Presentation is loading. Please wait.
1
Writing High Performance Delphi Application
Primož Gabrijelčič
2
About me Primož Gabrijelčič http://primoz.gabrijelcic.org
programmer, MVP, writer, blogger, consultant, speaker Blog Twitter @thedelphigeek Skype gabr42 LinkedIn gabr42 GitHub gabr42 SO gabr Google+ Primož Gabrijelčič
4
Performance
5
Performance What is performance? How do we “add it to the program”?
There is no silver bullet!
6
What is performance? Running “fast enough” Raw speed Responsiveness
Non-blocking
7
Improving performance
Analyzing algorithms Measuring execution time Fixing algorithms Fine tuning the code Mastering memory manager Writing parallel code Importing libraries
8
Algorithm Complexity
9
Algorithm complexity Tells us how algorithm slows down if data size is increased by a factor of n O() O(n), O(n2), O(n log n) … Time and space complexity
10
O(1) accessing array elements
O(log n) searching in ordered list O(n) linear search O(n log n) quick sort (average) O(n2) quick sort (worst), naive sort (bubblesort, insertion, selection) O(cn) recursive Fibonacci, travelling salesman
11
Comparing complexities
Data size O(1) O(log n) O(n) O(n log n) O(n2) O(cn) 1 10 4 43 100 512 8 764 10.000 1029 300 9 2.769 90.000 1090
12
O(RTL) http://bigocheatsheet.com/ Lists Dictionary Trees access O(1)
Search O(n) / O(n log n) Sort O(n log n) Dictionary * O(1) Search by value O(n) Unordered! Spring4D 1.3? Trees * O(log n) Spring4D 1.2.2
13
Measuring Performance
14
Measuring Manual Automated - Profilers GetTickCount
QueryPerformanceCounter TStopwatch Automated - Profilers Sampling Instrumenting Binary Source
15
Free profilers AsmProfiler Sampling Profiler
Instrumenting and sampling 32-bit Sampling Profiler Sampling
16
Commercial profilers AQTime Nexus Quality Suite ProDelphi
Instrumenting and sampling 32- and 64-bit Nexus Quality Suite Instrumenting ProDelphi Instrumenting (source)
17
Fixing the Algorithm
18
Fixing the algorithm Find a better algorithm
If a part of program is slow, don’t execute it so much If a part of program is slow, don’t execute it at all
19
“Don’t execute it so much”
Don’t update UI thousands of times per second Call BeginUpdate/EndUpdate Don’t send around millions of messages per second
20
“Don’t execute it at all”
UI virtualization Virtual listbox Virtual TreeView Memoization Caching Dynamic programming TGpCache<K,V> O(1) all operations GpLists.pas,
21
Fine Tuning the Code
22
Compiler settings
23
Behind the scenes Strings Arrays Records Classes Interfaces
Reference counted, Copy on write Arrays Static Dynamic Reference counted, Cloned Records Initialized if managed Classes Reference counted on ARC Interfaces Reference counted
24
Calling methods Parameter passing Inlining Dynamic arrays are strange
Single pass compiler!
25
Mastering Memory Manager
26
58 memory managers in one! Image source: ‘Delphi High Performance’
© 2018 Packt Publishing
27
Optimizations Reallocation Allocator locking
Small blocks: New size = at least 2x old size Medium blocks: New size = at least 1.25x old size Allocator locking Small block only Will try 2 ‘larger’ allocators Problem: Freeing memory allocIdx := find best allocator for the memory block repeat if can lock allocIdx then break; Inc(allocIdx); Dec(allocIdx, 2) until false
28
Optimizing parallel allocations
FastMM4 from GitHub DEFINE LogLockContention DEFINE UseReleaseStack
29
Alternatives ScaleMM TBBMalloc https://github.com/andremussche/scalemm
versions-and-freepascal
30
Writing Parallel Code
31
When to parallelize? When other means are exhausted
Pushing long operations into background “Unblock” the user interface Supporting multiple clients in parallel Speeding up the algorithm Hard!
32
Common problems Accessing UI from a background thread
33
“Never access UI from a background thread!”
34
“Never access UI from a background thread!”
35
Common problems Accessing UI from a background thread
Reading/writing shared data Structured data Simple data
36
Synchronization Critical section Spinlock Monitor
Readers-writers / MREW / SWMR TMREWSync / TMultiReadExclusiveWriteSynchronizer Terribly slow Slim Reader/Writer (SRW) Windows only
37
Synchronization problems
Slowdown Deadlocks
38
Interlocked operations
“Microlocking” Faster Limited use
39
Communication
40
Communication Windows messages TThread.Queue polling
TThread.Synchronize too, but … polling
41
Parallel Patterns
42
Patterns Async/Await Join Future Parallel For Pipeline Map Timed task
Parallel task Background worker Fork/Join
43
Async / Await
44
Future
45
Parallel For
46
Pipeline
47
High Performance in a Nutshell
48
Steps to faster code Understand the problem. What do you want to achieve? Find the problematic code. Measure! If possible, find a better algorithm. If that fails, fine tune the code. As a last resort, parallelize the solution. Danger, Will Robinson! Find/write faster code in a different language and link it into the application.
49
It’s Question Time!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.