Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.022 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'subject:code': 0.07; 'python': 0.09; "'return": 0.09; 'difference,': 0.09; 'def': 0.10; 'dec': 0.15; '*why*': 0.16; 'bug,': 0.16; 'subject:dead': 0.16; 'unreachable': 0.16; 'wed,': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'seems': 0.23; 'allows': 0.25; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'correct': 0.28; "d'aprano": 0.29; 'loads': 0.29; 'steven': 0.29; 'keyword': 0.30; 'code': 0.31; 'not.': 0.32; 'could': 0.32; 'certain': 0.33; 'function.': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'subject:?': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'should': 0.36; 'rather': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'end': 0.40; 'think': 0.40; 'days': 0.60; 'between': 0.63; 'worth': 0.63; 'here': 0.65; 'become': 0.65; 'believe': 0.69; '13)': 0.84; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=KwQiO6HobfMfBeF5kWCvxkevaaq4LOlcaUbCrhyF7a4=; b=dqaO5yT5SSIXcE7kmIhT/Al2zQmsg18smBqt9hy8IBRKPIUau3nld5OCv40lVa4UDT kHUK70BMxab/28CMhazM/TJejRFZki6wb9egU9uEjM5SWXQQcJV76lZx/7WFVVcCyDNb RaIodX1Ilp6y6tr72++l4P1rWwPdkTncOS9Emxbe/zZbhaEROZjiIP0tsoiovCnwM8Ia P7HZ+ekUK9AgPwGqobPNa56Klmqkoma4x5oaioaA/qjPdSWzD5qg7y6/V/VmC85fljU+ Eew5n4vnO3R4dcp4gPeRcaCUqxcFlCMpV6o/xRogY/vaprLb0TaWzWu9x4iKe5p7Eftl R7PQ== MIME-Version: 1.0 In-Reply-To: <50bf85c0$0$29994$c3e8da3$5496439d@news.astraweb.com> References: <50bf85c0$0$29994$c3e8da3$5496439d@news.astraweb.com> From: Ian Kelly Date: Wed, 5 Dec 2012 10:59:26 -0700 Subject: Re: why does dead code costs time? To: Python Content-Type: text/plain; charset=ISO-8859-1 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: 84 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354730398 news.xs4all.nl 6944 [2001:888:2000:d::a6]:47247 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34302 On Wed, Dec 5, 2012 at 10:34 AM, Steven D'Aprano wrote: > The difference is almost certain between the LOAD_CONST and the > LOAD_GLOBAL. > > As to *why* there is such a difference, I believe that's a leftover from > early Python days when None was not a keyword and could be reassigned. I think this should even be considered a bug, not just a missing optimization. Consider: >>> globals()['None'] = 42 >>> def f(x): ... return None ... print(x) ... >>> f('test') 42 The use of the LOAD_GLOBAL allows None to effectively be reassigned. It's also worth noting that: >>> def f(x): ... return ... print(x) ... >>> dis.dis(f) 2 0 LOAD_CONST 0 (None) 3 RETURN_VALUE 3 4 LOAD_GLOBAL 0 (print) 7 LOAD_FAST 0 (x) 10 CALL_FUNCTION 1 (1 positional, 0 keyword pair) 13 POP_TOP So if you just write 'return' rather than 'return None', you get the correct bytecode. Additionally, the use of LOAD_GLOBAL instead of LOAD_CONST seems to be linked to having unreachable code at the end of the function. This is fine: >>> def f(x): ... if x: ... return None ... print(x) ... >>> dis.dis(f) 2 0 LOAD_FAST 0 (x) 3 POP_JUMP_IF_FALSE 10 3 6 LOAD_CONST 0 (None) 9 RETURN_VALUE 4 >> 10 LOAD_GLOBAL 1 (print) 13 LOAD_FAST 0 (x) 16 CALL_FUNCTION 1 (1 positional, 0 keyword pair) 19 POP_TOP 20 LOAD_CONST 0 (None) 23 RETURN_VALUE But this is not. Note here that *both* loads of None become LOAD_GLOBAL in this case: >>> def f(x): ... if x: ... return None ... return None ... print(x) ... >>> dis.dis(f) 2 0 LOAD_FAST 0 (x) 3 POP_JUMP_IF_FALSE 13 3 6 LOAD_GLOBAL 0 (None) 9 RETURN_VALUE 10 JUMP_FORWARD 0 (to 13) 4 >> 13 LOAD_GLOBAL 0 (None) 16 RETURN_VALUE 5 17 LOAD_GLOBAL 1 (print) 20 LOAD_FAST 0 (x) 23 CALL_FUNCTION 1 (1 positional, 0 keyword pair) 26 POP_TOP