From 56603a8d83762d15e37c887e4c66a340d679f316 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 24 Aug 2012 12:40:20 +0200 Subject: [PATCH] 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 Reviewed-by: Olivier Goffart --- src/corelib/kernel/qmetatype.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index 8af3ce9024..80bc4a48b7 100644 --- a/src/corelib/kernel/qmetatype.h +++ b/src/corelib/kernel/qmetatype.h @@ -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