
Is there no way to optimize the byte code without adding to the language? Not that it’s a bad idea anyway, but I wonder if frozen sets are common enough to warrant a change. Are there any performance advantages to a frozen set? I ask because I do often use sets that could be frozen, but don’t need to be. E.g. they don’t change, nor are they used as keys. For example: If flag in {‘the’, ‘allowable’, ‘flags’}: … If a frozen set was even a little bit faster or used less memory, it would be nice to be able to create one directly. -CHB On Sun, Jan 16, 2022 at 8:50 AM MRAB <python@mrabarnett.plus.com> wrote:
On 2022-01-16 08:27, Steven D'Aprano wrote: [snip]
dis.dis("frozenset({1, 2, 3})") 1 0 LOAD_NAME 0 (frozenset) 2 BUILD_SET 0 4 LOAD_CONST 0 (frozenset({1, 2, 3})) 6 SET_UPDATE 1 8 CALL_FUNCTION 1 10 RETURN_VALUE
Got that? To create a frozenset of literals, first the compiler creates a frozenset constant containing what you wanted. Then at runtime, it:
- looks up frozenset in globals and builtins; - loads the pre-prepared frozenset (which is exactly what we want); - creates a new set from that frozenset; - calls the frozenset() function on that set to create a new frozenset that duplicates the pre-prepared one; - and finally garbage-collects the temporary set.
[snip]
Not quite as bad as that:
f = frozenset({1, 2, 3}) f is frozenset(f) True
Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/M2N3GN... Code of Conduct: http://python.org/psf/codeofconduct/
-- Christopher Barker, PhD (Chris) Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython