[Python-Dev] Interning string subtype instances

"Martin v. Löwis" martin at v.loewis.de
Tue Feb 13 11:46:04 CET 2007


Greg Ewing schrieb:

> That doesn't quite give you everything that real interning
> does, though. The string comparison method knows when both
> strings are interned, so it can compare them quickly
> whether they are equal or not. 

No, it doesn't - see stringobject.c:string_richcompare.
If they are identical, they are quickly recognized as equal,
and as not not-equal. Otherwise, if eq comparison is requested,
it compares the size, then the first characters, then does
memcmp - so if they are unequal, it doesn't help that they
are both interned.

If comparison is for notequal, no special casing is performed
at all (except that knowing that identical strings aren't notequal).

The entire algorithm and optimization works just as fine for
a user-defined interning dictionary (assuming that all
relevant strings get into it).

Regards,
Martin


More information about the Python-Dev mailing list