This EKON 22 conference is about high performance on servers, written in the object pascal (Delphi / FPC) language. Profiling should be the first step to avoid premature optimization, which is the root of all evil (Knuth). But when some bottlenecks are identified, we introduce some simple architecture patterns (like caching or microservices), data structures and algorithms to make process actually faster, with minimal refactoring. It was a fun session about how to write faster code, ending up by looking at the Delphi CPU view – even if you don’t know assembly.
5. High Performance Pascal Code On Servers
High Performance
Pascal Code On Servers
• High Performance
• On Servers
• In Pascal Code
6. High Performance Pascal Code On Servers
High Performance
Pascal Code On Servers
• High Performance
• On Servers
• In Pascal Code
7. High Performance Pascal Code On Servers
High Performance
Make our Software
Great Again
(Donald K.)
8. High Performance Pascal Code On Servers
High Performance
Make our Software
Great Again
(attributed to Donald K.)
9. High Performance Pascal Code On Servers
High Performance
Premature Optimization
is the root of all evil
(D. Knuth)
10. High Performance Pascal Code On Servers
High Performance
"Programmers waste enormous amounts of time
thinking about, or worrying about, the speed of
noncritical parts of their programs, and these
attempts at efficiency actually have a strong
negative impact when debugging and
maintenance are considered. We should forget
about small efficiencies, say about 97% of the
time: premature optimization is the root of all
evil. Yet we should not pass up our opportunities
in that critical 3%."
11. High Performance Pascal Code On Servers
High Performance
"Programmers waste enormous amounts of time
thinking about, or worrying about, the speed of
noncritical parts of their programs, and these
attempts at efficiency actually have a strong
negative impact when debugging and
maintenance are considered. We should forget
about small efficiencies, say about 97% of the
time: premature optimization is the root of all
evil. Yet we should not pass up our opportunities
in that critical 3%."
12. High Performance Pascal Code On Servers
High Performance
Premature Optimization
is the root of all evil
(D. Knuth)
13. High Performance Pascal Code On Servers
High Performance
No Premature Optimization
1. Make it right, then make it fast
Unit tests, then performance tests
2. Be Lazy
Change only what is worth optimizing
14. High Performance Pascal Code On Servers
High Performance
No Premature Optimization
1. Testing is everything
to avoid regression when refactoring
2. Measure, don’t guess
Use a profiler and/or performance logs
15. High Performance Pascal Code On Servers
High Performance
Premature Optimization
is the root of all evil
(D. Knuth - again)
16. High Performance Pascal Code On Servers
High Performance
Pascal Code On Servers
• High Performance
• On Servers
• In Pascal Code
17. High Performance Pascal Code On Servers
On Servers
Not your daddy’s RAD way
• RAD is database centric
creating proper indexes won’t hurt
• RAD is for rich clients
(a lot of) process / business logic
is done on the client side
18. High Performance Pascal Code On Servers
On Servers
REpresentational State Transfer (REST)
• It is not easy to switch from RAD to REST
but from proper isolation
comes ( may/should come? )
better performance
19. High Performance Pascal Code On Servers
On Servers
Scaling
• Guess your needs, prepare for eventual
Scaling is (really) hard
Your company may not need GAFA scaling
But you want to maximize ROI
20. High Performance Pascal Code On Servers
On Servers
Scaling
• Scaling is about uncoupling
Get rid of identified Bottlenecks
and Single Point of Failures
21. High Performance Pascal Code On Servers
On Servers
Scaling
• Database as Bottleneck
from Vertical Scaling
more CPU, RAM, switch to NVMe…
to Horizontal Scaling
add Microservices instances
with their own storage
22. High Performance Pascal Code On Servers
On Servers
Scaling
• MicroServices with Local Storage
Using Containers for API and DB servers
(RDMS, NoSQL – MongoDB, Redis)
Using stand-alone database
(objects, SQLite3)
23. High Performance Pascal Code On Servers
On Servers
Scaling
• MicroServices with Local Storage
Works as a local cache of data
Updates made via Events
24. High Performance Pascal Code On Servers
On Servers
Scaling
• MicroServices with Local Storage
Updates made via Events
leads to Eventual Consistency
Use a centralized DB for transactional data
26. High Performance Pascal Code On Servers
High Performance
Pascal Code On Servers
• High Performance
• On Servers
• In Pascal Code
27. High Performance Pascal Code On Servers
In Pascal Code
From Existing: Reuse, Test and Publish
• Reuse years (decades) of proven code
• Opportunity to add/enhance testing
• Open your solution to 3rd parties clients
28. High Performance Pascal Code On Servers
In Pascal Code
For new projects: Object Pascal
• Object Pascal is
readable (by nature)
safe (strong typing)
abstract (strings, interfaces)
fast (native/compiled, no GC)
29. High Performance Pascal Code On Servers
In Pascal Code
For new projects: Object Pascal
• Object Pascal is
your favorite / most fluent language
and you know your company business
30. High Performance Pascal Code On Servers
In Pascal Code
Not only the Delphi Language
• Complementary compilers/transpilers
FPC, TMS WebCore,
SmartMobileStudio, ElevateWebBuilder…
31. High Performance Pascal Code On Servers
In Pascal Code
Not only the Delphi Language
• Complementary compilers/transpilers
low-level optimizations usually work
for a single target compiler only
JavaScript execution VM/JIT is very specific
32. High Performance Pascal Code On Servers
In Pascal Code
The Last Resort
• Best performance comes from design
But code optimization may help
33. High Performance Pascal Code On Servers
In Pascal Code
Make it Fast?
• Make it right
then
• Make your Design Fast
• Make your Code Fast
34. High Performance Pascal Code On Servers
High Performance
Pascal Code On Servers
• High Performance
• On Servers
• In Pascal Code
Make your design fast
Make your code fast
35. High Performance Pascal Code On Servers
Make your Design Fast
• Measure against your company goals
Identify the bottlenecks
- Profiling (on dev/coder side)
- Performance Logging (client/prod side)
36. High Performance Pascal Code On Servers
Make your Design Fast
• Profiling
External Tools (AQTime, DelphiTools…)
Running on real-live usecases
using automated load stress tests
37. High Performance Pascal Code On Servers
Make your Design Fast
• Client/prod side performance logging
Log Timing of
Database Statements
SOA/REST methods calls
Main identified bottleneck functions
into logs – as text or DB (SQLite3)
38. High Performance Pascal Code On Servers
Make your Design Fast
• Client/prod side performance logging
Log Timing as part of mORMot
SQL, SOA on multiple threads
→ show some real logs
39. High Performance Pascal Code On Servers
Make your Design Fast
• Cache is your friend
In-process Cache
MicroService local DB
preloading the most used values
mORMot TSynDictionary in RAM
40. High Performance Pascal Code On Servers
Make your Design Fast
• Cache is your friend
Shared Cache
Dedicated Service
REDIS Server
41. High Performance Pascal Code On Servers
High Performance
Pascal Code On Servers
• High Performance
• On Servers
• In Pascal Code
Make your design fast
Make your code fast
42. High Performance Pascal Code On Servers
Make your Code Fast
• Processors are very complex beasts
https://www.agner.org/optimize
45. High Performance Pascal Code On Servers
Make your Code Fast
Shut down the profiler,
keep your regression tests close,
and let’s begin the fun !
(from the most obvious
to the most tenuous)
46. High Performance Pascal Code On Servers
Make your Code Fast
Shut down the profiler,
keep your regression tests close,
and let’s begin the fun !
(and don’t take my word for it)
47. High Performance Pascal Code On Servers
Make your Code Fast
• Avoid Unneeded Calls
Cache and reuse values
48. High Performance Pascal Code On Servers
Make your Code Fast
• Use Tuned Libraries
Written with performance in mind
Hardware accelerated (AES-NI, crc32c)
Link some C code known for efficiency
49. High Performance Pascal Code On Servers
Make your Code Fast
• Avoid Memory Allocation
Heap allocation has a cost,
especially on multi-threaded applications
50. High Performance Pascal Code On Servers
Make your Code Fast
• Avoid Copies or Reference Counting
By using const or var input parameters
51. High Performance Pascal Code On Servers
Make your Code Fast
• Avoid Hidden try..finally Blocks
By using a sub-function
when managed types are involved
52. High Performance Pascal Code On Servers
Make your Code Fast
• Better Register Allocation
By using a sub-function for loops
53. High Performance Pascal Code On Servers
Make your Code Fast
• Faster Data Structures
(Dynamic) arrays put all data in a row
are likely to stay in L1 cache
can be accessed using TDynArray wrapper
54. High Performance Pascal Code On Servers
• Questions?
High Performance
Pascal Code On Servers