1 / 133

Type Systems for Region-based Memory Management

Type Systems for Region-based Memory Management. Matthew Fluet Greg Morrisett & Amal Ahmed Harvard University. Memory Management. Dynamic allocation pervasive in computation. Memory Management. Dynamic allocation pervasive in computation Range of methods for managing memory.

Download Presentation

Type Systems for Region-based Memory Management

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Type Systems for Region-based Memory Management Matthew Fluet Greg Morrisett & Amal Ahmed Harvard University

  2. Memory Management • Dynamic allocation pervasive in computation

  3. Memory Management • Dynamic allocation pervasive in computation • Range of methods for managing memory

  4. Memory Management • Dynamic allocation pervasive in computation • Range of methods for managing memory • malloc/free • efficient, but tedious and error prone

  5. Memory Management • Dynamic allocation pervasive in computation • Range of methods for managing memory • malloc/free • efficient, but tedious and error prone • garbage collection • transparent and safe, but (can be) inefficient

  6. Memory Management • Dynamic allocation pervasive in computation • Range of methods for managing memory • malloc/free • efficient, but tedious and error prone • regions • garbage collection • transparent and safe, but (can be) inefficient

  7. Region-based Memory Management • Operationally • Memory is divided regions (denoted by r, r, …) • Objects are individually allocated in a region • All objects in a region are deallocated together

  8. Region-based Memory Management • Runtime Organization • Regions are linked lists of pages • Arbitrary intra- and inter-region references • Similar to arena-style allocators r1 Region handles r2 r3

  9. Application: Cyclone • Cyclone Safe-C Project • type-safety • with the “virtues” of C • low-level interface with manifest cost model

  10. Application: Cyclone • Cyclone Safe-C Project • type-safety • with the “virtues” of C • low-level interface with manifest cost model • range of memory management options • regions are an organizing principle

  11. Application: Cyclone • MediaNET • TCP benchmark (packet forwarding) • Cyclone v.0.1 • High water mark: 840 KB • 130 collections • Basic throughput: 50 MB/s • Cyclone v.0.5 • High water mark: 8 KB • 0 collections • Basic throughput: 74MB/s

  12. Cyclone: Regions

  13. Cyclone: Regions Meta-theory of Cyclone is a nightmare!!

  14. Cyclone: Regions Ultimate Goal: simple model where we can easily encode the key features of Cyclone in a target language with simpler meta-theory

  15. Cyclone: Regions Today’s Goal: Three type systems for region-based languages,culminating with a fairly good approximation of Cyclone’s features

  16. Outline • Introduction • Type-and-Effect System (Tofte-Talpin) • Monadic Type System (FRGN) • Translation Sketch • Substructural Type System (lrgnURAL) • Translation Sketch • Conclusion

  17. Type Systems for Regions • Memory is divided into regions • type of handle for region r hnd r

  18. Type Systems for Regions • Memory is divided into regions • type of handle for region r hnd r • Objects are individually allocated in a region • operations: new, read, write • type of object of type t allocated in region r ref r t

  19. Tofte-Talpin Region Calculus [’94] • Regions are created and destroyedwith a lexically scoped construct: letregionr,h in e • All objects in region r are deallocated together at the end of r’s scope

  20. Tofte-Talpin Region Calculus [’94] • Regions are created and destroyedwith a lexically scoped construct: letregionr,h in e • All objects in region r are deallocated together at the end of r’s scope • Regions have LIFO lifetimes • Live regions can be organized as a stack

  21. Tofte-Talpin Region Calculus [’94] • Regions are created and destroyedwith a lexically scoped construct

  22. Tofte-Talpin Region Calculus [’94] • Regions are created and destroyedwith a lexically scoped construct letregionr1,h1in let a = new h1 1 in let c = letregionr2,h2in let b = new h2 7 in new h1 (read a + read b) in … c … r1

  23. Tofte-Talpin Region Calculus [’94] • Regions are created and destroyedwith a lexically scoped construct letregionr1,h1in let a = new h1 1in let c = letregionr2,h2in let b = new h2 7 in new h1 (read a + read b) in … c … r1 a : 1 input allocated in first region

  24. Tofte-Talpin Region Calculus [’94] • Regions are created and destroyedwith a lexically scoped construct letregionr1,h1in let a = new h1 1 in let c = letregionr2,h2in let b = new h2 7 in new h1 (read a + read b) in … c … r2 r1 a : 1 input allocated in first region

  25. Tofte-Talpin Region Calculus [’94] • Regions are created and destroyedwith a lexically scoped construct letregionr1,h1in let a = new h1 1 in let c = letregionr2,h2in let b = new h2 7in new h1 (read a + read b) in … c … r2 b : 7 temporary allocated in second region r1 a : 1 inputallocated in first region

  26. Tofte-Talpin Region Calculus [’94] • Regions are created and destroyedwith a lexically scoped construct letregionr1,h1in let a = new h1 1 in let c = letregionr2,h2in let b = new h2 7 in new h1 (read a + read b)in … c … r2 b : 7 temporary allocated in second region r1 a : 1 c : 8 input and outputallocated in first region

  27. Tofte-Talpin Region Calculus [’94] • Regions are created and destroyedwith a lexically scoped construct letregionr1,h1in let a = new h1 1 in let c = letregionr2,h2in let b = new h2 7 in new h1 (read a + read b) in … c … temporary allocated in second region r1 a : 1 c : 8 input and outputallocated in first region

  28. Type-and-Effect System • Track the set f of regions accessed by a computation: G` e : t, f • Function types include a latent effect: t1!t2 • The role of f is to tell us when it is not safe to deallocate a region f

  29. Type-and-Effect System • Typing rule for letregion is subtle: G,h:hndr` e : t, fr∉ frv(G,t) G`letregionr,h in e : t, f \ {r}

  30. Type-and-Effect System • Typing rule for letregion is subtle: G,h:hndr` e : t, fr∉ frv(G,t) G`letregionr,h in e : t, f \ {r} • Typing rule for effect weakening: G` e : t, ffµf’ G` e : t, f’

  31. Type-and-Effect System • Effects are pervasive in typing rules: G` e1 : int, f1G` e2 : int, f2 G` e1 + e2 : int, f1[f2 G` eh : hndr, fhG` e : t, f G`new eh e : refrt, fh[f[ {r}

  32. Type-and-Effect System • Type-and-effects system ensures safety

  33. Type-and-Effect System • Type-and-effects system ensures safety • But adds complications: • Typing rule for letregion is subtle(due to the interplay of dangling pointers and effects) • Effect weakening and region subtyping • Effects correspond to sets of regions (term equality no longer suffices for type checking)

  34. Monadic Type Systems • Monadic encapsulation of effects [L-PJ 94] • Embed imperative features in pure languages

  35. Monadic Type Systems • Monadic encapsulation of effects [L-PJ 94] • Embed imperative features in pure languages • Types ST s aSTRef s a • Operations returnST :: 8s,a. a!ST s a thenST :: 8s,a,b.ST s a!(a!ST s b)!ST s b newSTRef :: 8s,a. a!ST s (STRef s a) readSTRef :: 8s,a. STRef s a!ST s a writeSTRef :: 8s,a. STRef s a!a!ST s 1

  36. Monadic Type Systems • Monadic encapsulation of effects [L-PJ 94] • Embed imperative features in pure languages runST :: 8a. (8s. ST s a) !a • Polymorphism over store index type ensures that the computation (and the result) are independent of the initial (and final) store

  37. Monadic Type Systems • Monadic encapsulation of effects [L-PJ 94] • Embed imperative features in pure languages • Polymorphic type system ensures safety • Well understood meta-theory • Simplicity of System F type system

  38. FRGN = System F + RGN monad • System F • Monadic sub-language

  39. RGN monad: Types • Monadic types

  40. RGN monad: Types • Monadic types RGNst – computations in stack of regions s returning values of type t; a “stack” transformer

  41. RGN monad: Types • Monadic types Hnds – handles for the region at the top of the stack of regions s

  42. RGN monad: Types • Monadic types Refst – values of type t allocated in region at the top of the stack of regions s

  43. RGN monad: Operations • Monadic unit and bind returnRGN :: 8s,a. a!RGNsa thenRGN :: 8s,a,b. RGNsa! (a!RGNsb) !RGNsb

  44. RGN monad: Operations • Monadic unit and bind returnRGN :: 8s,a. a!RGNsa thenRGN :: 8s,a,b. RGNsa! (a!RGNsb) !RGNsb

  45. RGN monad: Operations • Monadic unit and bind returnRGN :: 8s,a. a!RGNsa thenRGN :: 8s,a,b. RGNsa! (a!RGNsb) !RGNsb

  46. RGN monad: Operations • Create and read region allocated values new :: 8s,a. Hnds!a!RGNs (Refsa) read :: 8s,a. Refsa!RGNsa

  47. RGN monad: Operations • Create and read region allocated values new :: 8s,a. Hnds!a!RGNs (Refsa) read :: 8s,a. Refsa!RGNsa

  48. RGN monad: Encapsulation • Encapsulate and run a monadic computation runRGN :: 8a. (8s. RGNsa) !a

  49. RGN monad: Encapsulation • Encapsulate and run a monadic computation runRGN :: 8a. (8s. RGNsa) !a

  50. RGN monad: Encapsulation • Encapsulate and run a monadic computation runRGN :: 8a. (8s. RGNsa) !a “for all stacks” )no assumptions about stack of regions

More Related