[mypyc] Precompute set literals for "in" ops against / iteration over set literals (#14409)
Towards https://github.com/mypyc/mypyc/issues/726. (There's a Python
compatibility bug that needs to be fixed before the issue can be
closed.)
For example, the set literals here are now precomputed as frozensets at
module initialization.
```
x in {1, 2.0, "3"}
x not in {1, 2.0, "3"}
for _ in {1, 2.0, "3"}:
...
```
Set literal items supported:
- Anything supported by `irbuild.constant_fold.constant_fold_expr()`
- String and integer literals
- Final references to int/str values
- Certain int and str unary/binary ops that evaluate to a constant value
- `None`, `True`, and `False`
- Float, byte, and complex literals
- Tuple literals with only items listed above
**Results** (using gcc-9 on 64-bit Ubuntu)
Master @ 98cc165a657a316accb93f1ed57fdc128b086d9f
running in_set
..........
interpreted: 0.495790s (avg of 5 iterations; stdev 6.8%)
compiled: 0.810029s (avg of 5 iterations; stdev 1.5%)
compiled is 0.612x faster
running set_literal_iteration
.........................................................................................
interpreted: 0.020255s (avg of 45 iterations; stdev 2.5%)
compiled: 0.016336s (avg of 45 iterations; stdev 1.8%)
compiled is 1.240x faster
This PR
running in_set
..........
interpreted: 0.502020s (avg of 5 iterations; stdev 1.1%)
compiled: 0.390281s (avg of 5 iterations; stdev 6.2%)
compiled is 1.286x faster
running set_literal_iteration
..............................................................................................
interpreted: 0.019917s (avg of 47 iterations; stdev 2.2%)
compiled: 0.007134s (avg of 47 iterations; stdev 2.6%)
compiled is 2.792x faster
Benchmarks can be found here: mypyc/mypyc-benchmarks#32