Speed Up Your Qt 5 Programs Using C++11 - Qt Developer Days ...

55 downloads 118 Views 660KB Size Report
C++11 @ Qt 5.0. ○ constexpr added to many types. ○ move semantics added to a few types. ○ initializer_list added to most types. ○ very few N-ary ctors ...
Marc Mutz KDAB (DE), Qt Widget Maintenance

Speed Up Your Qt 5 Programs Using C++11

Overview

C++11 @ Qt 5.0



constexpr added to many types



move semantics added to a few types



initializer_list added to most types



very few N-ary ctors marked explicit, N ≥ 2



= delete used almost ubiquitously



noexcept added in a few central places



other features not very usable in APIs

A Simple Example

Prevents optimisations...

Results

Less!

Less!

The C++11 Free Lunch Less! (GCC 4.8-20120823 / AMD64 / Linux) Baseline (int main() {})

Whodunnit?

The Qt-Project did...

Whodunnit? This is magic...

Whodunnit?

C++98

Whodunnit?

C++11

constexpr?

constexpr



New keyword in C++11



Can be applied to





(Free and Member) Functions



Variables



Constructors

Enables Evaluation at Compile-Time

constexpr

● ●

New keyword in C++11

Can be appliedconstexpr to ● ● ●



constructor

(Free and Member) Functions (also requires a trivial destructor) Variables Constructors

“Literal Type”

Enables Evaluation at Compile-Time

const Type == compile-time constant

constexpr

constexpr

.section .rodata “read-only data” .align 16 .type _ZL8triangle, @object .size _ZL8triangle, 24 _ZL8triangle: .long -50 .long 0 .long 50 .long 0 .long 0 .long 100

C++11

constexpr

And that is new... ...how? Well...

constexpr

“read-only data” [...] .section .ctors,"aw",@progbits .align 8 .quad _GLOBAL__sub_I_main .local _ZL8triangle .comm _ZL8triangle,24,16

C++98

constexpr

.type _GLOBAL__sub_I_main, @function _GLOBAL__sub_I_main: movl $-50, _ZL8triangle(%rip) “D movl $0, y4+_ZL8triangle(%rip) nam ic I movl $50, 8+_ZL8triangle(%rip) n it iali movl $0, 12+_ZL8triangle(%rip) sat io n movl $0, 16+_ZL8triangle(%rip) ” movl $100, 20+_ZL8triangle(%rip) ret “read-only data” [...] .section .ctors,"aw",@progbits .align 8 .quad _GLOBAL__sub_I_main .local _ZL8triangle .comm _ZL8triangle,24,16

C++98

constexpr

Co sts ?

.type _GLOBAL__sub_I_main, @function _GLOBAL__sub_I_main: movl $-50, _ZL8triangle(%rip) “D movl $0, y4+_ZL8triangle(%rip) nam ic I movl $50, 8+_ZL8triangle(%rip) n it ? my> iali n movl $0, 12+_ZL8triangle(%rip) o i t a s anat o sat s i l a i t io n i movl $0, 16+_ZL8triangle(%rip) rey' n I g / < ” on e c movl $100, 20+_ZL8triangle(%rip) Ra a t a :D g n ret i d Or a e r h “read-only data” de ltit [...] u M ro .section .ctors,"aw",@progbits fI ni t ia .align 8 lis .quad _GLOBAL__sub_I_main at io n? .local _ZL8triangle .comm _ZL8triangle,24,16 art up

? ? ?

St

y l s u

S

o i r e

C++98

constexpr

constexpr

_Z3onev: subq movq movl movl movq movq call addq ret

$40, %rsp %rsp, %rdi $-1, (%rsp) $-1, 4(%rsp) $0, 8(%rsp) $0, 16(%rsp) _Z10checkIndexRK11QModelIndex@PLT $40, %rsp _Z3twov: leaq jmp

_ZL4root(%rip), %rdi _Z10checkIndexRK11QModelIndex@PLT

Tail Call Optimisation

constexpr

_Z3onev: subq $40, %rsp movq %rsp, %rdi ● compilers don't fold instances of movl $-1, (%rsp) movl $-1, 4(%rsp) ● standard doesn't permit it movq $0, 8(%rsp) movq $0, 16(%rsp) ● addresses call _Z10checkIndexRK11QModelIndex@PLT must be unique addq $40, %rsp ● ret

literal types

you need to do the folding yourself



learn to love

_Z3twov: _ZL4root(%rip), %rdi staticleaq const variables :) jmp _Z10checkIndexRK11QModelIndex@PLT

Tail Call Optimisation

virtual functions

Talking about embarrssments...

Error: `timerEvnet(QTimerEvent*)` doesn't override anything!

. . . e m i t n u r ) : p t u n e d e m e p p o l s e t ' v n e s d e t o u D ...b

Another Simple Example

C++98: QString::fromUtf8() C++11: copy of a pointer

C++11: won't throw

Guidelines



add Q_DECL_OVERRIDE to virtual overrides



use static const type in favour of temporaries ●

in apps, not DLLs/SOs/DYLIBs



even when just default-constructed



prefer unnamed over named temporaries



use QStringLiteral



qDebug() doesn't throw anymore

C++11 @ Qt 5.1 (planned)



add more constexpr



add more noexcept



add all missing move ctors



experiment with rvalue refs on *this



experiment with extern templates



mark N-arg ctors explicit, N ≥ 2



add Q_DECL_OVERRIDE everywhere



implement C++11 API on our containers

Q&S

Questions? Suggestions?