
On 16 Jan 2022, at 18:44, Christopher Barker <pythonchb@gmail.com> wrote:
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.
Not really relevant for the discussion, but CPython automaticly creates a frozenset here (set display with immutable members) as an optimisation.
dis.dis("x in {1,2,3}") 1 0 LOAD_NAME 0 (x) 2 LOAD_CONST 0 (frozenset({1, 2, 3})) 4 CONTAINS_OP 0 6 RETURN_VALUE
AFAIK the primary advantage of doing this is that the frozenset gets created once instead of every time the expression is executed. Frozenset itself is not faster than a regular set. Ronald — Twitter / micro.blog: @ronaldoussoren Blog: https://blog.ronaldoussoren.net/