Hands-On High Performance Programming with Qt 5

Book description

Build efficient and fast Qt applications, target performance problems, and discover solutions to refine your code

Key Features

  • Build efficient and concurrent applications in Qt to create cross-platform applications
  • Identify performance bottlenecks and apply the correct algorithm to improve application performance
  • Delve into parallel programming and memory management to optimize your code

Book Description

Achieving efficient code through performance tuning is one of the key challenges faced by many programmers. This book looks at Qt programming from a performance perspective. You'll explore the performance problems encountered when using the Qt framework and means and ways to resolve them and optimize performance.

The book highlights performance improvements and new features released in Qt 5.9, Qt 5.11, and 5.12 (LTE). You'll master general computer performance best practices and tools, which can help you identify the reasons behind low performance, and the most common performance pitfalls experienced when using the Qt framework. In the following chapters, you'll explore multithreading and asynchronous programming with C++ and Qt and learn the importance and efficient use of data structures. You'll also get the opportunity to work through techniques such as memory management and design guidelines, which are essential to improve application performance. Comprehensive sections that cover all these concepts will prepare you for gaining hands-on experience of some of Qt's most exciting application fields - the mobile and embedded development domains.

By the end of this book, you'll be ready to build Qt applications that are more efficient, concurrent, and performance-oriented in nature

What you will learn

  • Understand classic performance best practices
  • Get to grips with modern hardware architecture and its performance impact
  • Implement tools and procedures used in performance optimization
  • Grasp Qt-specific work techniques for graphical user interface (GUI) and platform programming
  • Make Transmission Control Protocol (TCP) and Hypertext Transfer Protocol (HTTP) performant and use the relevant Qt classes
  • Discover the improvements Qt 5.9 (and the upcoming versions) holds in store
  • Explore Qt's graphic engine architecture, strengths, and weaknesses

Who this book is for

This book is designed for Qt developers who wish to build highly performance applications for desktop and embedded devices. Programming Experience with C++ is required.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On High Performance Programming with Qt 5
  3. Dedication
  4. About Packt
    1. Why subscribe?
    2. Packt.com
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. Understanding Performant Programs
    1. Why performance is important
      1. The price of performance optimization
    2. Traditional wisdom and basic guidelines
      1. Avoiding repeated computation
      2. Avoiding paying the high price
      3. Avoiding copying data around
      4. General performance optimization approach
    3. Modern processor architectures
      1. Caches
      2. Pipelining
        1. Speculative execution and branch prediction
        2. Out-of-order execution
      3. Multicore
      4. Additional instruction sets
      5. Impact on performance
        1. Keeping your caches hot
        2. Don't confuse your branch predictor
        3. Parallelizing your application
    4. Summary
    5. Questions
    6. Further reading
  8. Profiling to Find Bottlenecks
    1. Types of profilers
      1. Instrumenting profilers
      2. Sampling profilers
      3. External counters
        1. Note on Read Time-Stamp Counter
    2. Platform and tools
      1. Development environment
      2. Profiling tools
        1. Just use gprof?
        2. Windows system tools
        3. Program profiling tools
        4. Visualizing performance data
      3. Memory tools
    3. Profiling CPU usage
      1. Poor man's sampling technique
      2. Using Qt Creator's QML profiler
      3. Using standalone CPU profilers
        1. Reiterating sampling profiling's limitations
    4. Investigating memory usage
      1. Poor man's memory profiling
      2. Using Qt Creator's heob integration
    5. Manual instrumentation and benchmarks
      1. Debug outputs
      2. Benchmarks
        1. Benchmarks in regression testing
      3. Manual instrumentation
    6. Further advanced tools
      1. Event Tracing for Windows (ETW) and xperf
        1. Installation
        2. Recording and visualizing traces
        3. Conclusion
      2. GammaRay
        1. Building GammaRay
        2. When can we use it?
      3. Other tools
        1. Graphic profilers
        2. Commercial Intel tools
        3. Visual Studio tools
    7. Summary
    8. Questions
  9. Deep Dive into C++ and Performance
    1. C++ philosophy and design
      1. Problems with exceptions
        1. Run-time overheads
        2. Non-determinism
        3. RTTI
        4. Conclusion
      2. Virtual functions
    2. Traditional C++ optimizations
      1. Low-hanging fruit
      2. Temporaries
        1. Return values and RVO
        2. Conversions
      3. Memory management
        1. Basic truths
        2. Replacing the global memory manager
        3. Custom memory allocators
          1. Where they do make sense
          2. Stack allocators
          3. Conclusion
        4. Custom STL allocators
      4. Template trickery
        1. Template computations
        2. Expression templates
        3. CRTP for static polymorphism
        4. Removing branches
    3. C++11/14/17 and performance
      1. Move semantics
        1. Passing by value fashionable again
      2. Compile time computations
      3. Other improvements
    4. What your compiler can do for you
      1. Examples of compiler tricks
      2. More on compiler optimizations
        1. Inlining of functions
        2. Loop unrolling and vectorization
      3. What compilers do not like
        1. Aliasing
        2. External functions
      4. How can you help the compiler?
        1. Profile Guided Optimization
      5. When compilers get overzealous
    5. Optimization tools beyond compiler
      1. Link time optimization and link time code generation
      2. Workaround – unity builds
      3. Beyond linkers
    6. Summary
    7. Questions
    8. Further reading
  10. Using Data Structures and Algorithms Efficiently
    1. Algorithms, data structures, and performance
      1. Algorithm classes
        1. Algorithmic complexity warning
      2. Types of data structures
        1. Arrays
        2. Lists
        3. Trees
        4. Hash tables
    2. Using Qt containers
      1. General design
        1. Implicit sharing
        2. Relocatability
      2. Container classes overview
        1. Basic Qt containers
        2. QList
        3. QVarLengthArray
        4. QCache
      3. C++11 features
      4. Memory management
      5. Should we use Qt containers?
    3. Qt algorithms, iterators, and gotchas
      1. Iterators and iterations
      2. Gotcha - accidental deep copies
    4. Working with strings
      1. Qt string classes
        1. QByteArray
        2. QString
        3. QStringBuilder
        4. Substring classes
      2. More string advice
        1. Interning
        2. Hashing
        3. Searching substrings
        4. Fixing the size
    5. Optimizing with algorithms and data structures
      1. Optimizing with algorithms
        1. Reusing other people's work
      2. Optimizing with data structures
        1. Be cache-friendly
        2. Flatten your data structures
        3. Improve access patterns
          1. Structure of arrays
          2. Polymorphism avoidance
          3. Hot-cold data separation
          4. Use a custom allocator
        4. Fixed size containers
        5. Write your own
    6. Summary
    7. Questions
    8. Further reading
  11. An In-Depth Guide to Concurrency and Multithreading
    1. Concurrency, parallelism, and multithreading
      1. Problems with threads
      2. More problems – false sharing
    2. Threading support classes in Qt
      1. Threads
      2. Mutexes
      3. Condition variables
      4. Atomic variables
      5. Thread local storage
      6. Q_GLOBAL_STATIC
    3. Threads, events, and QObjects
      1. Events and event loop
      2. QThreads and object affinities
        1. Getting rid of the QThread class
      3. Thread safety of Qt objects
    4. Higher level Qt concurrency mechanisms
      1. QThreadPool
      2. QFuture
      3. QFutureInterface
        1. Should we use it?
      4. Map, filter, and reduce
      5. Which concurrency class should I use?
    5. Multithreading and performance
      1. Costs of multithreading
        1. Thread costs
        2. Synchronization costs
          1. QMutex implementation and performance
        3. Atomic operation costs
        4. Memory allocation costs
        5. Qt's signals and slots performance
      2. Speeding up programs with threads
        1. Do not block the GUI thread
        2. Use the correct number of threads
        3. Avoid thread creation and switching cost
        4. Avoid locking costs
          1. Fine-grained locks
          2. Lock coarsening
          3. Duplicate or partition resources
          4. Use concurrent data structures
          5. Know your concurrent access patterns
          6. Do not share any data
          7. Double-checked locking and a note on static objects
        5. Just switch to lock-free and be fine?
          1. Lock-free performance
          2. Progress guarantees
        6. Messing with thread scheduling?
        7. Use a share nothing architecture
          1. Implementing a worker thread
          2. Active object pattern
          3. Command queue pattern
    6. Beyond threading
      1. User-space scheduling
      2. Transactional memory
      3. Continuations
      4. Coroutines
    7. Summary
    8. Questions
    9. Further reading
  12. Performance Failures and How to Overcome Them
    1. Linear search storm
      1. Context
      2. Problem
      3. Solution
      4. Conclusion
    2. Results dialog window opening very slowly
      1. Context
      2. Problem
      3. Solution
      4. Conclusion
    3. Increasing HTTP file transfer times
      1. Context
      2. Problem
      3. Solution
      4. Conclusion
    4. Loading SVGs
      1. Context
      2. Problem
      3. Solution
      4. Conclusion
    5. Quadratic algorithm trap
      1. Context
      2. Problem
      3. Solution
      4. Conclusion
    6. Stalls when displaying widget with QML contents
      1. Context
      2. Problem
      3. Solution
      4. Conclusion
    7. Too many items in view
      1. Context
      2. Problem
      3. Solution
      4. Conclusion
    8. Two program startup stories
      1. Time system calls
      2. Font cache
      3. Conclusion
    9. Hardware shutting down after an error message
      1. Context
      2. Problem
      3. Solution
      4. Conclusion
    10. Overly generic design
      1. Context
      2. Problem
      3. Solution
      4. Conclusion
    11. Other examples
    12. Summary
    13. Questions
    14. Further reading
  13. Understanding I/O Performance and Overcoming Related Problems
    1. Reading and writing files in Qt
      1. Basics of file I/O performance
        1. Buffering and flushing
        2. Tied and synchronized streams
        3. Reading and writing
        4. Seeking
        5. Caching files
      2. Qt's I/O classes
        1. QFile
        2. QTextStream and QDataStream
        3. Other helper I/O classes
        4. QDebug and friends
    2. Parsing XML and JSON at the speed of light
      1. QtXml classes
        1. QDomDocument
        2. QXmlSimpleReader
      2. New stream classes in QtCore
      3. Quick parsing of XML
      4. Reading JSON
        1. QJsonDocument's performance
    3. Connecting databases
      1. Basic example using SQLite
      2. Some performance considerations
    4. More about operating system interactions
      1. Paging, swapping, and the TLB
      2. Reading from disk
      3. Completion ports
    5. Summary
    6. Questions
    7. Further reading
  14. Optimizing Graphical Performance
    1. Introduction to graphics performance
      1. Graphics hardware's inner workings
      2. What is a GPU?
      3. OpenGL pipeline model
      4. Performance of the graphics pipeline
        1. CPU problems
        2. Data transfer optimization
        3. Costly GPU operations
      5. Newer graphics programming APIs
    2. Qt graphics architecture and its history
      1. The graphics API Zoo
        1. Qt Widget
        2. QGraphicalView
        3. QOpenGLWidget
        4. QVulkanWindow
        5. Qt Quick
        6. QtQuick Controls 1 and 2
        7. Extending QML
          1. Canvas 2D
          2. QQuickPaintedItem
          3. QQuickItem
          4. QQuickFrameBufferObject
          5. More APIs
        8. Qt 3D
      2. OpenGL drivers and Qt
        1. Graphic drivers and performance
        2. Setting the OpenGL implementation for QML
    3. Qt Widget's performance
      1. QPainter
        1. Images
        2. Optimized calls
      2. OpenGL rendering with QOpenGLWidget
        1. Images
        2. Threading and context sharing
        3. Usage of QPainter
      3. QGraphicsView
      4. Model/view framework
    4. QML performance
      1. Improvements in 5.9 and beyond
      2. Measuring QML performance
      3. Startup of a QML application
      4. QML rendering
        1. Scene graph optimizations
        2. Scene graph and threading
        3. Scene graph performance gotchas
          1. Batching
          2. Texture atlas
          3. Occlusion, blending, and other costly operations
          4. Antialiasing
          5. Use caching
        4. Which QML custom item should you choose?
        5. JavaScript usage
        6. Qt Quick Controls
    5. Other modules
      1. Qt 3D performance
      2. Hybrid web applications
    6. Summary
    7. Questions
    8. Further reading
  15. Optimizing Network Performance
    1. Introduction to networking
      1. Transport layer
        1. User Datagram Protocol (UDP)
        2. Transmission Control Protocol (TCP)
        3. A better TCP?
      2. Application layer
        1. Domain Name Service (DNS)
        2. HyperText Transfer Protocol (HTTP)
        3. Secure data transfer
        4. A better HTTP?
    2. Qt networking classes
      1. TCP and UDP networking classes
        1. QTcpServer and QTcpSocket
        2. QUdpSocket
        3. QAbstractSocket
        4. QSslSocket
        5. Other socket types
      2. HTTP networking using Qt classes
        1. DNS queries
        2. Basic HTTP
        3. HTTPS and other extensions
        4. Qt WebSocket classes
        5. Miscallaneous classes
      3. Other higher-level communication classes
        1. Qt WebChannel
        2. Qt WebGL streaming
        3. Qt remote objects
    3. Improving network performance
      1. General network performance techniques
      2. Receiving buffers and copying
      3. TCP performance
      4. HTTP and HTTPS performance
        1. Connection reuse
        2. Resuming SSL connections
        3. Preconnecting
        4. Pipelining
        5. Caching and compression
        6. Using HTTP/2 and WebSocket
    4. Advanced networking themes
    5. Summary
    6. Questions
    7. Further reading
  16. Qt Performance on Embedded and Mobile Platforms
    1. Challenges in embedded and mobile development
      1. Basic performance themes
      2. Run to idle
      3. Some hardware data
      4. Embedded hardware and performance
    2. Qt usage in embedded and mobile worlds
      1. Qt for embedded
        1. Qt usage on embedded Linux
        2. Qt's embedded tooling
        3. Supported hardware
        4. Example usage with Raspberry Pi
      2. Qt for mobile
        1. Android support in Qt Creator
        2. Profiling Android applications
        3. Mobile APIs in Qt
    3. Embedded Linux and Qt performance
      1. Executable size
      2. Minimizing assets
      3. Power consumption
      4. Start-up time
        1. Using the current Qt version
        2. Using loaders
        3. 3D asset conditioning
        4. Linux start-up optimizations
        5. Hardware matters
      5. Graphical performance
      6. Time series chart display
        1. Qt Charts and OpenGL acceleration
        2. Polyline simplifications
      7. Floating-point considerations
    4. Mobile-specific performance concerns
      1. Executable size
      2. Power usage
      3. Mobile networking
        1. Batch and piggyback
        2. Consider a push model
        3. Prefetch data
        4. Reuse connections
        5. Adapting to the current network connection type
      4. Graphic hardware
    5. Summary
    6. Questions
    7. Further reading
  17. Testing and Deploying Qt Applications
    1. Testing of Qt code
      1. Unit testing
        1. Qt Test
        2. Test support in Qt Creator
      2. Automated GUI testing
        1. Squish
        2. Example Squish test
      3. Performance regression testing
        1. Adding a qmlbench benchmark
        2. Using Squish
    2. Deploying Qt applications
      1. Flying parts
      2. Static versus dynamic builds
      3. Deploying on Windows
        1. Windows deployment tool
        2. Installation and paths
    3. Summary
    4. Questions
    5. Further reading
  18. Assessments
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
    10. Chapter 10
    11. Chapter 11
  19. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On High Performance Programming with Qt 5
  • Author(s): Marek Krajewski
  • Release date: January 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781789531244