Continuations and Stackless Python Where Do You Want To Jump Today?

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
Procedures. 2 Procedure Definition A procedure is a mechanism for abstracting a group of related operations into a single operation that can be used repeatedly.
Programming Languages and Paradigms
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Head First Python Chapter 4 Persistence: Saving Data to Files SNU IDB Lab.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Gary MarsdenSlide 1University of Cape Town Statements & Expressions Gary Marsden Semester 2 – 2000.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
Six compound procedures and higher-order procedures.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
Honors Compilers Addressing of Local Variables Mar 19 th, 2002.
Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week.
Run time vs. Compile time
Chapter 9: Subprogram Control
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Lecture 4: Advanced Instructions, Control, and Branching cont. EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Distributed-Memory Programming Using MPIGAP Vladimir Janjic International Workhsop “Parallel Programming in GAP” Aug 2013.
Memory organization - storing variables efficiently in the RAM memory.
Continuations and Stackless Python Where Do You Want To Jump Today?
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
Remote Procedure Calls Adam Smith, Rodrigo Groppa, and Peter Tonner.
Functions and Procedures. Function or Procedure u A separate piece of code u Possibly separately compiled u Located at some address in the memory used.
Stackless Python: programming the way Guido prevented it intended.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Programming Languages and Paradigms Imperative Programming.
CIT 590 Intro to Programming Lecture 3. Pair programming logistics You and your partner submit 1 assignment Figure out who the submitter will be PLEASE.
Exception Compiler Baojian Hua
Stackless Python: programming the way Guido prevented it.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
An Introduction. What is Python? Interpreted language Created by Guido Van Rossum – early 90s Named after Monty Python
Lecture 19: Control Abstraction (Section )
Efficient software-based fault isolation Robert Wahbe, Steven Lucco, Thomas Anderson & Susan Graham Presented by: Stelian Coros.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Prof. Necula CS 164 Lecture 171 Operational Semantics of Cool ICOM 4029 Lecture 10.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
PYTHON FOR HIGH PERFORMANCE COMPUTING. OUTLINE  Compiling for performance  Native ways for performance  Generator  Examples.
Introduction to Exceptions in Java CS201, SW Development Methods.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Assignment 5 is posted. Exercise 8 is very similar to what you will be doing with assignment 5. Exam.
Chapter 6 Functions The Tic-Tac-Toe Game. Chapter Content In this chapter you will learn to do the following: 0 Write your own functions 0 Accept values.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Run-Time Environments Chapter 7
Topic: Functions – Part 2
Chapter 5 Conclusion CIS 61.
Functions and Procedures
Chapter 9 :: Subroutines and Control Abstraction
UNIT V Run Time Environments.
When a function is called...
Yan Shi CS/SE 2630 Lecture Notes
Lecture 20 – Practice Exercises 4
Chapter 10 Def: The subprogram call and return operations of
Python Reserved Words Poster
Presentation transcript:

Continuations and Stackless Python Where Do You Want To Jump Today?

Stacklessssshhh Python... And the clueless implementor...

Why Stackless? Recursion depth limits –Platform dependant Generators Coroutines Micro-Threads  Persistent State  –Pickle the running program –Mobile Agents Continuations Modify running Program 

Fact Sheet Minimum C Stack resources Unlimited recursion 100% binary compatible to Standard Python –Except for left bugs 5% faster executable on Win32faster Full support for first-class continuations Continuations included as extension module As portable as Standard Python Breaks 4K stack limit on PalmIII

What Is a Continuation x = 2; y = x + 1; z = x * 2 The continuation ofIs this! The current continuation at any point in the execution of a program is an abstraction of the rest of the program

Continuation Objects Tiny interface objects to Python frames Continuations are callable objects Calling brings us immediately back to where we left off Can pass and return a value

Implementing a Loop def looptest(n): this = continuation.current() k = this.update(n) if k: this(k-1) else: del this.link Loosing frame reference Saved assignment Jump back

Building a Generator (1)

Building a Generator (2) Catch the current caller Jump to saved frame

Easy? Almost, but... The given principle can implement coroutines We can switch to any other disjoint frame chain Trouble when more than one continuation owns the target frame!

a-frame with refcount > 1 b-frame still referenced c-frame wants to return Critical Situation

Frame Ownership In order to jump to a frame, we must own it We own a frame iff its refcount is 1* The program must ensure this by building clone frames (solution, first half). We cannot just make a copy, but keep the current fram in place... (solution, second half) *Note: This rule is strong enough to build a complete object system!

a-frame as continuation b-frame still referenced c-frame wants to return new frame with contents of a-frame Push-back Frame

3 rd Half of Solution Upto version 0.3: –Managing growing chains of pushed-back frames becomes tedious. Continuation frames pile up. Runtime isn‘t O(1) Insert an extra node object that always walks with the real frame does the trick Runtime is O(1) now

a-frame as continuation b-frame still referenced c-frame wants to return new frame with contents of a-frame PyCoNode Node Object

a-frame as continuation b-frame still referenced c-frame wants to return new frame with contents of a-frame PyCoNode Final View Rc=1 Rc=3 Rc=1 (exec) About to die Rc=1

def a(x):... b(x+1)... def b(x):... c(x*x)... def c(x):... print "x=",x... a(42) interactive frame a-frame b-frame c-frame Frame StackC Stack (simplified) PyRun_InteractiveOne PyEval_EvalCode eval_code2 call_function eval_code2... eval_code2... eval_code2... Standard „stackfull“ Python

c-frame PyEval_Frame_Dispatch this *can* still happen Dispatcher Object eval_code2_loop Bad case: an internal function does a call via the old interface interactive frame a-frame b-frame PyRun_InteractiveOne PyEval_EvalCode eval_code2_loop PyEval_Frame_Dispatch always calls from here Dispatcher Object Good case: always the same dispatcher Stackless Python

About Optimization Moved opcode/oparg into the instructions Shortened the big switch Mapped Error handling to pseudo-opcodes Shortened lifetime of state variables Inc/decref not macro but inline (win32) Would gain about 15% for standard Python –(but this is not what I want )