Results 1 to 4 of 4

Thread: Why does QDateTime allocate its internal QDate and QTime pair on the heap?

  1. #1
    Join Date
    Jul 2017
    Posts
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Why does QDateTime allocate its internal QDate and QTime pair on the heap?

    Hi all,

    I am using QDateTime extensively in a series of procedures that need to run as fast as possible, under 250 milliseconds in total, therefore I really want to avoid heap allocations as much as possible.

    However, I just realized that QDateTime allocates its internals on the heap, sharing them with other QDateTime objects until they are modified. This might be fine when the datetime object gets just passed around, but it seems overkill when lots of operations have to be done on it, resulting in multiple temporary copies and therefore multiple temporary heap allocations.

    To be totally honest I've not done any extensive profiling to determine whether this is in reality an issue, but just in case I find that the heap allocations really waste too much time, what obstacles do you think there would be in building a QDateTime-alike class that embedded directly in itself a pair of QDate and QTime objects, providing the same interface as QDateTime's?

    Has this ever been done before?

  2. #2
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Why does QDateTime allocate its internal QDate and QTime pair on the heap?

    Allocating the internals on the heap has the advantage that the member variables can be modified or extended without breaking the ABI. This so called D-Pointer-pattern is actually used by a lot of classes in the Qt framework.

    In theory is would be possible to implement any of these classes without using that pattern. It is really just about ABI compatibility.

    Has this ever been done before?
    I once created my own DateTime and TimeSpan classes: https://github.com/Martchus/cpp-util.../master/chrono
    It is very lightweight because it uses only a 64-bit int and no further heap allocations. However, its interface is different from QDateTime.

  3. #3
    Join Date
    Jul 2017
    Posts
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Why does QDateTime allocate its internal QDate and QTime pair on the heap?

    Allocating the internals on the heap has the advantage that the member variables can be modified or extended without breaking the ABI. This so called D-Pointer-pattern is actually used by a lot of classes in the Qt framework.
    Yeah, I'm aware of that pattern in general, it just seems overkill in this case, given that QDateTime is a little more than an aggregate of a QDate and QTime, and that QDate and QTime themselves don't use such a pattern: is the ABI really important in this case? Why?

    Thanks for the pointer to your code!

  4. #4
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Why does QDateTime allocate its internal QDate and QTime pair on the heap?

    If you look at the code QDateTime is a little bit more complex:

    Qt Code:
    1. qint64 m_msecs;
    2. StatusFlags m_status;
    3. int m_offsetFromUtc;
    4. mutable QAtomicInt ref;
    5. #if QT_CONFIG(timezone)
    6. QTimeZone m_timeZone;
    7. #endif // timezone
    To copy to clipboard, switch view to plain text mode 

    The QDateTime class with all its additional features like status and time zone information seems to be complex enough for Qt developers to assume that future changes might require modifying member variables. Hence they chose the D-Pointer pattern to allow those changes without breaking ABI. Preserving the ABI is important for all parts of the Qt libs (except some 'private' parts, eg. the API for writing QPA-plugins).

    However, I also find that QDateTime class is a bit of an overkill. Additionally, it lacks a 'TimeSpan' class. So I usually use my own classes which I actually wrote to learn C++.

Similar Threads

  1. QTime and QDate from String
    By Eleasar in forum Qt Programming
    Replies: 4
    Last Post: 23rd May 2015, 23:07
  2. Casting QDate to QDateTime
    By alitoh in forum Qt Programming
    Replies: 3
    Last Post: 26th August 2011, 01:22
  3. Replies: 2
    Last Post: 8th February 2011, 14:53
  4. View QDate and QDateTime in VisualStudio debugger.
    By r2d2u2 in forum Qt Programming
    Replies: 1
    Last Post: 10th July 2010, 17:23
  5. Extending QDate, QTime and QDateTime
    By jamadagni in forum Qt Programming
    Replies: 8
    Last Post: 24th March 2006, 15:51

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.