New submission from Terry J. Reedy <tjre...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue46393>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to