Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-07 Thread Terry Reedy
Alexandru Moșoi wrote: From: "Cesare Di Mauro" So if Python will generate LOAD_CONST 1 LOAD_CONST 2 BINARY_ADD the constant folding code will simply replace them with a single LOAD_CONST 3 When working with such kind of optimizations, the temptation is to apply them at any sit

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-07 Thread Terry Reedy
Cesare Di Mauro wrote: On Tue, Apr 7, 2009 07:22PM, Guido van Rossum wrote: In my experience it's better to discover a bug at compile time rather than at running time. That's my point though, which you seem to be ignoring: if the user explicitly writes "1/0" it is not likely to be a bug. That's

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-07 Thread Jared Grubb
On 7 Apr 2009, at 11:59, Alexandru Moșoi wrote: Not necessarily. For example C/C++ doesn't define the order of the operations inside an expression (and AFAIK neither Python) and therefore folding 2 * 3 is OK whether b is an integer or an arbitrary object with mul operator overloaded. Moreover one

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-07 Thread Fredrik Johansson
On Tue, Apr 7, 2009 at 8:59 PM, Alexandru Moșoi wrote: > Not necessarily. For example C/C++ doesn't define the order of the > operations inside an expression (and AFAIK neither Python) and > therefore folding 2 * 3 is OK whether b is an integer or an arbitrary > object with mul operator overloade

[Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-07 Thread Alexandru Moșoi
> From: "Cesare Di Mauro" > So if Python will generate > > LOAD_CONST      1 > LOAD_CONST      2 > BINARY_ADD > > the constant folding code will simply replace them with a single > > LOAD_CONST      3 > > When working with such kind of optimizations, the temptation is to > apply them at any situat

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-07 Thread Cesare Di Mauro
On Tue, Apr 7, 2009 07:22PM, Guido van Rossum wrote: >> In my experience it's better to discover a bug at compile time rather >> than >> at running time. > > That's my point though, which you seem to be ignoring: if the user > explicitly writes "1/0" it is not likely to be a bug. That's very > diff

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-07 Thread Guido van Rossum
On Tue, Apr 7, 2009 at 9:46 AM, Cesare Di Mauro wrote: > On Tue, Apr 7, 2009 06:25PM, Guido van Rossum wrote: >> Well I'm sorry Cesare but this is unacceptable. As Skip points out >> there is plenty of code that relies on this. > > Guido, as I already said, in the final code the normal Python beha

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-07 Thread Cesare Di Mauro
On Tue, Apr 7, 2009 06:25PM, Guido van Rossum wrote: > Well I'm sorry Cesare but this is unacceptable. As Skip points out > there is plenty of code that relies on this. Guido, as I already said, in the final code the normal Python behaviour will be kept, and the stricter one will be enabled solely

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-07 Thread Guido van Rossum
Well I'm sorry Cesare but this is unacceptable. As Skip points out there is plenty of code that relies on this. Also, consider what "problem" you are trying to solve here. What is the benefit to the user of moving this error to compile time? I cannot see any. --Guido On Tue, Apr 7, 2009 at 8:19 A

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-07 Thread Cesare Di Mauro
In data 07 aprile 2009 alle ore 17:19:25, ha scritto: > > Cesare> The only difference at this time is regards invalid operations, > Cesare> which will raise exceptions at compile time, not at running > Cesare> time. > > Cesare> So if you write: > > Cesare> a = 1 / 0 > > Ce

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-07 Thread skip
Cesare> The only difference at this time is regards invalid operations, Cesare> which will raise exceptions at compile time, not at running Cesare> time. Cesare> So if you write: Cesare> a = 1 / 0 Cesare> an exception will be raised at compile time. I think I have to ca

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-07 Thread Paul Moore
2009/4/7 Cesare Di Mauro : > The principle that I followed on doing constant folding was: "do what Python > will do without constant folding enabled". > > So if Python will generate > > LOAD_CONST      1 > LOAD_CONST      2 > BINARY_ADD > > the constant folding code will simply replace them with a

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-07 Thread Andrew McNamara
On 07/04/2009, at 7:27 AM, Guido van Rossum wrote: On Mon, Apr 6, 2009 at 7:28 AM, Cesare Di Mauro wrote: The Language Reference says nothing about the effects of code optimizations. I think it's a very good thing, because we can do some work here with constant folding. Unfortunately the

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-07 Thread Cesare Di Mauro
On Apr 07, 2009 at 02:10AM, Steven D'Aprano wrote: > On the other hand, I'm with Guido when he wrote "it is certainly not > right to choose speed over correctness". This is especially a problem > for floating point optimizations, and I urge Cesare to be conservative > in any f.p. optimizations he

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-06 Thread Guido van Rossum
On Mon, Apr 6, 2009 at 5:10 PM, Steven D'Aprano wrote: > On Tue, 7 Apr 2009 07:27:29 am Guido van Rossum wrote: > >> Unfortunately the language reference is not the only thing we have to >> worry about. Unlike languages like C++, where compiler writers have >> the moral right to modify the compile

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-06 Thread Adam Olsen
On Mon, Apr 6, 2009 at 2:22 PM, Mark Dickinson wrote: > Well, I'd say that the obvious solution here is to compute > the constant 2.0**53 just once, somewhere outside the > inner loop.  In any case, that value would probably be better > written as 2.0**DBL_MANT_DIG (or something similar). > > As A

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-06 Thread Guido van Rossum
On Mon, Apr 6, 2009 at 1:22 PM, Mark Dickinson wrote: > On Mon, Apr 6, 2009 at 9:05 PM, Raymond Hettinger wrote: >> The code for the lsum() recipe is more readable with a line like: >> >>  exp = long(mant * 2.0 ** 53) >> >> than with >> >>  exp = long(mant * 9007199254740992.0) >> >> It would be

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-06 Thread Guido van Rossum
On Mon, Apr 6, 2009 at 7:28 AM, Cesare Di Mauro wrote: > The Language Reference says nothing about the effects of code optimizations. > I think it's a very good thing, because we can do some work here with constant > folding. Unfortunately the language reference is not the only thing we have to w

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-06 Thread Mark Dickinson
On Mon, Apr 6, 2009 at 9:05 PM, Raymond Hettinger wrote: > The code for the lsum() recipe is more readable with a line like: > >  exp = long(mant * 2.0 ** 53) > > than with > >  exp = long(mant * 9007199254740992.0) > > It would be ashamed if code written like the former suddenly > started doing t

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-06 Thread Raymond Hettinger
+1 for removing constant folding for floats (besides conversion of -). There are just too many things to worry about: FPU rounding mode and precision, floating-point signals and flags, effect of compiler flags, and the potential benefit seems small. If you're talking about the existing peepho

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-06 Thread Mark Dickinson
[Antoine] > - Issue #5593: code like 1e16+2. is optimized away and its result stored > as > a constant (again), but the result can vary slightly depending on the internal > FPU precision. [Guido] > I would just not bother constant folding involving FP, or only if the > values involved have an

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-06 Thread Cesare Di Mauro
On Mon, Apr 6, 2009 18:57, s...@pobox.com wrote: > > Cesare> At this time with Python 2.6.1 we have these results: > Cesare> def f(): return 1 + 2 * 3 + 4j > ... > Cesare> def f(): return ['a', ('b', 'c')] * (1 + 2 * 3) > > Guido can certainly correct me if I'm wrong, but I believe

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-06 Thread Cesare Di Mauro
On Lun, Apr 6, 2009 16:43, Antoine Pitrou wrote: > Cesare Di Mauro a-tono.com> writes: >> def f(): return ['a', ('b', 'c')] * (1 + 2 * 3) > [...] >> >> With proper constant folding code, both functions can be reduced >> to a single LOAD_CONST and a RETURN_VALUE (or, definitely, by >> a single inst

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-06 Thread skip
Cesare> At this time with Python 2.6.1 we have these results: Cesare> def f(): return 1 + 2 * 3 + 4j ... Cesare> def f(): return ['a', ('b', 'c')] * (1 + 2 * 3) Guido can certainly correct me if I'm wrong, but I believe the main point of his message was that you aren't going to en

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-06 Thread Antoine Pitrou
Cesare Di Mauro a-tono.com> writes: > def f(): return ['a', ('b', 'c')] * (1 + 2 * 3) [...] > > With proper constant folding code, both functions can be reduced > to a single LOAD_CONST and a RETURN_VALUE (or, definitely, by > a single instruction at all with an advanced peephole optimizer). Lis

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-04-06 Thread Cesare Di Mauro
On Mar 29, 2009 at 05:36PM, Guido van Rossum wrote: >> - Issue #5593: code like 1e16+2. is optimized away and its result stored >> as >> a constant (again), but the result can vary slightly depending on the >> internal >> FPU precision. > > I would just not bother constant folding involving

Re: [Python-Dev] pyc files, constant folding and borderline portability issues

2009-03-29 Thread Guido van Rossum
On Sun, Mar 29, 2009 at 9:42 AM, Antoine Pitrou wrote: > There are a couple of ancillary portability concerns due to optimizations > which > store system-dependent results of operations between constants in pyc files: > > - Issue #5057: code like '\U00012345'[0] is optimized away and its result

[Python-Dev] pyc files, constant folding and borderline portability issues

2009-03-29 Thread Antoine Pitrou
Hello, There are a couple of ancillary portability concerns due to optimizations which store system-dependent results of operations between constants in pyc files: - Issue #5057: code like '\U00012345'[0] is optimized away and its result stored as a constant in the pyc file, but the result should