[Python-3000] Use Py_CLEAR only

Walter Dörwald walter at livinglogic.de
Wed Apr 19 09:30:51 CEST 2006


Greg Ewing wrote:
> Walter Dörwald wrote:
>> Greg Ewing wrote:
>>> It would be less efficient in the cases where you don't
>>> need to check for NULL and/or clear the reference
>>> afterwards.
>>
>> Both should be optimized away by the compiler.
> 
> How? I don't see how the compiler can know either of
> those things.

The "checking for NULL" scenario looks somewhat like this:

PyObject *foo = NULL;
foo = get_foo();
if (foo)
    do something useful
else
    return;

if (foo)
    foo = NULL;

The compiler should be able to detect that the second foo check is 
redundant, if "do something useful" doesn't use foo.

And the "clearing the reference" scenario should look ike this:

{
     PyObject *foo = get_foo();
     do something useful
     foo = NULL;
}

The compiler should be able to detect that foo isn't used any more after 
the last assignment, so the assignment can be optimized away.

Servus,
    Walter


More information about the Python-3000 mailing list