parse-time optimizations

jcm grumble at usa.net
Tue May 29 01:08:47 EDT 2001


Remco Gerlich <scarblac at pino.selwerd.nl> wrote:
> jcm <grumble at usa.net> wrote in comp.lang.python:
>> It'd be Nice if the Python parser (or bytecode-compiler or whatever
>> non-runtime thing is most appropriate) would translate the addition of
>> two string literals into a single string.  Or maybe it does this
>> already?  I haven't tried.

> Python does *no* magic optimizations. It's completely WYSIWYG in that
> respect. You tell it to add the two strings, Python does that, at runtime
> like every other addition.

Yea, I guess it's just wishful thinking.  Something about
string-literal juxtaposition doesn't sit right with me.  I guess I'm
used to optimizing compilers, which would transform "a" + "b" into
"ab" (and 1 + 2 into 3, etc).  Having a different syntax specifically
for string-literal concatenation seems odd (it seems less odd in C
only because strings are so klunky).

This set of optimizations, which are very easy to identify at
parse-time, would be nice to see added to Python.  There are some
things to pay attention to (like arithmetic overflow), but still
fairly simple to implement, as far as optimizations go.  And yes, I
have.

Yeah I know: "so go implement it yourself in Python too".  Wishful
thinking is about as far as I expect I'll get.  If I ever stuck my
hands in the Python parser, there are a couple of things I'd want to
change (like not generating the full concrete syntax tree--parser
module be damned!).  I should stop now or I might actually convince
myself to go look at the Python source.



More information about the Python-list mailing list