On Tue, Dec 03, 2019 at 01:54:44AM -0800, Andrew Barnert via Python-ideas wrote:
> On Dec 2, 2019, at 16:27, Soni L. <[email protected]> wrote:
> >
> > Even use-cases where you have different objects whose differences are
> > ignored for __eq__ and __hash__ and you want to grab the one from the set
> > ignoring their differences would benefit from this.
>
> A more concrete use case might help make the argument better.
Is interning concrete enough?
The Python interpreter interns at least two kinds of objects: ints and
strings, or rather, *some* ints and strings. Back in Python 1.5, there
was a built-in for interning strings:
# Yes I still have a 1.5 interpreter :-)
>>> a = intern("hello world")
>>> b = intern("hello world")
>>> a is b
1
so perhaps people might like to track down the discussion for and
against removing intern.
We can get the same effect with a dict, but at the cost of using two
pointers per interned object (one as the key, one as the value):
cache = {}
def intern(obj):
return cache.setdefault(obj, obj)
You could cut that to one pointer by using a set, at the expense of
making retrieval slower and more memory-hungry:
# untested
cache = set()
def intern(obj):
if obj in cache:
return cache - (cache - {obj})
cache.add(obj)
return obj
The interpreter interns only a subset of ints and strings because to
intern more would just waste memory for no use. But that's because the
interpreter has to consider arbitrary programs. If I knew that my
program was generating billions of copies of the same subset of values,
I might be able to save memory (and time?) by interning them.
This is terribly speculative of course, but with no easy way to
experiment, speculating is all I can do.
--
Steven
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/CB75IYKGADZMSNVZJZVBQDLDQZ3VOUWI/
Code of Conduct: http://python.org/psf/codeofconduct/