Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Terry Reedy Newsgroups: comp.lang.python Subject: Re: Question about math.pi is mutable Date: Tue, 10 Nov 2015 03:30:05 -0500 Lines: 53 Message-ID: References: <563eba85$0$1611$c3e8da3$5496439d@news.astraweb.com> <87ziyowy83.fsf@elektro.pacujo.net> <8537wghwjb.fsf@benfinney.id.au> <85y4e8ggrj.fsf@benfinney.id.au> <85twowgfm6.fsf@benfinney.id.au> <564081e7$0$1592$c3e8da3$5496439d@news.astraweb.com> <56415873$0$1614$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de i5rv6+B3imP79fRpe/MKKQBXFfgt3KSvY4C26lguAiiw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'modify': 0.04; 'true,': 0.04; 'compiler': 0.05; 'modified': 0.05; 'subject:Question': 0.05; 'builtin': 0.07; 'interpreted': 0.07; 'skip:/ 10': 0.07; 'builtins': 0.09; 'executes': 0.09; 'len(x)': 0.09; 'lookup': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'semantics': 0.09; 'advance': 0.10; 'python': 0.10; 'jan': 0.11; 'interpreter': 0.15; 'prototype': 0.15; 'builtins.': 0.16; 'did,': 0.16; 'modified.': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'runtime.': 0.16; 'set,': 0.16; 'suite.': 0.16; 'unsafe': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'runs': 0.18; 'changes': 0.20; 'fairly': 0.22; 'function,': 0.22; 'decide': 0.23; 'nearly': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; "doesn't": 0.26; '(which': 0.26; 'header:X -Complaints-To:1': 0.26; 'not.': 0.27; 'function': 0.28; "skip:' 10": 0.28; 'measure': 0.29; 'code': 0.30; 'another': 0.32; 'knows': 0.32; 'says': 0.32; 'compiled': 0.32; 'plans': 0.32; 'changed': 0.33; 'class': 0.33; 'avoiding': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'could': 0.35; 'done': 0.35; 'replace': 0.35; 'something': 0.35; 'but': 0.36; 'created': 0.36; 'to:addr :python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'say': 0.37; 'received:org': 0.37; 'test': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'space': 0.40; 'called': 0.40; 'some': 0.40; 'avoid': 0.61; 'tracking': 0.61; 'inline': 0.63; 'lose': 0.63; 'pays': 0.63; 'more': 0.63; '"yes,': 0.84; 'dict,': 0.84; 'dict.': 0.84; 'penalty': 0.84; 'shadows': 0.84; 'victor': 0.84; 'received:fios.verizon.net': 0.91; 'shadow': 0.91; 'safe.': 0.93; 'proposal,': 0.95; 'imagine': 0.96 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: pool-173-59-124-74.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 In-Reply-To: <56415873$0$1614$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98580 On 11/9/2015 9:37 PM, Steven D'Aprano wrote: > The compiler doesn't need to decide *in advance* whether the attribute might > have changed. It knows whether it has changed or not *at runtime*. You are using 'compiler' when you should, to avoid confusion, use 'interpreter'. > It's one thing to say that *in principle* any function might modify or > shadow builtins. That's true, because we don't know what's inside the > function. But the compiler knows, because it actually executes the code 'interpreter' > inside the function and can see what happens when it does. It doesn't have > to predict in advance whether or not calling `func(x)` shadows the builtin > `len` function, *it can see for itself* whether it did or not. > > At compile time, `func(x)` might do anything. But at runtime, we know > exactly what it did, because it just did it. > > Imagine that the compiler keeps track of whether or not builtins has been /compiler/code compiled by the compiler and interpreted by the interpreter (which could be the CPU)/ > modified. Think of it as a simple "dirty" flag that says "yes, builtins is > still pristine" or "no, something may have shadowed or modified the > builtins". That's fairly straight-forward: builtins is a dict, and the > compiler can tell whether or not __setitem__ etc has been called on that > dict. Likewise, it can keep track of whether or not a global has been > created that shadows builtins: some of that can be done statically, at > compile-time, but most of it needs to be done dynamically, at runtime. This is more or less Victor Stinner's proposal, and he has a working prototype that runs nearly all the test suite. He now plans to refine it and measure changes in space and time usage. > If the flag is set, the compiler knows that the optimization is unsafe and > it has to follow the standard name lookup, and you lose nothing: the > standard Python semantics are still followed. But if the flag is clear, the > compiler knows that nothing has shadowed or modified builtins, and a whole > class of optimizations are safe. It can replace a call to (say) len(x) with > a fast jump, avoiding an unnecessary name lookup in globals, and another > unnecessary name lookup in builtins. Or it might even inline the call to > len. Since *most* code doesn't play tricks with builtins, the overhead of > tracking these changes pays off *most* of the time -- and when it doesn't, > the penalty is very small. -- Terry Jan Reedy