is None or == None ?

Alf P. Steinbach alfps at start.no
Fri Nov 6 12:28:08 EST 2009


* Rami Chowdhury:
> On Fri, 06 Nov 2009 08:54:53 -0800, Alf P. Steinbach <alfps at start.no> 
> wrote:
> 
>> But wow. That's pretty hare-brained: dynamic allocation for every 
>> stored value outside the cache range, needless extra indirection for 
>> every operation.
>>
> 
> Perhaps I'm not understanding this thread at all but how is dynamic 
> allocation hare-brained, and what's the 'needless extra indirection'?

Dynamic allocation isn't hare-brained, but doing it for every stored integer 
value outside a very small range is, because dynamic allocation is (relatively 
speaking, in the context of integer operations) very costly even with a 
(relatively speaking, in the context of general dynamic allocation) very 
efficient small-objects allocator - here talking order(s) of magnitude.

A typical scheme for representing dynamically typed objects goes like, in C++,

    enum TypeId { int_type_id, dyn_object_type_id };

    struct Object
    {
        int   type_id;
        union
        {
            void*   p;
            int     i;
            // Perhaps other special cased type's values in this union.
        };
    };

This would then be the memory layout of what's regarded as a variable at the 
script language level.

Then getting the integer value reduces to

   int intValueOf( Object const& o )
   {
       if( o.type_id != int_type_id ) { throw TypeError(); }
       return o.i;
   }

If on the other hand int (and perhaps floating point type, whatever) isn't 
special-cased, then it goes like

   int intValueOf( Object const& o )
   {
       if( o.type_id != int_type_id ) { throw TypeError(); }
       return static_cast<IntType*>( o.p )->value;    // Extra indirection
   }

and depending on where the basic type id is stored it may be more extra 
indirection, and worse, creating that value then involves a dynamic allocation.


Cheers & hth.

- Alf



More information about the Python-list mailing list