Runtime systems Programmation Fonctionnelle Avance - - PowerPoint PPT Presentation

runtime systems
SMART_READER_LITE
LIVE PREVIEW

Runtime systems Programmation Fonctionnelle Avance - - PowerPoint PPT Presentation

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management Runtime systems Programmation Fonctionnelle Avance http://www-lipn.univ-paris13.fr/~saiu/teaching/PFA-2010 Luca Saiu


slide-1
SLIDE 1

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Runtime systems

Programmation Fonctionnelle Avancée

http://www-lipn.univ-paris13.fr/~saiu/teaching/PFA-2010

Luca Saiu saiu@lipn.univ-paris13.fr

Master Informatique 2ème année, spécialité Programmation et Logiciels Sûrs Laboratoire d’Informatique de l’Université Paris Nord — Institut Galilée

2010-12-08

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-2
SLIDE 2

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Runtime systems

Functional program are very high-level: it’s not obvious how to implement them. Complex library support at run time

How do we represent objects in memory?

Think about memory words, bits and pointers

How do we release memory? The runtime must be written in a low-level language (C, assembly)

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-3
SLIDE 3

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Runtime systems

Functional program are very high-level: it’s not obvious how to implement them. Complex library support at run time

How do we represent objects in memory?

Think about memory words, bits and pointers

How do we release memory? The runtime must be written in a low-level language (C, assembly)

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-4
SLIDE 4

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Runtime systems

Functional program are very high-level: it’s not obvious how to implement them. Complex library support at run time

How do we represent objects in memory?

Think about memory words, bits and pointers

How do we release memory? The runtime must be written in a low-level language (C, assembly)

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-5
SLIDE 5

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Runtime systems

Functional program are very high-level: it’s not obvious how to implement them. Complex library support at run time

How do we represent objects in memory?

Think about memory words, bits and pointers

How do we release memory? The runtime must be written in a low-level language (C, assembly)

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-6
SLIDE 6

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Runtime systems

Functional program are very high-level: it’s not obvious how to implement them. Complex library support at run time

How do we represent objects in memory?

Think about memory words, bits and pointers

How do we release memory? The runtime must be written in a low-level language (C, assembly)

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-7
SLIDE 7

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Runtime systems

Functional program are very high-level: it’s not obvious how to implement them. Complex library support at run time

How do we represent objects in memory?

Think about memory words, bits and pointers

How do we release memory? The runtime must be written in a low-level language (C, assembly)

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-8
SLIDE 8

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Think of an efficient implementation

Return the given list with 42 prepended, without modifying anything: let f xs = 42 :: xs;; What if we call f with a ten-million-element list? We don’t need to copy anything! We’re just building a very small structure (one cons) which refers the given one

We should always pass and return pointers to data structures in memory Fast and simple!

but when do we destroy lists...?

Anyway we don’t want to allocate in memory small data which fit in registers: avoid allocation whenever possible

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-9
SLIDE 9

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Think of an efficient implementation

Return the given list with 42 prepended, without modifying anything: let f xs = 42 :: xs;; What if we call f with a ten-million-element list? We don’t need to copy anything! We’re just building a very small structure (one cons) which refers the given one

We should always pass and return pointers to data structures in memory Fast and simple!

but when do we destroy lists...?

Anyway we don’t want to allocate in memory small data which fit in registers: avoid allocation whenever possible

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-10
SLIDE 10

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Think of an efficient implementation

Return the given list with 42 prepended, without modifying anything: let f xs = 42 :: xs;; What if we call f with a ten-million-element list? We don’t need to copy anything! We’re just building a very small structure (one cons) which refers the given one

We should always pass and return pointers to data structures in memory Fast and simple!

but when do we destroy lists...?

Anyway we don’t want to allocate in memory small data which fit in registers: avoid allocation whenever possible

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-11
SLIDE 11

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Think of an efficient implementation

Return the given list with 42 prepended, without modifying anything: let f xs = 42 :: xs;; What if we call f with a ten-million-element list? We don’t need to copy anything! We’re just building a very small structure (one cons) which refers the given one

We should always pass and return pointers to data structures in memory Fast and simple!

but when do we destroy lists...?

Anyway we don’t want to allocate in memory small data which fit in registers: avoid allocation whenever possible

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-12
SLIDE 12

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Think of an efficient implementation

Return the given list with 42 prepended, without modifying anything: let f xs = 42 :: xs;; What if we call f with a ten-million-element list? We don’t need to copy anything! We’re just building a very small structure (one cons) which refers the given one

We should always pass and return pointers to data structures in memory Fast and simple!

but when do we destroy lists...?

Anyway we don’t want to allocate in memory small data which fit in registers: avoid allocation whenever possible

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-13
SLIDE 13

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Think of an efficient implementation

Return the given list with 42 prepended, without modifying anything: let f xs = 42 :: xs;; What if we call f with a ten-million-element list? We don’t need to copy anything! We’re just building a very small structure (one cons) which refers the given one

We should always pass and return pointers to data structures in memory Fast and simple!

but when do we destroy lists...?

Anyway we don’t want to allocate in memory small data which fit in registers: avoid allocation whenever possible

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-14
SLIDE 14

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Think of an efficient implementation

Return the given list with 42 prepended, without modifying anything: let f xs = 42 :: xs;; What if we call f with a ten-million-element list? We don’t need to copy anything! We’re just building a very small structure (one cons) which refers the given one

We should always pass and return pointers to data structures in memory Fast and simple!

but when do we destroy lists...?

Anyway we don’t want to allocate in memory small data which fit in registers: avoid allocation whenever possible

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-15
SLIDE 15

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Binary words

Figure: A 32-bit word

Modern machines have 32- or 64-bit words. There are assembly instructions working efficiently on word-sized data (arithmetics, load, store) At the hardware level, memory is untyped: a binary word can represent an integer, a boolean, a float, some characters, a pointer1, a sum type element with no parameters...

1Today we ignore internal pointers for simplicity Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-16
SLIDE 16

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Binary words

Figure: A 32-bit word

Modern machines have 32- or 64-bit words. There are assembly instructions working efficiently on word-sized data (arithmetics, load, store) At the hardware level, memory is untyped: a binary word can represent an integer, a boolean, a float, some characters, a pointer1, a sum type element with no parameters...

1Today we ignore internal pointers for simplicity Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-17
SLIDE 17

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Boxed vs. unboxed

boxed: allocate the object in memory, and pass around a pointer to it unboxed: pass around the object itself, which is small (word-sized)

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-18
SLIDE 18

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Stack allocation is not enough

int f(int x){ struct big_strict s; s.q = x; s.w = g(x, &s); ... return 42; } s is visible to g. s remains alive in memory until f returns, so also the functions called by g might access it. When f returns s is automatically destroyed: its memory will be reused LIFO policy implemented with a stack: push at function entry, pop at function exit

Very, very efficient. But not expressive enough.

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-19
SLIDE 19

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Stack allocation is not enough

int f(int x){ struct big_strict s; s.q = x; s.w = g(x, &s); ... return 42; } s is visible to g. s remains alive in memory until f returns, so also the functions called by g might access it. When f returns s is automatically destroyed: its memory will be reused LIFO policy implemented with a stack: push at function entry, pop at function exit

Very, very efficient. But not expressive enough.

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-20
SLIDE 20

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Stack allocation is not enough

int f(int x){ struct big_strict s; s.q = x; s.w = g(x, &s); ... return 42; } s is visible to g. s remains alive in memory until f returns, so also the functions called by g might access it. When f returns s is automatically destroyed: its memory will be reused LIFO policy implemented with a stack: push at function entry, pop at function exit

Very, very efficient. But not expressive enough.

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-21
SLIDE 21

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Stack allocation is not enough

int f(int x){ struct big_strict s; s.q = x; s.w = g(x, &s); ... return 42; } s is visible to g. s remains alive in memory until f returns, so also the functions called by g might access it. When f returns s is automatically destroyed: its memory will be reused LIFO policy implemented with a stack: push at function entry, pop at function exit

Very, very efficient. But not expressive enough.

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-22
SLIDE 22

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Stack allocation is not enough

int f(int x){ struct big_strict s; s.q = x; s.w = g(x, &s); ... return 42; } s is visible to g. s remains alive in memory until f returns, so also the functions called by g might access it. When f returns s is automatically destroyed: its memory will be reused LIFO policy implemented with a stack: push at function entry, pop at function exit

Very, very efficient. But not expressive enough.

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-23
SLIDE 23

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Stack allocation is not enough

int f(int x){ struct big_strict s; s.q = x; s.w = g(x, &s); ... return 42; } s is visible to g. s remains alive in memory until f returns, so also the functions called by g might access it. When f returns s is automatically destroyed: its memory will be reused LIFO policy implemented with a stack: push at function entry, pop at function exit

Very, very efficient. But not expressive enough.

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-24
SLIDE 24

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Heap allocation and de-allocation

Any reasonable programming language also lets you explicitly create new objects in memory without following a LIFO policy: . . /* C */ p = malloc(sizeof(int) * 2); p[0] = 42; ... free(p); (* OCaml *) x :: xs ... (the memory is freed automatically)

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-25
SLIDE 25

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

A heap with free list

A heap is the data structure on which malloc and free are implemented:

Figure: A 16-word heap with a free list: each unused word in the heap points to the next one. Red words belong to alive objects.

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-26
SLIDE 26

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

How to interpret a word-sized datum

Figure: Is this a number, a boolean, a pointer, or maybe an object of type t = A | B | C, or ...?

1001100010011010100010002 = 1000103210 Number, memory address, or what else? In general we can’t tell We could establish a non-standard convention in our runtime so that all objects are tagged with an encoding of their type

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-27
SLIDE 27

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

How to interpret a word-sized datum

Figure: Is this a number, a boolean, a pointer, or maybe an object of type t = A | B | C, or ...?

1001100010011010100010002 = 1000103210 Number, memory address, or what else? In general we can’t tell We could establish a non-standard convention in our runtime so that all objects are tagged with an encoding of their type

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-28
SLIDE 28

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

How to interpret a word-sized datum

Figure: Is this a number, a boolean, a pointer, or maybe an object of type t = A | B | C, or ...?

1001100010011010100010002 = 1000103210 Number, memory address, or what else? In general we can’t tell We could establish a non-standard convention in our runtime so that all objects are tagged with an encoding of their type

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-29
SLIDE 29

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

How to interpret a word-sized datum

Figure: Is this a number, a boolean, a pointer, or maybe an object of type t = A | B | C, or ...?

1001100010011010100010002 = 1000103210 Number, memory address, or what else? In general we can’t tell We could establish a non-standard convention in our runtime so that all objects are tagged with an encoding of their type

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-30
SLIDE 30

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

How to interpret a word-sized datum

Figure: Is this a number, a boolean, a pointer, or maybe an object of type t = A | B | C, or ...?

1001100010011010100010002 = 1000103210 Number, memory address, or what else? In general we can’t tell We could establish a non-standard convention in our runtime so that all objects are tagged with an encoding of their type

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-31
SLIDE 31

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Object headers - I

We can reserve a word for runtime type information at the beginning of each object:

Figure: The pair (3, false) represented with object headers. The words shown in red contain some binary encoding of the type.

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-32
SLIDE 32

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Object headers - II

Another example with object headers:

Figure: The list 2 :: 3 :: [], also written as [2; 3], with object headers

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-33
SLIDE 33

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Object headers - III

Pros Easy to understand and implement One word per object suffices to encode any type

The header can also be a pointer, if needed

Cons Inefficient: we have to unbox everything

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-34
SLIDE 34

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Object headers - III

Pros Easy to understand and implement One word per object suffices to encode any type

The header can also be a pointer, if needed

Cons Inefficient: we have to unbox everything

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-35
SLIDE 35

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Object headers - III

Pros Easy to understand and implement One word per object suffices to encode any type

The header can also be a pointer, if needed

Cons Inefficient: we have to unbox everything

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-36
SLIDE 36

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Object headers - III

Pros Easy to understand and implement One word per object suffices to encode any type

The header can also be a pointer, if needed

Cons Inefficient: we have to unbox everything

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-37
SLIDE 37

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Object headers - III

Pros Easy to understand and implement One word per object suffices to encode any type

The header can also be a pointer, if needed

Cons Inefficient: we have to unbox everything

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-38
SLIDE 38

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Tagging within a datum word - I

Instead of using a prefix word, we can reserve some bits in a fixed position within a datum to encode its type. Example (3-bit tag): 000: unique values (booleans, empty list, unit, ...) 001: integer 010: cons 011: character 100: float 101: ref 110: string 111: (not used)

Figure: A 32-bit word with a 3-bit tag. What’s this? The integer 710

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-39
SLIDE 39

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Tagging within a datum word - I

Instead of using a prefix word, we can reserve some bits in a fixed position within a datum to encode its type. Example (3-bit tag): 000: unique values (booleans, empty list, unit, ...) 001: integer 010: cons 011: character 100: float 101: ref 110: string 111: (not used)

Figure: A 32-bit word with a 3-bit tag. What’s this? The integer 710

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-40
SLIDE 40

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Tagging within a datum word - I

Instead of using a prefix word, we can reserve some bits in a fixed position within a datum to encode its type. Example (3-bit tag): 000: unique values (booleans, empty list, unit, ...) 001: integer 010: cons 011: character 100: float 101: ref 110: string 111: (not used)

Figure: A 32-bit word with a 3-bit tag. What’s this? The integer 710

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-41
SLIDE 41

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Tagging within a datum word - I

Instead of using a prefix word, we can reserve some bits in a fixed position within a datum to encode its type. Example (3-bit tag): 000: unique values (booleans, empty list, unit, ...) 001: integer 010: cons 011: character 100: float 101: ref 110: string 111: (not used)

Figure: A 32-bit word with a 3-bit tag. What’s this? The integer 710

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-42
SLIDE 42

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Tagging within a datum word - II

Figure: A 32-bit word: 29-bit payload plus 3-bit tag

Pros:

compact: no additional space is used

Cons:

  • perating on data is harder and possibly slower (think of

adding two tagged integers)

...but we can choose tags in a smart way (any ideas?)

less space available for the datum payload very few tags available: at most 2n with n tag bits

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-43
SLIDE 43

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Tagging within a datum word - II

Figure: A 32-bit word: 29-bit payload plus 3-bit tag

Pros:

compact: no additional space is used

Cons:

  • perating on data is harder and possibly slower (think of

adding two tagged integers)

...but we can choose tags in a smart way (any ideas?)

less space available for the datum payload very few tags available: at most 2n with n tag bits

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-44
SLIDE 44

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Tagging within a datum word - II

Figure: A 32-bit word: 29-bit payload plus 3-bit tag

Pros:

compact: no additional space is used

Cons:

  • perating on data is harder and possibly slower (think of

adding two tagged integers)

...but we can choose tags in a smart way (any ideas?)

less space available for the datum payload very few tags available: at most 2n with n tag bits

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-45
SLIDE 45

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Tagging within a datum word - II

Figure: A 32-bit word: 29-bit payload plus 3-bit tag

Pros:

compact: no additional space is used

Cons:

  • perating on data is harder and possibly slower (think of

adding two tagged integers)

...but we can choose tags in a smart way (any ideas?)

less space available for the datum payload very few tags available: at most 2n with n tag bits

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-46
SLIDE 46

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Tagging within a datum word - II

Figure: A 32-bit word: 29-bit payload plus 3-bit tag

Pros:

compact: no additional space is used

Cons:

  • perating on data is harder and possibly slower (think of

adding two tagged integers)

...but we can choose tags in a smart way (any ideas?)

less space available for the datum payload very few tags available: at most 2n with n tag bits

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-47
SLIDE 47

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Tagging within a datum word - II

Figure: A 32-bit word: 29-bit payload plus 3-bit tag

Pros:

compact: no additional space is used

Cons:

  • perating on data is harder and possibly slower (think of

adding two tagged integers)

...but we can choose tags in a smart way (any ideas?)

less space available for the datum payload very few tags available: at most 2n with n tag bits

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-48
SLIDE 48

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Tagging within a datum word - Example

Figure: A list with in-word tags

Notice that we have tagged a pointer. Why can we do it? If heap objects are aligned on word boundary, the rightmost two (for 32-bit architectures) or three (for 64-bit architectures) bits are always zero in native pointers. Aligning on a wider boundary gives us more bits to use for tagging, but may waste heap space

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-49
SLIDE 49

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Tagging within a datum word - Example

Figure: A list with in-word tags

Notice that we have tagged a pointer. Why can we do it? If heap objects are aligned on word boundary, the rightmost two (for 32-bit architectures) or three (for 64-bit architectures) bits are always zero in native pointers. Aligning on a wider boundary gives us more bits to use for tagging, but may waste heap space

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-50
SLIDE 50

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Tagging within a datum word - Example

Figure: A list with in-word tags

Notice that we have tagged a pointer. Why can we do it? If heap objects are aligned on word boundary, the rightmost two (for 32-bit architectures) or three (for 64-bit architectures) bits are always zero in native pointers. Aligning on a wider boundary gives us more bits to use for tagging, but may waste heap space

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-51
SLIDE 51

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Alignment: look at the heap again

Figure: Think of the binary representation of pointers to heap objects: here any pointer will end with 00 (because addresses in radix 10 are divisible by 4).

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-52
SLIDE 52

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Hybrid tagging

In-word tags are more efficient than headers, but we would need much more than two or three bits... We can find a compromise . Use a short in-word tag of two or three bits for the most common types which we want to keep unboxed or boxed without header, reserving one value for boxed objects with headers. . Example (with two-bit in-word tag): 00 int (unboxed) 01 pointer to cons (boxed, but no header) 10 unique (unboxed) 11 pointer to a boxed object with header Very efficient if integers and lists are used a lot.

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-53
SLIDE 53

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Hybrid tagging

In-word tags are more efficient than headers, but we would need much more than two or three bits... We can find a compromise . Use a short in-word tag of two or three bits for the most common types which we want to keep unboxed or boxed without header, reserving one value for boxed objects with headers. . Example (with two-bit in-word tag): 00 int (unboxed) 01 pointer to cons (boxed, but no header) 10 unique (unboxed) 11 pointer to a boxed object with header Very efficient if integers and lists are used a lot.

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-54
SLIDE 54

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Hybrid tagging

In-word tags are more efficient than headers, but we would need much more than two or three bits... We can find a compromise . Use a short in-word tag of two or three bits for the most common types which we want to keep unboxed or boxed without header, reserving one value for boxed objects with headers. . Example (with two-bit in-word tag): 00 int (unboxed) 01 pointer to cons (boxed, but no header) 10 unique (unboxed) 11 pointer to a boxed object with header Very efficient if integers and lists are used a lot.

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-55
SLIDE 55

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Hybrid tagging

In-word tags are more efficient than headers, but we would need much more than two or three bits... We can find a compromise . Use a short in-word tag of two or three bits for the most common types which we want to keep unboxed or boxed without header, reserving one value for boxed objects with headers. . Example (with two-bit in-word tag): 00 int (unboxed) 01 pointer to cons (boxed, but no header) 10 unique (unboxed) 11 pointer to a boxed object with header Very efficient if integers and lists are used a lot.

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-56
SLIDE 56

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Hybrid tagging – example

Figure: The pair (2, [true; false]), following the convention of the previous slide. The pair has a header because we didn’t consider it “common” enough: but integers, conses and unique values (booleans and the empty list) need no header.

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-57
SLIDE 57

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Static typing and tagging

We ignored static typing until now. Does OCaml need runtime tags? . . Possible bonus if you answer this in a smart way

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-58
SLIDE 58

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Automatic memory management

We want to work under the illusion that memory is infinite. The program just allocates objects, ignoring the problem Unneeded objects are automatically destroyed by the runtime, which “wakes up” when needed Pros: No dangling pointers No double free No (trivial) memory leaks Cons: Inefficient.

  • Mmm. Is it really inefficient?

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-59
SLIDE 59

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Automatic memory management

We want to work under the illusion that memory is infinite. The program just allocates objects, ignoring the problem Unneeded objects are automatically destroyed by the runtime, which “wakes up” when needed Pros: No dangling pointers No double free No (trivial) memory leaks Cons: Inefficient.

  • Mmm. Is it really inefficient?

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-60
SLIDE 60

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Automatic memory management

We want to work under the illusion that memory is infinite. The program just allocates objects, ignoring the problem Unneeded objects are automatically destroyed by the runtime, which “wakes up” when needed Pros: No dangling pointers No double free No (trivial) memory leaks Cons: Inefficient.

  • Mmm. Is it really inefficient?

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-61
SLIDE 61

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Automatic memory management

We want to work under the illusion that memory is infinite. The program just allocates objects, ignoring the problem Unneeded objects are automatically destroyed by the runtime, which “wakes up” when needed Pros: No dangling pointers No double free No (trivial) memory leaks Cons: Inefficient.

  • Mmm. Is it really inefficient?

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-62
SLIDE 62

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Automatic memory management

We want to work under the illusion that memory is infinite. The program just allocates objects, ignoring the problem Unneeded objects are automatically destroyed by the runtime, which “wakes up” when needed Pros: No dangling pointers No double free No (trivial) memory leaks Cons: Inefficient.

  • Mmm. Is it really inefficient?

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-63
SLIDE 63

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Automatic memory management

We want to work under the illusion that memory is infinite. The program just allocates objects, ignoring the problem Unneeded objects are automatically destroyed by the runtime, which “wakes up” when needed Pros: No dangling pointers No double free No (trivial) memory leaks Cons: Inefficient.

  • Mmm. Is it really inefficient?

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-64
SLIDE 64

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Automatic memory management

We want to work under the illusion that memory is infinite. The program just allocates objects, ignoring the problem Unneeded objects are automatically destroyed by the runtime, which “wakes up” when needed Pros: No dangling pointers No double free No (trivial) memory leaks Cons: Inefficient.

  • Mmm. Is it really inefficient?

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-65
SLIDE 65

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Automatic memory management — Definitions

At a given time, we call heap objects which will never be used again by the program semantic garbage. The runtime system works with roots (processor registers and stack, global variables): heap objects can only be reached via pointers from roots, or... ...from other heap objects. For example, many conses refer

  • ther conses

A piece of syntactic garbage is a heap object which can’t be reached by recursively following pointers starting from roots

Automatic memory management recycles syntactic garbage Because of deep theoretical reasons it’s impossible to find all semantic garbage; but recycling syntactic garbage is a conservative approximation

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-66
SLIDE 66

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Automatic memory management — Main approaches

Two main approaches: Tracing garbage collection (or just garbage collection):

when the memory is full visit the graph of alive objects, starting from roots; what we didn’t visit is garbage: destroy it

Reference counting

count the pointers to each object when an object has zero pointers destroy it

Only two main approaches. But there are many, many, many variants

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-67
SLIDE 67

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Automatic memory management — Main approaches

Two main approaches: Tracing garbage collection (or just garbage collection):

when the memory is full visit the graph of alive objects, starting from roots; what we didn’t visit is garbage: destroy it

Reference counting

count the pointers to each object when an object has zero pointers destroy it

Only two main approaches. But there are many, many, many variants

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-68
SLIDE 68

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Automatic memory management — Main approaches

Two main approaches: Tracing garbage collection (or just garbage collection):

when the memory is full visit the graph of alive objects, starting from roots; what we didn’t visit is garbage: destroy it

Reference counting

count the pointers to each object when an object has zero pointers destroy it

Only two main approaches. But there are many, many, many variants

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-69
SLIDE 69

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Automatic memory management — Main approaches

Two main approaches: Tracing garbage collection (or just garbage collection):

when the memory is full visit the graph of alive objects, starting from roots; what we didn’t visit is garbage: destroy it

Reference counting

count the pointers to each object when an object has zero pointers destroy it

Only two main approaches. But there are many, many, many variants

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-70
SLIDE 70

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Automatic memory management — Main approaches

Two main approaches: Tracing garbage collection (or just garbage collection):

when the memory is full visit the graph of alive objects, starting from roots; what we didn’t visit is garbage: destroy it

Reference counting

count the pointers to each object when an object has zero pointers destroy it

Only two main approaches. But there are many, many, many variants

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-71
SLIDE 71

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Automatic memory management — Main approaches

Two main approaches: Tracing garbage collection (or just garbage collection):

when the memory is full visit the graph of alive objects, starting from roots; what we didn’t visit is garbage: destroy it

Reference counting

count the pointers to each object when an object has zero pointers destroy it

Only two main approaches. But there are many, many, many variants

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-72
SLIDE 72

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

History

John McCarthy proposed mark-sweep garbage collection in his famous 1959 (!) paper introducing Lisp

Figure: John McCarthy in 2006. Photo by null0, released under the “Creative

Commons Attribution 2.0 Generic” license: http://www.flickr.com/photos/null0/272015955/

George E. Collins responded in 1960 by proposing reference counting as a “more efficient” alternative Popularly considered inefficient. Many languages have always been depending on it, but accepted into the mainstream only in the 1990s

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-73
SLIDE 73

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

History

John McCarthy proposed mark-sweep garbage collection in his famous 1959 (!) paper introducing Lisp

Figure: John McCarthy in 2006. Photo by null0, released under the “Creative

Commons Attribution 2.0 Generic” license: http://www.flickr.com/photos/null0/272015955/

George E. Collins responded in 1960 by proposing reference counting as a “more efficient” alternative Popularly considered inefficient. Many languages have always been depending on it, but accepted into the mainstream only in the 1990s

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-74
SLIDE 74

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

History

John McCarthy proposed mark-sweep garbage collection in his famous 1959 (!) paper introducing Lisp

Figure: John McCarthy in 2006. Photo by null0, released under the “Creative

Commons Attribution 2.0 Generic” license: http://www.flickr.com/photos/null0/272015955/

George E. Collins responded in 1960 by proposing reference counting as a “more efficient” alternative Popularly considered inefficient. Many languages have always been depending on it, but accepted into the mainstream only in the 1990s

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-75
SLIDE 75

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Reference-counting - I

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-76
SLIDE 76

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Reference-counting - II

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-77
SLIDE 77

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Reference-counting - III

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-78
SLIDE 78

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Reference-counting problems - I

Very inefficient (one word overhead per object, keeping counters up-to-date costs more than payload operations)... ...but this is not the main problem

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-79
SLIDE 79

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Reference-counting problems - II

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-80
SLIDE 80

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Reference-counting problems - III

Figure: Circular garbage is never destroyed!

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-81
SLIDE 81

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Reference-counting problems - IV

Figure: This is definitely syntactic garbage; but cyclic objects can’t be destroyed by the reference counter.

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems

slide-82
SLIDE 82

Introduction and hardware architecture reminders Object representation and runtime typing Automatic memory management

Tracing garbage collection

Luca Saiu — saiu@lipn.univ-paris13.fr Runtime systems