This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: peephole optimization for constant string interpolation
Type: performance Stage: resolved
Components: Interpreter Core Versions: Python 3.9
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: BTaskaya, methane, nnorwitz, rhettinger, terry.reedy, vstinner
Priority: low Keywords:

Created on 2013-01-28 22:19 by nnorwitz, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (6)
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 (vstinner) * (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 :-).
msg357651 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2019-11-29 20:19
I think this operation is more suitable for AST optimizer then peephole but i'm still not sure if such a conversion gains anything compared to usage of old type string formattings.
msg357666 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-11-30 22:19
Am thinking this should be closed.  It isn't clear that it can be done easily or that there would be a measurable impact.  Also, the advent of f-strings means that code like this will occur much less often -- both code examples presented would likely no longer be coded as they once were.
msg357670 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-12-01 00:03
Counting Serhiy's de-priorizing, all five core devs find this a bit dubious, so yes, let's close.
History
Date User Action Args
2022-04-11 14:57:41adminsetgithub: 61270
2019-12-01 00:03:06terry.reedysetstatus: open -> closed
title: peephole optimization for constant strings -> peephole optimization for constant string interpolation
messages: + msg357670

resolution: rejected
stage: needs patch -> resolved
2019-11-30 22:19:30rhettingersetnosy: + rhettinger
messages: + msg357666
2019-11-30 06:37:19serhiy.storchakasetpriority: normal -> low
versions: + Python 3.9, - Python 3.4
2019-11-29 20:19:33BTaskayasetnosy: + BTaskaya
messages: + msg357651
2019-04-13 11:34:20methanesetnosy: + methane
2013-02-02 10:01:37terry.reedysetversions: + Python 3.4
nosy: + terry.reedy

messages: + msg181156

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