Message410666
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. |
|
Date |
User |
Action |
Args |
2022-01-16 03:11:25 | terry.reedy | set | recipients:
+ terry.reedy, serhiy.storchaka |
2022-01-16 03:11:25 | terry.reedy | set | messageid: <1642302685.54.0.683212024451.issue46393@roundup.psfhosted.org> |
2022-01-16 03:11:25 | terry.reedy | link | issue46393 messages |
2022-01-16 03:11:25 | terry.reedy | create | |
|