[New-bugs-announce] [issue46393] Generate frozenset constants when explicitly appropriate
Terry J. Reedy
report at bugs.python.org
Sat Jan 15 22:11:25 EST 2022
New submission from Terry J. Reedy <tjreedy at udel.edu>:
The CPython compiler is capable of making frozenset constants without being explicitly asked to. Exactly how it does so is, of course, 'hidden' from python code. With current main:
.
>>> dis('{1,2,3}')
1 0 BUILD_SET 0
2 LOAD_CONST 0 (frozenset({1, 2, 3}))
4 SET_UPDATE 1
6 RETURN_VALUE
Suppose one wants actually wants a frozenset, not a mutable set. 'frozenset({1,2,3})' is compiled as the above followed by a frozenset call -- making an unneeded double conversion to get what already exists.
To avoid the intermediate set, one can use a constant tuple instead.
>>> dis('frozenset((1,2,3))')
1 0 LOAD_NAME 0 (frozenset)
2 LOAD_CONST 0 ((1, 2, 3))
4 CALL_FUNCTION 1
6 RETURN_VALUE
Even nicer would be
1 0 (frozenset({1, 2, 3}))
2 RETURN_VALUE
'set((1,2,3))' is compiled the same as 'frozenset((1,2,3)), but frozenset does not having the option is using a more efficient display form. I cannot think of any reason to not call frozenset during compile time when the iterable is a constant tuple.
Serhiy, I not sure how this relates to your issue 33318 and the discussion therein about stages, but it does relate to your interest in compile time constants.
----------
components: Interpreter Core
messages: 410666
nosy: serhiy.storchaka, terry.reedy
priority: normal
severity: normal
stage: test needed
status: open
title: Generate frozenset constants when explicitly appropriate
type: enhancement
versions: Python 3.11
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46393>
_______________________________________
More information about the New-bugs-announce
mailing list