Reputation: 353
Is it useful, in terms of saving time and reducing number of operations, to create a reference to (*this)
in the beginning of a member function where using of this->
is recurrent? Is it something that compilers (gcc interests me the most) already optimize for me? Are there reasons not to do this?
Example:
void A::checkBytes( const byte * dataChunk, uint32_t chunkSize )
{
A & self = (*this);
bool UTF8Valid = self.InvalidSequences & 1;
byte current, expectedUTF8Bytes = 0;
for (uint32_t i = 0; i < chunkSize; i++)
{
current = dataChunk[i];
// many tests with 'current' and 'this->InvalidSequences'
self.Count[current]++;
self.ChunkSize++;
}
if (!UTF8Valid) self.InvalidSequences |= 1;
}
I know every non-static member function takes its own hidden this
. I know I'll have both the hidden A * this
and the A & self
. What I do not know is whether many this->someMember
will cost more than many referenceToThis.someMember
or not at all.
Upvotes: 0
Views: 450
Reputation: 30145
No. worst case it would consume extra stack space and need some extra instructions, best case it gets optimized back to this->
.
this
will almost certainly live in a register where possible, and a reference like A&
is basically a pointer at the implementation level.
There probably isn't even a gain in trying to store a direct pointer/reference to this->Count
etc., as on most platforms this->Count[n]
can be a single instruction (e.g. on x86 I believe LEA
will be used, you might check the disassembly).
Upvotes: 5