Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'else:': 0.03; 'cpython': 0.05; '(python': 0.07; 'pypy': 0.07; 'variables': 0.07; 'optimizing': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Does': 0.09; 'python': 0.11; 'jan': 0.12; 'mostly': 0.14; 'article:': 0.16; 'ast': 0.16; 'blocks': 0.16; 'bytecode': 0.16; 'exception?': 0.16; 'folding.': 0.16; 'profiling,': 0.16; 'pypy.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject:already': 0.16; 'transforming': 0.16; 'work.)': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'obviously': 0.18; 'any,': 0.19; 'feb': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'byte': 0.24; 'skip': 0.24; 'mon,': 0.24; 'source': 0.25; 'mention': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; "doesn't": 0.30; 'moved': 0.30; 'code': 0.31; 'lines': 0.31; 'constant': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'probably': 0.32; 'open': 0.33; 'url:python': 0.33; 'not.': 0.33; 'but': 0.35; 'there': 0.35; 'thanks': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'should': 0.36; 'easily': 0.37; 'step': 0.37; 'improving': 0.38; 'to:addr:python-list': 0.38; 'expect': 0.39; 'reported': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'read': 0.60; 'most': 0.60; 'new': 0.61; 'received:173': 0.61; 'simple': 0.61; "you're": 0.61; 'save': 0.62; 'show': 0.63; 'kind': 0.63; 'real': 0.63; 'temporary': 0.65; 'effectively': 0.66; 'between': 0.67; 'believe': 0.68; 'safe': 0.72; 'apart': 0.72; 'benchmark': 0.84; 'percent,': 0.84; 'received:fios.verizon.net': 0.84; 'resulted': 0.84; 'subject:skip:o 10': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Does CPython already has Peephole optimizations? Date: Mon, 17 Feb 2014 08:51:16 -0500 References: <5301cf82$0$29985$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-254-207.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0 In-Reply-To: <5301cf82$0$29985$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 61 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1392645126 news.xs4all.nl 2861 [2001:888:2000:d::a6]:57425 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:66606 On 2/17/2014 3:59 AM, Steven D'Aprano wrote: > On Mon, 17 Feb 2014 13:54:25 +0530, Laxmikant Chitare wrote: > >> I read about this article: >> http://www.python.org/workshops/1998-11/proceedings/papers/montanaro/ > montanaro.html >> >> Just wanted to clarify whether CPython already includes these kind of >> byte code optimizations? Most of the easily seen and obviously safe low-hanging fruits for the compile step have been plucked. Note that the effect of the peephole process would only save a few percent, if any, for real apps*. Improving the C code invoked by bytecode has resulted in much larger gains. * We now have a much better benchmark suite with some real apps. This is thanks in part to the pypy project. >> Are all the temporary variables removed when byte code is generated? > > You can check these things for yourself: > > import dis > dis.dis(function) > > will show you the byte code. > > But in general, I would expect not. CPython (that's the Python you > probably use) doesn't do a lot of optimization apart from some simple > constant folding. If you're interested in optimizing Python, you should > look at the JIT optimizing Python compiler, PyPy. For CPython, new optimization has mostly moved to AST tranformations prior to compilation. (Python ASTs are new since Skip started the peephole work.) I believe there are some open issues on the tracker. Once optimization constraint Skip did not mention is the correspondence between source lines and blocks of bytecode, which is used by profiling, tracing, and tracebacks. Effectively transforming if type(a) == types.ComplexType: x = cmath.sin(a) foo(x) else: x = math.sin(a) foo(x) into if type(a) == types.ComplexType: x = cmath.sin(a) else: x = math.sin(a) foo(x) breaks the correspondence. If foo(x) raises, which original line should be reported as the source of the exception? -- Terry Jan Reedy