A Func'onal Space Model

Similar documents
A Functional Space Model. COS 326 David Walker Princeton University

Thinking Induc,vely. COS 326 David Walker Princeton University

Thinking Induc,vely. COS 326 David Walker Princeton University

Contents. 8-1 Copyright (c) N. Afshartous

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

CS 61C: Great Ideas in Computer Architecture Strings and Func.ons. Anything can be represented as a number, i.e., data or instruc\ons

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

CS 61C: Great Ideas in Computer Architecture Func%ons and Numbers

Modules and Representa/on Invariants

CMSC 330: Organization of Programming Languages

OCaml Datatypes. COS 326 David Walker Princeton University

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

CMSC 330: Organization of Programming Languages

Dynamic Memory Alloca/on: Advanced Concepts

CSE 130: Programming Languages. Polymorphism. Ranjit Jhala UC San Diego

CSE P 501 Compilers. Memory Management and Garbage Collec<on Hal Perkins Winter UW CSE P 501 Winter 2016 W-1

Verification of an ML compiler. Lecture 3: Closures, closure conversion and call optimisations

Variables and Bindings

Principles of Programming Languages

Programming Languages and Techniques (CIS120)

Run-time Environments

Scope, Functions, and Storage Management

From last time. News. Example. Example. Typechecking let. Typechecking let. Pick up your first two quizzes after class. Assignment #2 due tomorrow

Run-time Environment

Run-time Environments

Programming Languages and Techniques (CIS120)

CA Compiler Construction

NAMES, SCOPES AND BINDING A REVIEW OF THE CONCEPTS

CMSC 330: Organization of Programming Languages

G Programming Languages - Fall 2012

Lecture 13: Garbage Collection

Programming Language Implementation

Memory Management. CS449 Fall 2017

Lecture 15 Garbage Collection

Common Misunderstandings from Exam 1 Material

Con$nuing CPS. COS 326 David Walker Princeton University

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Dynamic Memory Alloca/on: Basic Concepts

Dynamic Memory Alloca/on: Basic Concepts

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

CA341 - Comparative Programming Languages

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

One-Slide Summary. Lecture Outine. Automatic Memory Management #1. Why Automatic Memory Management? Garbage Collection.

Parametric types. map: ( a (a b) a list b list. filter: ( a bool) a list a list. Why? a and b are type variables!

Final CSE 131B Spring 2004

CS153: Compilers Lecture 15: Local Optimization

CS558 Programming Languages

A Func'onal Introduc'on. COS 326 David Walker Princeton University

Mutation. COS 326 Andrew W. Appel Princeton University. slides copyright David Walker and Andrew W. Appel

Automatic Memory Management

Administration CS 412/413. Advanced Language Support. First-class vs. Second-class. First-class functions. Function Types

Recap: Functions as first-class values

2. dead code elimination (declaration and initialization of z) 3. common subexpression elimination (temp1 = j + g + h)

Lecture 7: Binding Time and Storage

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

CS558 Programming Languages

CMSC 330: Organization of Programming Languages. Closures (Implementing Higher Order Functions)

Performance of Non-Moving Garbage Collectors. Hans-J. Boehm HP Labs

CS61C : Machine Structures

CPSC 213. Introduction to Computer Systems. Instance Variables and Dynamic Allocation. Unit 1c

ENEE 457: Computer Systems Security. Lecture 16 Buffer Overflow Attacks

CS558 Programming Languages. Winter 2013 Lecture 3

Dynamic Data Structures. CSCI 112: Programming in C

G Programming Languages - Fall 2012

COS 326 David Walker Princeton University

CSC 1600 Memory Layout for Unix Processes"

Qualifying Exam in Programming Languages and Compilers

Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

CPSC 213. Introduction to Computer Systems. Winter Session 2017, Term 2. Unit 1c Jan 24, 26, 29, 31, and Feb 2

Mutation. COS 326 David Walker Princeton University

2/12/2018. Recall Why ISAs Define Calling Conventions. ECE 220: Computer Systems & Programming. Recall the Structure of the LC-3 Stack Frame

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Motivation was to facilitate development of systems software, especially OS development.

Goal of lecture. Object-oriented Programming. Context of discussion. Message of lecture

Programming Languages and Techniques (CIS120)

Compiler Construction Lent Term 2013 Lectures 9 & 10 (of 16)

Language of the Machine Recursive functions

Muta%on. COS 326 David Walker Princeton University

CMSC 330: Organization of Programming Languages. OCaml Data Types

Implementation Garbage Collection

Control in Sequential Languages

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Announcements. Scope, Function Calls and Storage Management. Block Structured Languages. Topics. Examples. Simplified Machine Model.

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

Runtime. The optimized program is ready to run What sorts of facilities are available at runtime

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CSE 303: Concepts and Tools for Software Development

CSE 504: Compilers. Expressions. R. Sekar

Binghamton University Dynamic Memory Alloca/on

Types II. Hwansoo Han

CS61C : Machine Structures

Programming Languages and Techniques (CIS120)

CS 101: Computer Programming and Utilization

Programs in memory. The layout of memory is roughly:

Topic 7: Activation Records

Transcription:

A Func'onal Space Model COS 26 David Walker Princeton University slides copyright 2017 David Walker permission granted to reuse these slides for non-commercial educa'onal purposes

2 Because Halloween draws nigh: Serial killer or programming languages researcher? hmp://www.malevole.com/mv/misc/killerquiz/

Space Understanding the space complexity of func'onal programs At least two interes'ng components: the amount of live space at any instant in 'me the rate of alloca.on a func'on call may not change the amount of live space by much but may allocate at a substan'al rate because func'onal programs act by genera'ng new data structures and discarding old ones, they ouen allocate a lot» OCaml garbage collector is op'mized with this in mind» interes'ng fact: at the assembly level, the number of writes by a func'onal program is roughly the same as the number of writes by an impera've program

Space 4 Understanding the space complexity of func'onal programs At least two interes'ng components: the amount of live space at any instant in 'me the rate of alloca.on a func'on call may not change the amount of live space by much but may allocate at a substan'al rate because func'onal programs act by genera'ng new data structures and discarding old ones, they ouen allocate a lot» OCaml garbage collector is op'mized with this in mind» interes'ng fact: at the assembly level, the number of writes by a func'onal program is roughly the same as the number of writes by an impera've program What takes up space? conven'onal first-order data: tuples, lists, strings, datatypes func'on representa'ons (closures) the call stack

CONVENTIONAL DATA 5

Type: OCaml Representa'ons for Data Structures type triple = int * char * int Representa'on: (, 'a', 17) 'a' 17

Type: OCaml Representa'ons for Data Structures type mylist = int list Representa'on: [ ] [; 4; 5] 0 4 5 0

Type: Space Model 8 type tree = Leaf Node of int * tree * tree Representa'on: Leaf Node(, leu, right) 0 Node leu right

Alloca'ng space In C, you allocate when you call malloc In Java, you allocate when you call new What about ML?

Alloca'ng space Whenever you use a constructor, space is allocated: 10 let rec insert (t:tree) (i:int) = match t with Leaf -> Node (i, Leaf, Leaf) Node (j, left, right) -> if i <= j then Node (j, insert left i, right) else Node (j, left, insert right i)

Alloca'ng space Whenever you use a constructor, space is allocated: 11 let rec insert (t:tree) (i:int) = match t with Leaf -> Node (i, Leaf, Leaf) Node (j, left, right) -> if i <= j then Node (j, insert left i, right) else Node (j, left, insert right i) Consider: insert t 21 t

Alloca'ng space Whenever you use a constructor, space is allocated: 12 let rec insert (t:tree) (i:int) = match t with Leaf -> Node (i, Leaf, Leaf) Node (j, left, right) -> if i <= j then Node (j, insert left i, right) else Node (j, left, insert right i) Consider: insert t 21 t

Alloca'ng space Whenever you use a constructor, space is allocated: 1 let rec insert (t:tree) (i:int) = match t with Leaf -> Node (i, Leaf, Leaf) Node (j, left, right) -> if i <= j then Node (j, insert left i, right) else Node (j, left, insert right i) Consider: insert t 21 t 21

Alloca'ng space Whenever you use a constructor, space is allocated: 14 let rec insert (t:tree) (i:int) = match t with Leaf -> Node (i, Leaf, Leaf) Node (j, left, right) -> if i <= j then Node (j, insert left i, right) else Node (j, left, insert right i) Consider: insert t 21 t 21

Alloca'ng space Whenever you use a constructor, space is allocated: let rec insert (t:tree) (i:int) = match t with Leaf -> Node (i, Leaf, Leaf) Node (j, left, right) -> if i <= j then Node (j, insert left i, right) else Node (j, left, insert right i) Consider: insert t 21 t 21

Alloca'ng space Whenever you use a constructor, space is allocated: 16 let rec insert (t:tree) (i:int) = match t with Leaf -> Node (i, Leaf, Leaf) Node (j, left, right) -> if i <= j then Node (j, insert left i, right) else Node (j, left, insert right i) Consider: insert t 21 t 21

Alloca'ng space Whenever you use a constructor, space is allocated: let rec insert (t:tree) (i:int) = match t with Leaf -> Node (i, Leaf, Leaf) Node (j, left, right) -> if i <= j then Node (j, insert left i, right) else Node (j, left, insert right i) Total space allocated is propor'onal to the height of the tree. ~ log n, if tree with n nodes is balanced 17 t 21

Net space allocated The garbage collector reclaims unreachable data structures on the heap. 18 let fiddle (t: tree) = insert t 21 t John McCarthy invented g.c. 160 21

Net space allocated The garbage collector reclaims unreachable data structures on the heap. 1 let fiddle (t: tree) = insert t 21 If t is dead (unreachable), t 21

Net space allocated The garbage collector reclaims unreachable data structures on the heap. let fiddle (t: tree) = insert t 21 If t is dead (unreachable), 20 t Then all these nodes will be reclaimed! 21

Net space allocated 21 The garbage collector reclaims unreachable data structures on the heap. let fiddle (t: tree) = insert t 21 Net new space allocated: 1 node (just like impera've version of binary search trees) t 21

Net space allocated But what if you want to keep the old tree? 22 let faddle (t: tree) = (t, insert t 21) faddle(t) t 21

Net space allocated But what if you want to keep the old tree? let faddle (t: tree) = (t, insert t 21) faddle(t) Net new space allocated: log(n) nodes but note: impera've version would have to copy the old tree, space cost N new nodes! 2 t 21

Compare 24 let check_option (o:int option) : int option = match o with Some _ -> o None -> failwith found none let check_option (o:int option) : int option = match o with Some j -> Some j None -> failwith found none

Compare 25 let check_option (o:int option) : int option = match o with Some _ -> o None -> failwith found none allocates nothing when arg is Some i let check_option (o:int option) : int option = match o with Some j -> Some j None -> failwith found none allocates an op'on when arg is Some i

Compare 26 let cadd (c1:int*int) (c2:int*int) : int*int = let (x1,y1) = c1 in let (x2,y2) = c2 in (x1+x2, y1+y2) let double (c1:int*int) : int*int = let c2 = c1 in cadd c1 c2 let double (c1:int*int) : int*int = cadd c1 c1 let double (c1:int*int) : int*int = let (x1,y1) = c1 in cadd (x1,y1) (x1,y1)

Compare 27 let cadd (c1:int*int) (c2:int*int) : int*int = let (x1,y1) = c1 in let (x2,y2) = c2 in (x1+x2, y1+y2) let double (c1:int*int) : int*int = let c2 = c1 in cadd c1 c2 c1 1 2 c2 let double (c1:int*int) : int*int = cadd c1 c1 let double (c1:int*int) : int*int = let (x1,y1) = c1 in cadd (x1,y1) (x1,y1)

Compare 28 let cadd (c1:int*int) (c2:int*int) : int*int = let (x1,y1) = c1 in let (x2,y2) = c2 in (x1+x2, y1+y2) let double (c1:int*int) : int*int = let c2 = c1 in cadd c1 c2 let double (c1:int*int) : int*int = cadd c1 c1 c1 1 2 let double (c1:int*int) : int*int = let (x1,y1) = c1 in cadd (x1,y1) (x1,y1)

Compare 2 let cadd (c1:int*int) (c2:int*int) : int*int = let (x1,y1) = c1 in let (x2,y2) = c2 in (x1+x2, y1+y2) let double (c1:int*int) : int*int = let c2 = c1 in cadd c1 c2 let double (c1:int*int) : int*int = cadd c1 c1 let double (c1:int*int) : int*int = let (x1,y1) = c1 in cadd (x1,y1) (x1,y1) c1 arg1 arg2 1 2 1 2 1 2

Compare 0 let cadd (c1:int*int) (c2:int*int) : int*int = let (x1,y1) = c1 in let (x2,y2) = c2 in (x1+x2, y1+y2) let double (c1:int*int) : int*int = let c2 = c1 in cadd c1 c2 no alloca'on let double (c1:int*int) : int*int = cadd c1 c1 no alloca'on let double (c1:int*int) : int*int = let (x1,y1) = c1 in cadd (x1,y1) (x1,y1) allocates 2 pairs (unless the compiler happens to op'mize )

Compare 1 let cadd (c1:int*int) (c2:int*int) : int*int = let (x1,y1) = c1 in let (x2,y2) = c2 in (x1+x2, y1+y2) let double (c1:int*int) : int*int = let (x1,y1) = c1 in cadd c1 c1 double does not allocate extracts components: it is a read

FUNCTION CLOSURES 2

Closures (A reminder) Nested func'ons like bar ouen contain free variables: let foo y = let bar x = x + y in bar Here's bar on its own: let bar x = x + y y is free in the defini'on of bar To implement bar, the compiler creates a closure, which is a pair of code for the func'on plus an environment holding the free variables.

But what about nested, higher-order func'ons? bar again: 4 let bar x = x + y bar's representa'on: code let f2 (n, env) = n + env.y {y = 1} environment closure

But what about nested, higher-order func'ons? 5 To es'mate the (heap) space used by a program, we ouen need to es'mate the (heap) space used by its closures. code let f2 (n, env) = n + env.y {y = 1} environment Our es'mate will include the cost of the pair: two pointers = two 4-byte values = 8 bytes total + the cost of the environment (4 bytes in this case).

Space Model Summary 6 Understanding space consump'on in FP involves: understanding the difference between live space rate of alloca'on understanding where alloca'on occurs any 'me a constructor is used whenever closures are created understanding the costs of data types (fairly similar to Java) costs of closures (pair + environment)