codehaus


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

constant folding - why not more


>
> On the contrary, comparison remains for runtime:
> >>> dis.dis(compile('1 < 2', filename='<string>', mode='eval',
> >>> optimize=2))
>   1           0 LOAD_CONST               0 (1)
>               2 LOAD_CONST               1 (2)
>               4 COMPARE_OP               0 (<)
>               6 RETURN_VALUE
> In function fold_unaryop (though comparison is a binary operation) in
> Python/ast_opt.c is remark: /* Fold not into comparison */
>
> Is there a reason why comparison (== != < > <= >=) is not folded?
>

I can think of two reasons. One, this kind of comparison will almost never
appear in production code (maybe in unit tests?). Unlike the C family of
languages, Python doesn't have a macro processor which would give symbolic
names to numeric constants or string literals. Code generators might
conceivably generate constant comparisons, but they might be able to easily
do constant folding of comparisons themselves.

Two, given that this sort of construct will almost never be found in the
wild, folding constant comparisons in the compiler would increase the
maintenance burden of the compiler (just slightly, but still...) with no
clear benefit.

Skip

>