This idea came up during a conversation with @Fidget-Spinner.
dct=frozendict(a=1)
deffoo():
dct['a'] # fold this to 1
When a frozendict is a promoted constant in the JIT, subscript lookups like dct['a'] should be foldable to the constant value at compile time. This would make frozendict lookups and match/case on frozendict significantly faster. If implemented, this optimization could create strong motivation for users to adopt frozendict in many more places, anywhere a dict is used as a read only lookup table, configuration mapping, or dispatch table.
Plan to start from REPLACE_OPCODE_IF_EVALUATES_PURE in optimizer_bytecodes.c and extend it to handle _BINARY_OP_SUBSCR_DICT for frozendict constant
Activity
pythongh-146381: Fold frozendict(key=const, ...) calls to LOAD_CONST
pythongh-146381: Fold frozendict subscript via _BINARY_OP_SUBSCR_FROZ…
pythongh-146381: Fold frozendict subscript via _BINARY_OP_SUBSCR_FROZ…
gh-146381: Constant-fold frozendict subscript lookups via REPLACE_OPC…
pythongh-146381: Constant fold local frozendict subscript lookups via…
pythongh-146381: Constant fold local frozendict subscript lookups via…
corona10 commented on Apr 4, 2026
Anyway global frozendict now become optimized.
pythongh-146381: Constant-fold frozendict subscript lookups via REPLA…
pythongh-146381: Constant-fold frozendict subscript lookups via REPLA…