parse-time optimizations
Delaney, Timothy
tdelaney at avaya.com
Wed May 30 02:04:46 EDT 2001
> [Greg Ewing]
> > You would have to be careful how you implemented
> > this, because
> >
> > x + "foo" + "blarg"
> >
> > could have very different results from
> >
> > x + "fooblarg"
> >
> > depending on what sort of thing x is bound to.
>
> Ditto
> x + 1 + 2
> vs
> x + 3
>
> and so on.
Very true. Constant folding could only be used in unambiguous situations ...
and most such things aren't unambiguous :(
Indeed
x + 1 + 2 + 3
couldn't even be folded down to
x + 1 + 5
as the expression needs to be evaluated left to right
((x + 1) + 2) + 3
OTOH
3 + 2 + 1 + x
*could* be validly folded to
6 + x
Hmm ... this brings to mind C++ compilers - do *they* always get it right
with constant folding? Or do most not bother these days, for precisely this
reason?
>I don't want to use it at all unless it helpfully expands
>
> stars = "*" * 1000000
>
> at compile-time too <wink>.
>
> .pyc-files-are-far-too-small-ly y'rs - tim
Hmm ... I think a constant-folding implementation should perhaps have an
arbitrary maximum string length which can be folded... ;) On second
thoughts, perhaps it should only apply to numeric constants (which was my
approach in the first place). Then you have space *savings*, rather than
wasted space (both disc and RAM).
Tim Delaney
More information about the Python-list
mailing list