Closure Representations in Higher-Order Programming Languages

Slides:



Advertisements
Similar presentations
Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Advertisements

Recursion vs. Iteration The original Lisp language was truly a functional language: –Everything was expressed as functions –No local variables –No iteration.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
CS 536 Spring Run-time organization Lecture 19.
Prof. Bodik CS 164 Lecture 171 Register Allocation Lecture 19.
Run-Time Storage Organization
Run time vs. Compile time
Register Allocation (via graph coloring)
Catriel Beeri Pls/Winter 2004/5 environment 68  Some details of implementation As part of / extension of type-checking: Each declaration d(x) associated.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Register Allocation (via graph coloring). Lecture Outline Memory Hierarchy Management Register Allocation –Register interference graph –Graph coloring.
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.
4/29/09Prof. Hilfinger CS164 Lecture 381 Register Allocation Lecture 28 (from notes by G. Necula and R. Bodik)
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
CS 403: Programming Languages Lecture 2 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
An Adaptive, Region-based Allocator for Java Feng Qian, Laurie Hendren {fqian, Sable Research Group School of Computer Science McGill.
Runtime Environments Compiler Construction Chapter 7.
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
Operating Systems (CS 340 D) Princess Nora University Faculty of Computer & Information Systems Computer science Department.
COMP3190: Principle of Programming Languages
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
COMP091 – Operating Systems 1 Memory Management. Memory Management Terms Physical address –Actual address as seen by memory unit Logical address –Address.
Simple Generational GC Andrew W. Appel (Practice and Experience, February 1989) Rudy Kaplan Depena CS 395T: Memory Management February 9, 2009.
LINKED LISTS.
Eliminating External Fragmentation in a Non-Moving Garbage Collector for Java Author: Fridtjof Siebert, CASES 2000 Michael Sallas Object-Oriented Languages.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
A Single Intermediate Language That Supports Multiple Implemtntation of Exceptions Delvin Defoe Washington University in Saint Louis Department of Computer.
Design issues for Object-Oriented Languages
Complexity Analysis (Part I)
Storage Allocation Mechanisms
Memory Management.
Introduction to Analysis of Algorithms
Chapter 2 Memory and process management
Module 11: File Structure
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
Lectures linked lists Chapter 6 of textbook
Database Management System
Storage Management.
Chapter 9 – Real Memory Organization and Management
Run-time organization
CS 153: Concepts of Compiler Design November 28 Class Meeting
Memory Locations For Variables
Automatic Memory Management
Optimizing Malloc and Free
Continuations and Compilation
Virtual Memory Hardware
Lecture 2- Query Processing (continued)
Binding Times Binding is an association between two things Examples:
Created By: Asst. Prof. Ashish Shah, J.M.Patel College, Goregoan West
Data Structures & Algorithms
Run Time Environments 薛智文
Scope, Function Calls and Storage Management
CS703 - Advanced Operating Systems
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
COMP755 Advanced Operating Systems
Procedure Linkages Standard procedure linkage Procedure has
Complexity Analysis (Part I)
Dynamic Binary Translators and Instrumenters
Run-time environments
CMPE 152: Compiler Design May 2 Class Meeting
Presentation transcript:

Closure Representations in Higher-Order Programming Languages Vijay Kumar Gurramkonda Ball State University Computer Science Department 11/22/00 CS 689

Introduction Higher Order Functions Functions that take functional arguments. Higher Order Functional Programming Languages. A higher-order functional language is one with nested scope and higher-order functions.

Closures It is a record that contains the machine-code pointer and a way to access the necessary nonlocal variables. One simple kind of closure is just a pair of code pointer and static link. Closures need not be based on static links, any other data structure that gives access to nonlocal variables will do.

Heap-Allocated Activation Records Activation record for a function say “add” must not be destroyed when “add” returns, because it may still serve as an environment to another function say “U”. To solve this problem, we could create activation records on the heap instead of on the stack. Instead of explicitly destroying add’s frame when add returns, we would wait until the garbage collector determines that it is safe to reclaim the frame, this would happen when all the pointers to “U” disappear.

Problem Description Compilers implement function calls in two steps:   First, a closure environment is installed to provide an access for free variables in the target program fragment. Second, the control is transferred to the target by a jump with arguments.

Contd… Closure conversion, which decides where and how to represent closures at runtime is a crucial step in the compilation of functional languages. Depending on the runtime behavior of each function, closures can be represented as data structures of virtually any shape, allocated in the heap, on the stack or in the registers. The decisions of where and how to represent closures at runtime greatly affect the quality of the code generated. Thus, a good closure-allocation scheme is required to optimize function calls.

Research Objectives Make best use of closure representation techniques. Test and use a closure-allocation scheme that integrates and improves previous closure analysis techniques and construct safely linked closures. Decide where and how to represent closures at runtime

Comparison of Stack and Heap Literature Review Comparison of Stack and Heap Costs involved in creating, accessing and destroying activation records whether they are on heap or on stack.

Comparison of Stack and Heap Component Heap Stack Copying and sharing 0.0 3.4 Frames pointers 2.0 0.0 Creation 3.1 1.0 Cache write misses 0.0 or 5.3 0.0 Disposal (pop) 1.4 1.0 Cache read misses 1.0 0.0 Total Cost 7.5 or 12.8 5.4 Call/cc O(1) O(N) ------------------------------------------------------------------------ Implementation easy hard

Comparison Results The write-miss policy of the machine’s primary cache. On machines with fetch-on-write or write-around write-miss policies, heap-allocated frames are significantly more expensive Stacks are harder to implement without space leaks as explained later If programming language supports call-with-current-continuation (call/cc), stacks have a much higher cost.

Implementation of Heap frames Implementation of Stacks To achieve a good performance with heap frames, it is necessary to have a sophisticated algorithm to choose closure representations. Implementation of Stacks A good closure analysis algorithm must be used to preserve space complexity while still trying to avoid too much copying If call/cc is to be supported, then stack copying or some more complicated technique must be implemented

Contd…. In a system with multiple threads, each thread must have its own stack. A large contiguous region of virtual memory must be reserved. Stack overflow detection must be implemented. In most cases this is handled automatically by the operating system using virtual- memory page faults.

Efficiency and Space Safety Research Design I would like to adopt a new closure allocation scheme that does not use any runtime stack. Instead, all closure environments are either allocated in the heap or in registers. Efficiency and Space Safety Flat Closures Linked Closures Safely Linked closure

An Example in ML fun f (v, w, x, y, z) = let fun g () = let val u = hd (v) fun h () = let fun i() = w+x+y+z+3 in (i,u) end in h

Contd… fun big n = if n<1 then nil else n :: big(n-1) fun loop (n, res) = if n<1 then res else let val s = f(big(N),0,0,0,0) () in loop(n-1,s::res) end val result = loop(N,nil)

Flat Closures

Linked Closures

Comparison of various Closures Flat Closures – The final result (I.e result) contains ‘N’ copies for h, thus it uses at most O(N) space. Linked Closures – Each closure for ‘h’ contains a pointer to the closure for ‘g’, which contains a list ‘v’ of size ‘N’. Since the final result keeps ‘N’ closures for ‘h’ simultaneously, it uses O(N^2) space. This space leak is caused by inappropriate retaining of some dead objects (‘v’) that should be reclaimed by the garbage collection.

SSC Rule Garbage - collected languages should satisfy the safe for space complexity rule. Each local variable should be considered “dead” after its last use in the current function body. This rule is not important for C-like languages because we can manually deallocate intermediate data structures in the program source.

Drawbacks of Flat and Linked Closures Linked Closures – they violate the SSC rule because local variable bindings will stay on the stack until they exit their scope, so they may remain live even after their last use. Flat Closures – they satisfy the SSC rule, but require that variables be copied many times from one closure to another.

Safely Linked Closures

Safely Linked Closures These closures contain only variables actually needed in the function but avoid closure copying by grouping variables with the same life time into a sharable record. In linked closures accessing variables is quite expensive because at least two links need to be traversed. But the nesting level of safely linked closures never exceeds two. So they still enjoy very fast variable access time.

Closure Conversion Algorithm I have chosen an algorithm proposed by A.Shao and A.Appel for closure conversion with the following properties: Activation records are allocated in the heap, hence they can be shared with other heap-allocated closures. Once a closure is created, no later writes are made to it; this makes generational garbage collection and call/cc efficient. It makes programs smaller and faster. It decreases the rate of heap allocation by 32% and reduces the amount of live data preserved by garbage collection.

Conclusions Thus by using safely linked closures and allocating closures in the heap and using the algorithm proposed by A.Shao and A.Appel for closure conversion we can improve the design of the compilers for higher-order functional programming languages such as ML, Scheme and Small Talk.

Questions Questions