On 09/13/2011 09:57 PM, Amos Jeffries wrote:
> On Tue, 13 Sep 2011 10:11:55 -0600, Alex Rousskov wrote:
> <snip>
>>> void
>>> +HttpHdrCc::clear()
>>> +{
>>> + mask=0;
>>> + max_age=-1;
>>> + mask=0;
>>> + max_age=-1;
>>> + s_maxage=-1;
>>> + max_stale=-1;
>>> + stale_if_error=0;
>>> + min_fresh=-1;
>>> + other.clean();
>>> +}
>>
>> This duplicates the default constructor. Mask and max_age are cleared
>> twice. Consider this implementation instead (which can be inlined):
>>
>> *this = HttpHdrCc();
>>
>
> Nice.
>
> We use ~Foo(){clear();} in quite a few places. Do you have any info
> which is faster?
In general, you should not call clear() from the destructor because
a) clear() may leave members allocated (and ready for use) and
b) clear() spends extra cycles on making the object usable again while
the destructor does not need to do that.
For example, HttpHdrCc's default/generated destructor is already
sufficient and you cannot beat the efficiency of "nothing".
However, it is often technically correct, too convenient, and not very
expensive to call clean() in destructor instead of splitting the code
into some kind of destruct() and init().
In general, the following scheme avoids code duplication:
default constructor: init()
copy constructor: copy()
destructor: destruct()
assignment operator: destruct() followed by copy()
clean(): destruct() followed by init()
but writing correct init() and copy() members is difficult/dangerous,
and there are plenty of reasonable shortcuts and useful optimizations as
HttpHdrCc illustrates.
HTH,
Alex.
Received on Wed Sep 14 2011 - 14:59:38 MDT
This archive was generated by hypermail 2.2.0 : Thu Sep 15 2011 - 12:00:05 MDT