Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #66594 > unrolled thread

Does CPython already has Peephole optimizations?

Started byLaxmikant Chitare <laxmikant.general@gmail.com>
First post2014-02-17 13:54 +0530
Last post2014-02-18 10:41 +0530
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Does CPython already has Peephole optimizations? Laxmikant Chitare <laxmikant.general@gmail.com> - 2014-02-17 13:54 +0530
    Re: Does CPython already has Peephole optimizations? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-17 08:59 +0000
      Re: Does CPython already has Peephole optimizations? Ned Batchelder <ned@nedbatchelder.com> - 2014-02-17 07:48 -0500
      Re: Does CPython already has Peephole optimizations? Terry Reedy <tjreedy@udel.edu> - 2014-02-17 08:51 -0500
      Re: Does CPython already has Peephole optimizations? Laxmikant Chitare <laxmikant.general@gmail.com> - 2014-02-18 10:41 +0530

#66594 — Does CPython already has Peephole optimizations?

FromLaxmikant Chitare <laxmikant.general@gmail.com>
Date2014-02-17 13:54 +0530
SubjectDoes CPython already has Peephole optimizations?
Message-ID<mailman.7086.1392625473.18130.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

Hello All,

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? Are all the temporary variables removed when byte code
is generated?

Regards,
Laxmikant

[toc] | [next] | [standalone]


#66596

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-02-17 08:59 +0000
Message-ID<5301cf82$0$29985$c3e8da3$5496439d@news.astraweb.com>
In reply to#66594
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? 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.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#66600

FromNed Batchelder <ned@nedbatchelder.com>
Date2014-02-17 07:48 -0500
Message-ID<mailman.7092.1392641296.18130.python-list@python.org>
In reply to#66596
On 2/17/14 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? 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.
>
>

CPython does some constant folding, and also jump optimizations.  In my 
role as coverage.py maintainer, I would love to see a way to disable all 
those optimizations.  I tried filing a bug about it 
(http://bugs.python.org/issue2506), but it did not win the popular 
support I had hoped for.

-- 
Ned Batchelder, http://nedbatchelder.com

[toc] | [prev] | [next] | [standalone]


#66606

FromTerry Reedy <tjreedy@udel.edu>
Date2014-02-17 08:51 -0500
Message-ID<mailman.7096.1392645125.18130.python-list@python.org>
In reply to#66596
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

[toc] | [prev] | [next] | [standalone]


#66632

FromLaxmikant Chitare <laxmikant.general@gmail.com>
Date2014-02-18 10:41 +0530
Message-ID<mailman.7115.1392700274.18130.python-list@python.org>
In reply to#66596

[Multipart message — attachments visible in raw view] — view raw

Thank you all for the enlightening inputs. I have learnt a lot just with
this one question. Great to know about dis library. Ned, from explanation
I now realize how important it is to do impact analysis. Things are not
always rosy :).

I have always appreciated everyone over this list. This is just another
opportunity.

Best regards,
Laxmikant



On Mon, Feb 17, 2014 at 7:21 PM, Terry Reedy <tjreedy@udel.edu> wrote:

> 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
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web