[Python-Dev] Interning string subtype instances

"Martin v. Löwis" martin at v.loewis.de
Wed Feb 14 20:39:31 CET 2007


Larry Hastings schrieb:
> If I understand your question correctly, you're saying "why doesn't 
> string comparison take advantage of interned strings?"  If so, the 
> answer is "it does".  Examine string_richcompare() in stringobject.c, 
> and PyUnicode_compare() in unicodeobject.c.  Both functions compare the 
> pointers of the two objects in case they are literally the same object, 
> and if so skip all the expensive comparison code.  Interned strings 
> which have the same value are likely to be (are guaranteed to be?) the 
> same object.  And there you are.
> 
> If that's not the question you are asking, I'll be quiet, 'cause it 
> seems like other people understand what you're talking about.

The question was similar: "Why doesn't it take the maximum advantage?"
Interned strings that are equal are guaranteed to be identical. So
if strings are not identical and both interned, it could infer that
they can't be equal, but currently doesn't infer so (instead, it
compares length and contents in this case).

Regards,
Martin


More information about the Python-Dev mailing list