C++ Logo


Advanced search

Re: [std-proposals] this return type

From: Jan Schultke <janschultke_at_[hidden]>
Date: Sun, 9 Apr 2023 17:42:56 +0200
> But is it worth it?

Yes, see Lenard's example:

> bar(S const&):
> push rbx
> mov rbx, rdi

As can be seen, we are spilling rbx and using it to store the
reference we're given (in rdi). This means we don't have to push and
pop all the time to make subsequent virtual calls, but can keep our
reference in a register.

If the vtable stayed constant, we could prevent even more memory
access by spilling rcx and using it to store the vtable pointer during
the next N calls. However, this is not the case, the compiler assumes
that the vtable changes, so there are two extra reads from memory in
the form of:
> mov rax, qword ptr [rbx]

Received on 2023-04-09 15:43:09