Title: String literals are not interned if in a tuple
Type: performance Stage: resolved
Components: Interpreter Core Versions: Python 3.6
Status: closed Resolution: duplicate
Dependencies: Superseder: Default value identity regression
View: 27942
Assigned To: Nosy List: benjamin.peterson, brett.cannon, georg.brandl, ncoghlan, rhettinger, serhiy.storchaka, vstinner, yselivanov
Priority: normal Keywords:

Created on 2016-01-18 20:09 by serhiy.storchaka, last changed 2016-09-25 10:44 by serhiy.storchaka. This issue is now closed.

Messages (3)
msg258542 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-01-18 20:09
Usually string literals are interned. But not if they are a part of constant tuple.

>>> def abc(): pass
>>> x = 'abc'
>>> x is abc.__name__
>>> x = ('abc',)
>>> x[0] is abc.__name__

This makes effect on namedtuples (issue25981). May make effect on __slots__ or other uses of constant tuples since searching a value in a tuple when values are not identical is a little slower that when they are identical.
msg258594 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-01-19 13:58
FYI fatoptimizer has a more efficient algorithm to merge constants:

It looks like it interns strings which are part of tuples. To be honest, I don't understand how it works, but it works :-)
msg277361 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-09-25 10:44
The patch is provided in issue27942.
Date User Action Args
2016-09-25 10:44:49serhiy.storchakasetstatus: open -> closed
superseder: Default value identity regression
messages: + msg277361

resolution: duplicate
stage: resolved
2016-04-26 08:51:12serhiy.storchakalinkissue25981 superseder
2016-01-19 13:58:38vstinnersetmessages: + msg258594
2016-01-19 13:41:24vstinnersetversions: + Python 3.6
2016-01-19 13:41:17vstinnersettype: performance
2016-01-18 20:09:48serhiy.storchakacreate