QMetaTypeId: optimize qt_metatype_id()

The old code resulted in one memory fetch for the test plus another one
for the return value (at the assembler level). The new code reuses the
already-loaded value:

-       movl    _ZZN11QMetaTypeIdI12QDBusMessageE14qt_metatype_idEvE11metatype_id(%rip), %edx
-       testl   %edx, %edx
-       je      .L158
        movl    _ZZN11QMetaTypeIdI12QDBusMessageE14qt_metatype_idEvE11metatype_id(%rip), %eax
-       ret
+       testl   %eax, %eax
+       je      .L160
+       rep; ret

It also saves one load in the not-yet-initialised case:

-.L158:
+.L160:
        leaq    .LC7(%rip), %rdi
        subq    , %rsp
        .cfi_def_cfa_offset 16
        movq    himBH1, %rsi
        call    _Z17qRegisterMetaTypeI12QDBusMessageEiPKcPT_
        movl    %eax, _ZZN11QMetaTypeIdI12QDBusMessageE14qt_metatype_idEvE11metatype_id(%rip)
-       movl    _ZZN11QMetaTypeIdI12QDBusMessageE14qt_metatype_idEvE11metatype_id(%rip), %eax
        addq    , %rsp
        .cfi_def_cfa_offset 8
        ret

Change-Id: I769950449822f2b1587680e05518be0a4f3120a2
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
This commit is contained in:
Marc Mutz 2012-08-24 12:40:20 +02:00 committed by Qt by Nokia
parent e6c50609c8
commit 56603a8d83

View File

@ -690,10 +690,12 @@ inline int qRegisterMetaTypeStreamOperators()
static int qt_metatype_id() \
{ \
static QBasicAtomicInt metatype_id = Q_BASIC_ATOMIC_INITIALIZER(0); \
if (!metatype_id.load()) \
metatype_id.storeRelease(qRegisterMetaType< TYPE >(#TYPE, \
reinterpret_cast< TYPE *>(quintptr(-1)))); \
return metatype_id.loadAcquire(); \
if (const int id = metatype_id.loadAcquire()) \
return id; \
const int newId = qRegisterMetaType< TYPE >(#TYPE, \
reinterpret_cast< TYPE *>(quintptr(-1))); \
metatype_id.storeRelease(newId); \
return newId; \
} \
}; \
QT_END_NAMESPACE