Settings

Theme

Goto in Python

github.com

170 points by wallunit 10 years ago · 44 comments

Reader

david-given 10 years ago

Hey, awesome! And it produces a true goto as well, working on the bytecode level. I wonder if there's a way to remove the extra nops?

This is really, really useful for doing language-to-language translation. Without the ability to arbitrarily branch from basic block to basic block, it's possible for the basic block graph produced by a program in one language to be simply inexpressible in your target language. You end up having to fake it with a while loop and switch statement, which has horrible performance implications.

(Emscripten has some exotic algorithms to try and express the source program's basic block graph using the target language's structured programming constructs. This is vitally important to make goto-less Javascript perform well, but it can't work in all cases, and some C functions will force Emscripten to fall back to loop-and-switch. And, of course, these functions are typically the ones which have been carefully hand-optimised for speed...)

  • wallunitOP 10 years ago

    > I wonder if there's a way to remove the extra nops?

    Yes, technically it would be possible to remove the code instead injecting NOPs. But then I'd have to adjust the jump targets. However, I don't think these NOPs are too bad. Note that goto jumps directly after the NOP ramp of the label, and the NOPs of the goto itself are never reached. The only scenario where NOPs are actually seen by the interpreter is when the natural code path visits a label.

    EDIT: Instead re-assembling the bytecode and adjusting jumps, I went to use JUMP_FORWARD(3) instead 7 NOPs now. Note that there are still 4 NOPs left to fill the gap, these however are never executed as they are skipped by the preceding jump instruction. https://github.com/snoack/python-goto/commit/2b0f5e5069cbb88...

    • masklinn 10 years ago

      Is it certain that JUMP_FORWARD is faster than a few NOPs?

      • wallunitOP 10 years ago

        I did run the example from the README, with "%timeit range(0, 1000)" in ipython:

        10000 loops, best of 3: 72.9 µs per loop @ CPython 2.7.10 with NOP

        10000 loops, best of 3: 77.2 µs per loop @ CPython 2.7.10 with JUMP_FORWARD

        10000 loops, best of 3: 106 µs per loop @ CPython 3.5.0 with NOP

        10000 loops, best of 3: 106 µs per loop @ CPython 3.5.0 with JUMP_FORWARD

        100000 loops, best of 3: 8.6 µs per loop @ PyPy 2.4.0 with NOP

        100000 loops, best of 3: 8.7 µs per loop @ PyPy 2.4.0 with JUMP_FORWARD

        To my surprise, in fact, JUMP_FORWARD isn't any faster than 7 NOPs. In Python 2.7, JUMP_FORWARD is even slower. So reverted:

        https://github.com/snoack/python-goto/commit/d19d244a9e5efdf...

        Thanks for the pointer!

    • david-given 10 years ago

      D'oh. Yes, of course.

      It'd be interesting to know whether peculiar basic block graphs upset PyPy's JIT. Apparently Lua's JIT is much less good at optimising programs that don't look like normal Lua.

  • masklinn 10 years ago

    > I wonder if there's a way to remove the extra nops?

    Sure you just have to recompute all the jump destinations and patch the jumps themselves.

mrsirduke 10 years ago

The technique used to implement this is rather interesting, and the implementation itself[1] is very understandable.

I'm sure neither should be used in a production project.

[1]: https://github.com/snoack/python-goto/blob/master/goto.py

simgidacav 10 years ago

https://www.xkcd.com/292/

Sorry, I had to.

tveita 10 years ago

This is cute but of course horribly unsafe since it doesn't respect control statements.

Some examples you'd expect to work that will crash the interpreter:

  @with_goto
  def fun1():
   while True:
    for x in [1]:
     goto .next
    label .next
  
  @with_goto
  def fun2():
   goto .next
   for x in [1]:
    label .next
  
  @with_goto
  def fun3():
   while True:
    try:
     goto .next
    finally:
     print('skipped')
    label .next
  • kazinator 10 years ago

    Note how Lisp's TAGBODY/GO doesn't have this problem.

      (loop
        (tagbody
          (loop for x in '(1 2 3)
                do (go next))
        next
          (print 'out)))
    
    (OUT is printed repeatedly.)

    How it works is that tagbody contains a bunch of labels mixed with forms. The tagbody establishes an exit point for GO expressions used in these forms. When a (go label) occurs, it works by abandoning the current sub-form being evaluated, and initiating a control transfer to the exit point, which then transfers control to the appropriate label.

    The upshot is that whatever form is interrupted will cleanly unwind, as necessary:

      (tagbody
        (unwind-protect (progn 
                          (go out)
                          (progn 'never-printed))
          (print 'bye-bye))
       out)
    
    Note that you can only have labels and GO in certain forms like TAGBODY and PROG, not just willy nilly in any construct. The labels of a given TAGBODY are all on the same level of nesting: immediate children of the TAGBODY form. SO this isn't possible:

      (tagbody
         (go impossible)
         (let ((x 42))
           impossible x))
    
    Here, impossible is not considered a label associated with the tagbody, so the (go impossible) is branching to a nonexistent label. If it were allowed, of course it would create the problem of what becomes of the initialization of x.

    Thus, compilers only have to reason about GO within specific constructs, and rely on those GO's to only be performing abandonment followed by a simple lateral move.

  • wallunitOP 10 years ago

    Fixed: https://github.com/snoack/python-goto/commit/a46dbe57e9cfa4f...

    Jumps out of loops and other blocks work now. However, there are some limitations:

    1. As I only have 4 bytes left to inject POP_BLOCK instructions, you can't exit more than 4 blocks with a goto. But this is handled and results into a SyntaxError rather than crashing Python.

    2. Jumps into loops still doesn't work. However, it's handled now, and causes a SyntaxError as well.

    3. When jumping out of a try- or with-block the finalizer is skipped.

  • RussianCow 10 years ago

    How would you expect the second one to work?

    • tveita 10 years ago

      I guess similarly to C:

        for i in foo:
          print(42)
      
      would be considered equivalent to something like

        it = iter(foo)
        while True:
         try:
          i = next(it)
         catch StopIteration:
          break
         print(42)
      
      and jumping to print(42) would skip the init and first iteration expression.

      Of course that's non-obvious behaviour, so in practice you would make it a compile time error. A careful language designer might even decide to not support goto at all. :)

polemic 10 years ago

Incidentally, from 2009: http://code.activestate.com/recipes/576944-the-goto-decorato...

And presented at KiwiPycon 2014: https://www.youtube.com/watch?v=DdU8I09BGsU

moretti 10 years ago

Here's another Python 3 compatible implementation: https://github.com/cdjc/goto

ssanderson11235 10 years ago

If you're interested in this sort of thing, you might also check out https://github.com/llllllllll/codetransformer, which is a general-purpose library for doing these sorts of shenanigans in an almost-sane way. (Disclaimer: I'm one of the library authors).

As an example, I've got a PR open right now that lets you do things like:

    @mutable_locals
    def f():
        out = []
        x = 1
        out.append(x)
        locals().update({'x': 2, 'y': 3})
        out.append(x)
        out.append(y)
        return out

    assert f() == [1, 2, 3]
which works using a combination of ctypes hackery and replacing all LOAD_FAST instructions with appropriately-resolved LOAD_NAME instructions.
veddox 10 years ago

47 years after Dijkstra's "Go To Statement Considered Harmful"[1], the Structured Programming Wars flare up again on Hacker News :D

Neat hack, though.

[1] http://www.u.arizona.edu/~rubinson/copyright_violations/Go_T...

Veedrac 10 years ago

I was aware of the April Fools version.

The idea that it needs optimizing, no doubt because someone's using it in a hot code, is hilarious. That's either the best satire or the worst truth I've heard all day.

  • sklogic 10 years ago

    I am totally going to use this approach in a production code. Because I am using generated code a lot.

ainiriand 10 years ago

WHY?! Good experiment anyway, well done.

  • ceronman 10 years ago

    Because it's a fun exercise to understand the internals of CPython. That's what hacking is all about.

  • aap_ 10 years ago

    Because goto can be useful?

    • Walkman 10 years ago

      You are not a Python programmer for sure :)

      • JupiterMoon 10 years ago

        In e.g. fortran there are labelled loops. So one can break out of an inner loop back to the main program flow. I know that this wouldn't be pythonic but sometimes I just want to finish the one off script and get back to something more fun and I miss this ability. I still don't think arbitrary gotos are great.

        • k8tte 10 years ago

          You can resolve this in basically every language by reworking your code in smaller pieces, and simply returning from a loop. Jumping left and right should only be signalling that you are Doing It Wrong (tm)

      • emidln 10 years ago

        I've seen exceptions masquerading as goto in the wild in multiple python codebases (and not because I added them). `raise HttpResponse(status=401, body="unauthorized")` inside some utility method is little more than goto exit in a lot of C code.

        • Walkman 10 years ago

          Using Exceptions this way in Python code is common practice and considered "Pythonic" and it's different than goto:

          - an Exception only can bubble up the call stack, not to an arbitrary location in the code

          - it can be caught whatever level during that path

WalterBright 10 years ago

goto is sometimes the right tool for the job.

  • Intermernet 10 years ago

    Agreed. The Linux kernel being a good example.

    See http://blog.regehr.org/archives/894 , http://thread.gmane.org/gmane.linux.kernel/84823/focus=85436 and https://github.com/torvalds/linux/search?q=goto for some discussion and examples.

    From Mr Torvalds (as diplomatic as ever...):

    >I think goto's are fine, and they are often more readable than large amounts of indentation. That's _especially_ true if the code flow isn't actually naturally indented (in this case it is, so I don't think using goto is in any way _clearer_ than not, but in general goto's can be quite good for readability).

    >Of course, in stupid languages like Pascal, where labels cannot be descriptive, goto's can be bad. But that's not the fault of the goto, that's the braindamage of the language designer.

  • Retra 10 years ago

    Theorem: "X is sometimes the right tool for the job" is true for all X.

    Now could we maybe explain what jobs we're talking about and why it's the right tool for them?

    • kazinator 10 years ago

      Goto is absolutely the right tool for: "the instruction pointer finds itself here, and at this critical juncture in the algorithm, one would rather prefer that it were over there, for want of correctness, if not beauty."

    • WalterBright 10 years ago

      One job is when auto-generating source code. It's much, much easier to generate a graph as a collection of blocks connected by various goto's and if statements.

      This does not adversely affect modern optimizers - they'll figure out the loops from the goto graph. At least the D compiler does.

  • parados 10 years ago

    For example for writing Python C extensions using 'Pythonic' C: https://github.com/paulross/PythonExtensionPatterns/blob/mas...

csl 10 years ago

Nice. But does it handle extended jumps?

I know this is a problem for at least the Byteplay module.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection