From 050928ae69c95018718fd77084aa70fa26a033a3 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Wed, 26 Jan 2011 15:09:47 +0100 Subject: Replace the handcoded math and change the timer buckets again. Reduce from 8 to 6 buckets and increase the step between each bucket. This way, the second bucket is now of 224 timers, which should be enough for 99.9% of the applications. Also change the hardcoded math to calculations using enum values. This helps in changing the timer buckets again in the future. Also fix the last bucket not to have a timer ID of 16777216, as that is not valid. Reviewed-by: Olivier Goffart --- src/corelib/kernel/qabstracteventdispatcher.cpp | 51 +++++++++++++++++++------ 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/src/corelib/kernel/qabstracteventdispatcher.cpp b/src/corelib/kernel/qabstracteventdispatcher.cpp index 06b76c9..2949cd0 100644 --- a/src/corelib/kernel/qabstracteventdispatcher.cpp +++ b/src/corelib/kernel/qabstracteventdispatcher.cpp @@ -52,28 +52,48 @@ QT_BEGIN_NAMESPACE static const int TimerIdMask = 0x00ffffff; static const int TimerSerialMask = ~TimerIdMask & ~0x80000000; static const int TimerSerialCounter = TimerIdMask + 1; -static const int MaxTimerId = TimerSerialCounter - 1; - -enum { NumberOfBuckets = 8, FirstBucketSize = 32 }; - -static const int BucketSize[NumberOfBuckets] = - { 32, 64, 512, 4096, 32768, 262144, 2097152, 16777216 - 2364000 }; -static const int BucketOffset[NumberOfBuckets] = - { 0, 32, 96, 608, 4704, 37448, 266848, 2364000 }; +static const int MaxTimerId = TimerIdMask; static int FirstBucket[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 }; -static QBasicAtomicPointer timerIds[NumberOfBuckets] = +enum { + FirstBucketOffset = 0, + SecondBucketOffset = sizeof(FirstBucket) / sizeof(FirstBucket[0]), + ThirdBucketOffset = 0x100, + FourthBucketOffset = 0x1000, + FifthBucketOffset = 0x10000, + SixthBucketOffset = 0x100000 +}; + +enum { + FirstBucketSize = SecondBucketOffset, + SecondBucketSize = ThirdBucketOffset - SecondBucketOffset, + ThirdBucketSize = FourthBucketOffset - ThirdBucketOffset, + FourthBucketSize = FifthBucketOffset - FourthBucketOffset, + FifthBucketSize = SixthBucketOffset - FifthBucketOffset, + SixthBucketSize = MaxTimerId - SixthBucketOffset +}; + +static const int BucketSize[] = { + FirstBucketSize, SecondBucketSize, ThirdBucketSize, + FourthBucketSize, FifthBucketSize, SixthBucketSize +}; +enum { NumberOfBuckets = sizeof(BucketSize) / sizeof(BucketSize[0]) }; + +static const int BucketOffset[] = { + FirstBucketOffset, SecondBucketOffset, ThirdBucketOffset, + FourthBucketOffset, FifthBucketOffset, SixthBucketOffset +}; + +static QBasicAtomicPointer timerIds[] = { Q_BASIC_ATOMIC_INITIALIZER(FirstBucket), Q_BASIC_ATOMIC_INITIALIZER(0), Q_BASIC_ATOMIC_INITIALIZER(0), Q_BASIC_ATOMIC_INITIALIZER(0), Q_BASIC_ATOMIC_INITIALIZER(0), - Q_BASIC_ATOMIC_INITIALIZER(0), - Q_BASIC_ATOMIC_INITIALIZER(0), Q_BASIC_ATOMIC_INITIALIZER(0) }; static void timerIdsDestructorFunction() @@ -92,8 +112,17 @@ static inline int prepareNewValueWithSerialNumber(int oldId, int newId) return (newId & TimerIdMask) | ((oldId + TimerSerialCounter) & TimerSerialMask); } +namespace { + template struct QStaticAssertType; + template<> struct QStaticAssertType { enum { Value = 1 }; }; +} +#define q_static_assert(expr) (void)QStaticAssertType::Value + static inline int bucketOffset(int timerId) { + q_static_assert(sizeof BucketSize == sizeof BucketOffset); + q_static_assert(sizeof(timerIds) / sizeof(timerIds[0]) == NumberOfBuckets); + for (int i = 0; i < NumberOfBuckets; ++i) { if (timerId < BucketSize[i]) return i; -- cgit v0.12