Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'cpython': 0.05; 'finally:': 0.05; 'mrab': 0.05; 'cleanup': 0.07; 'cc:addr:python- list': 0.09; '*is*': 0.09; 'instruction.': 0.09; 'wrong,': 0.09; 'python': 0.10; ':-)': 0.12; 'stack': 0.13; 'wed,': 0.15; '10:00': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'gregory': 0.16; 'iirc': 0.16; 'operation.': 0.16; 'x()': 0.16; 'wrote:': 0.16; 'first.': 0.18; 'language': 0.19; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'explicit': 0.22; 'function,': 0.22; 'lawrence': 0.22; 'seems': 0.23; 'tried': 0.24; 'header:In-Reply-To:1': 0.24; 'sort': 0.25; 'wondering': 0.25; "doesn't": 0.26; 'chris': 0.26; 'message- id:@mail.gmail.com': 0.27; 'restrict': 0.27; 'assembly': 0.29; 'tail': 0.29; 'instruction': 0.29; "i'm": 0.30; 'subject:/': 0.30; 'code': 0.30; '15,': 0.30; 'implement': 0.32; 'says': 0.32; 'anybody': 0.32; 'problem': 0.33; 'call,': 0.33; 'jump': 0.33; 'languages': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'done': 0.35; 'too': 0.36; 'instead': 0.36; 'alone': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'turn': 0.37; 'itself': 0.38; 'or,': 0.38; 'mark': 0.40; 'still': 0.40; 'some': 0.40; 'ever': 0.60; 'your': 0.60; 'designers': 0.72; 'jul': 0.72; 'chrisa': 0.84; 'to:none': 0.91; 'succeed.': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=oql3jPkOGvkZMmaQ29HSU7bQnFesGCnRe7hMk5f99GQ=; b=vfp5W9WQbthWUppygJnvi//NIJ91/8NxEhCiheBBFM9+WUjPFyjRsg3GcX+I2DcAvG KpHFL6bifRZKKC6GE9l43FfXaEgz4tVT8qX4NvKsfu42WUwURJoAuWnRExp8Fwyhg+wi Kl4L7jEfSBZdMDSC04sRPL+IRkp277JQxzCf3Chamh3aQOHQhGM2zgoHLj0Fjk5qbYmo v7SCriIUtpCaAdEraomurdwdT0hImhjtRlZ/hQWuHZ8daPVgZWZbTfqb7UHNSqmgAgh0 MEjyqOYuBBzyCk/PN5b8uBJcNZVsRMOi/yd9fTGhii9qJqYMHpF+E8R3YsmtojlJ5snm hmwQ== MIME-Version: 1.0 X-Received: by 10.50.87.74 with SMTP id v10mr9962067igz.37.1436962427904; Wed, 15 Jul 2015 05:13:47 -0700 (PDT) In-Reply-To: <55A64B70.1000204@mrabarnett.plus.com> References: <55A3A853.4040006@rece.vub.ac.be> <55A3C366.6060602@rece.vub.ac.be> <87fv4r1fre.fsf@jester.gateway.sonic.net> <87bnff1eks.fsf@jester.gateway.sonic.net> <55A64B70.1000204@mrabarnett.plus.com> Date: Wed, 15 Jul 2015 22:13:47 +1000 Subject: Re: Possibly Pythonic Tail Call Optimization (TCO/TRE) From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: , Newsgroups: comp.lang.python Message-ID: Lines: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1436962436 news.xs4all.nl 2966 [2001:888:2000:d::a6]:37250 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:93871 On Wed, Jul 15, 2015 at 10:00 PM, MRAB wrote: > On 2015-07-15 12:22, Mark Lawrence wrote: >> >> On 15/07/2015 10:13, Gregory Ewing wrote: >>> >>> Chris Angelico wrote: >>>> >>>> I'm still interested in the explicit "replace current stack frame with >>>> this call" operation. Calling it "goto" seems wrong, as most languages >>>> with goto restrict it to _within_ a function, >>> >>> >>> This just suggests to me is that most language designers >>> are not very imaginative. :-) >>> >>> A tail call *is* a goto. That's how you implement one in >>> assembly language -- you write a jump instruction instead >>> of a call instruction. The jump doesn't have to be to >>> the same function. >>> >> >> IIRC the realms of the C setjmp and longjmp. I'm just wondering out >> aloud if anybody has ever tried playing with cPython using these, or >> whether the code and/or Python itself is just too complex to allow this >> to even be tried, let alone succeed. >> > The problem with longjmp is that it could inadvertently bypass some > clean-up code or deallocation, or, in the case of CPython, a DECREF. Which really says that TCO is impossible if you have any sort of clean-up or deallocation to be done after the call begins. The only way to truly turn your tail call into a GOTO is to do all your cleanup first. "try: return x() finally: more_code" is not a tail call, nor is it if you have "except:" in place of "finally:". ChrisA