classification
Title: peephole optimization for constant strings
Type: performance Stage: needs patch
Components: Interpreter Core Versions: Python 3.4
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: haypo, nnorwitz, terry.reedy
Priority: normal Keywords:

Created on 2013-01-28 22:19 by nnorwitz, last changed 2013-02-02 10:01 by terry.reedy.

Messages (3)
msg180885 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2013-01-28 22:19
I was looking through code like this:

  foo = '%s%s%s' % ('https://', host, uri)

and realized this could be rewritten by the interpreter as:

  foo = 'https://%s%s' % (host, uri)

I tried to determine how much code this might affect, but it was pretty hard for me to come up with a decent regex to filter out all the false positives.  There were too many hits to determine if this would be used often.
msg180889 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2013-01-29 00:09
> and realized this could be rewritten by the interpreter as:

Yeah, it could but it's tricky to implement it. The current peephole is implemented in C. You may first try to implement it using my astoptimizer project which is implemented in Python. At least to check if it's possible or not :-)

https://bitbucket.org/haypo/astoptimizer

astoptimizer only optimizes str%args if it succeed at compile time, so if all arguments are constant and no error is raised.
msg181156 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-02-02 10:01
It would also be nice to optimize '{}{}{}'.format('https://', host, uri).
Either seems a bit much to ask from a fairly naive compiler :-).
History
Date User Action Args
2013-02-02 10:01:37terry.reedysetversions: + Python 3.4
nosy: + terry.reedy

messages: + msg181156

stage: needs patch
2013-01-29 00:09:11hayposetmessages: + msg180889
2013-01-28 22:39:40pitrousetnosy: + haypo
2013-01-28 22:19:04nnorwitzcreate