[Python-Dev] Tagged integers

James Y Knight foom at fuhm.net
Thu Jul 15 09:11:14 CEST 2004


On Jul 14, 2004, at 10:32 PM, Jeff Epler wrote:
> By the way, I don't know what optimizers actually do, but given
>     PyObject *spam(PyObject *o) {
>         Py_GETTYPE(o); junk; Py_GETREF(o); junk;
>         Py_INCREF(o); junk; Py_DECREF(o); junk;
>     }
> the compiler is free to notice that "o" can't change (its address is 
> never
> taken), so the "is it a tagged object" test can be done once, not 4 
> times.

I was unable to convince GCC 3.3.3 to be this smart. I don't think it 
even optimized "Py_GETTYPE(o) == X || Py_GETTYPE(o) == Y" to remove the 
redundant Py_GETTYPE. Perhaps someone versed in compiler-trickery could 
explain why it refuses to do common-subexpression-elimination there, or 
else trick it into doing so.

> I'm showing my ignorance here, but what other types are traditionally
> tagged, and do any of those translate well to Python?
>
> I suppose you could tag some singleton objects, like None, True, False,
> and (), but those are already shared.
>
> Tagging length-1 strings and unicode strings seems like it might have 
> its
> advantages, though the length-0 and length-1 strings, length-0 unicode
> strings, and first 256 length-1 unicode strings are already shared too.
>
> I can't see much use for a 21-bit-mantissa floating-point type, and
> I can't see how you could use tagging for any other types in Python.

In general, there's not much point in using tagged objects for 
singleton types like booleans. It's a waste of a tag value. However see 
my other post for counter-results in the case of Python.

You've basically got the standard tagged objects covered there: 
integers, and characters. E.g. see 
<http://www.xemacs.org/Documentation/21.5/html/internals_8.html> I had 
an idea that it might be possible to have a single-element tuple be a 
tagged object as well, but that may well be not worth the overhead.

On a 64-bit computers it could be possible to steal two bits from the 
exponent of (small) floats to do inline 62-bit floats. I wouldn't want 
to steal bits from the mantissa, you'd end up with different (non IEEE 
standard) results.

James



More information about the Python-Dev mailing list