Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

High Performance Object Pascal Code on Servers (at EKON 22)

538 views

Published on

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.

Published in: Software

High Performance Object Pascal Code on Servers (at EKON 22)

  1. 1. High Performance Pascal Code On Servers Arnaud Bouchez – Synopse / LiveMon High Performance Pascal Code On Servers
  2. 2. High Performance Pascal Code On Servers Arnaud Bouchez – Delphi / FPC • Various solutions (from Vatican to gaming industry) • IoT solution (RSI) • Real-time Monitoring solution (LiveMon) – Open Source • mORMot (SOA ORM MVC framework) • SynPDF SynMustache SynDB SynCrypto – Training and consultancy • Synopse one-man company
  3. 3. High Performance Pascal Code On Servers
  4. 4. High Performance Pascal Code On Servers High Performance Pascal Code On Servers
  5. 5. High Performance Pascal Code On Servers High Performance Pascal Code On Servers • High Performance • On Servers • In Pascal Code
  6. 6. High Performance Pascal Code On Servers High Performance Pascal Code On Servers • High Performance • On Servers • In Pascal Code
  7. 7. High Performance Pascal Code On Servers High Performance Make our Software Great Again (Donald K.)
  8. 8. High Performance Pascal Code On Servers High Performance Make our Software Great Again (attributed to Donald K.)
  9. 9. High Performance Pascal Code On Servers High Performance Premature Optimization is the root of all evil (D. Knuth)
  10. 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. 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. 12. High Performance Pascal Code On Servers High Performance Premature Optimization is the root of all evil (D. Knuth)
  13. 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. 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. 15. High Performance Pascal Code On Servers High Performance Premature Optimization is the root of all evil (D. Knuth - again)
  16. 16. High Performance Pascal Code On Servers High Performance Pascal Code On Servers • High Performance • On Servers • In Pascal Code
  17. 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. 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. 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. 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. 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. 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. 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. 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
  25. 25. High Performance Pascal Code On Servers
  26. 26. High Performance Pascal Code On Servers High Performance Pascal Code On Servers • High Performance • On Servers • In Pascal Code
  27. 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. 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. 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. 30. High Performance Pascal Code On Servers In Pascal Code Not only the Delphi Language • Complementary compilers/transpilers FPC, TMS WebCore, SmartMobileStudio, ElevateWebBuilder…
  31. 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. 32. High Performance Pascal Code On Servers In Pascal Code The Last Resort • Best performance comes from design But code optimization may help
  33. 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. 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. 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. 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. 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. 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. 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. 40. High Performance Pascal Code On Servers Make your Design Fast • Cache is your friend Shared Cache Dedicated Service REDIS Server
  41. 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. 42. High Performance Pascal Code On Servers Make your Code Fast • Processors are very complex beasts https://www.agner.org/optimize
  43. 43. High Performance Pascal Code On Servers Make your Code Fast
  44. 44. High Performance Pascal Code On Servers Make your Code Fast
  45. 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. 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. 47. High Performance Pascal Code On Servers Make your Code Fast • Avoid Unneeded Calls Cache and reuse values
  48. 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. 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. 50. High Performance Pascal Code On Servers Make your Code Fast • Avoid Copies or Reference Counting By using const or var input parameters
  51. 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. 52. High Performance Pascal Code On Servers Make your Code Fast • Better Register Allocation By using a sub-function for loops
  53. 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. 54. High Performance Pascal Code On Servers • Questions? High Performance Pascal Code On Servers

×