Issue 1465406: Allowing the definition of constants (original) (raw)

Created on 2006-04-05 23:30 by ciw42, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (7)
msg54774 - (view) Author: Chris Wilson (ciw42) Date: 2006-04-05 23:30
One of the current optimizations due in v2.5 includes constant folding of expressions, which as it stands serves as a way of somply getting rid of a redundant arithmetic operations and the like. In practice, it's rare a developer would leave an expression such as "2+3" sat in his/her code, but by allowing the declaration of constants within a script, it could make this new feature *much* more useful. As an example, in a recent script I had the following at the top, outside the main loop: SCREEN_WIDTH=640 SCREEN_HEIGHT=480 SCREEN_RATIO=SCREEN_WIDTH/SCREEN_HEIGHT As SCREEN_RATIO is used a number of times during my main loop, it makes sense to pre-calculate it to avoid the extra processing, but if the compiler were aware that SCREEN_WIDTH and SCREEN_HEIGHT were constants, it could optimise out the calculation and I could include the calculation in-place. I frequently make use of "constants" to make my code more readable, and wonder whether there is any performance penalty or lack of optimisation going on due to them in fact being regular variables?
msg54775 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2006-04-05 23:57
Logged In: YES user_id=80475 Python is too dynamic for this kind of optimization to be done automatically. If those "constants" are defined at the module level, they can be changed by code outside the module. Even within the module, it would take a thorough analysis of the code to determine that nothing was trying to alter the value of the global variable. If the "constant" is defined inside a function, it is still a local variable subject to change by later lines in function. Your best bet is to use the bind_consts recipe at ASPN. It will automatically turn some global references into locals: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/277 940 It may be possible to adapt the recipe to go an additional step and fold the globals "constants".
msg54776 - (view) Author: Chris Wilson (ciw42) Date: 2006-04-06 21:59
Logged In: YES user_id=1018283 I've re-opened this, as I don't feel it would be difficult to implement or require any fundamental changes to the parser or runtime. In fact, it would be very easy, and potentially very useful beyond the scope of my initial suggestion. Appologies to rhettinger if this seems rude, but I would ask that you give the following some consideration. The addition of a "const" or similar compiler directive would allow the compiler to simply do an on-the-fly substitution for the specified value/string etc. There would be no code analysis required, and adding this type of functionality would carry no real overheads or further complicate the compilation process. There would be no changes required within the runtime. Once substituted, the already incorporated compiler constant folding features would then come into play. I suppose, that what I'm suggesting is effectively a basic pre-compiler macro feature. This in itself may prove useful in many other situations.
msg54777 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2006-04-10 13:55
Logged In: YES user_id=21627 The problem is that declaring the value assignment const doesn't help. Consider this: from math import pi def area(r): return r*r*pi pi = 4 print area(10) So even though math.pi might be declared as a constant, hard-coding its value into the function area would break this program - the value of the variable pi might change not change inside math, but it might change where it is imported.
msg54778 - (view) Author: Chris Wilson (ciw42) Date: 2006-04-28 13:38
Logged In: YES user_id=1018283 I see your point, and it's a good example of why using namespaces is so important, but in practice, with my proposal in place, the code you propose simply wouldn't compile. Assuming the compiler simply substituted the literal "3.1415" for "pi" as I've proposed, you'd end up with "3.1415 = 4", and a syntax error for trying to assign to a literal value. You'd not get as far as running the code, so in practice there'd be no issues with it running incorrectly. Being able to declare constants is important as it allows the compiler to make the sort of optimistations I mentioned previously.
msg54779 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2006-05-09 04:51
Logged In: YES user_id=80475 Introducing a new keyword, "const", requires a PEP. Such a PEP should be backed-up by timings that demonstrate performance improvements impressive enough to warrant a change. In my experience, replacing global lookups with constants almost never produces much a speed-up in real code. If you write a PEP, it needs to first go through discussion on comp.lang.python. Also, you should find out how this would affect Jython and IronPython.
msg54780 - (view) Author: Collin Winter (collinwinter) * (Python committer) Date: 2007-03-30 07:11
The PEP Raymond mentioned hasn't been written (to Google's knowledge), and I can't see much support for it even if it were to be written. The performance benefits from a "const" keyword would in my view be minimal and not worth adding the necessary complication to the compiler. Closing.
History
Date User Action Args
2022-04-11 14:56:16 admin set github: 43166
2006-04-05 23:30:05 ciw42 create