1.33k likes | 1.55k Views
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.
E N D
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
Memory Management • Dynamic allocation pervasive in computation • Range of methods for managing memory • malloc/free • efficient, but tedious and error prone
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
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
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
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
Application: Cyclone • Cyclone Safe-C Project • type-safety • with the “virtues” of C • low-level interface with manifest cost model
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
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
Cyclone: Regions Meta-theory of Cyclone is a nightmare!!
Cyclone: Regions Ultimate Goal: simple model where we can easily encode the key features of Cyclone in a target language with simpler meta-theory
Cyclone: Regions Today’s Goal: Three type systems for region-based languages,culminating with a fairly good approximation of Cyclone’s features
Outline • Introduction • Type-and-Effect System (Tofte-Talpin) • Monadic Type System (FRGN) • Translation Sketch • Substructural Type System (lrgnURAL) • Translation Sketch • Conclusion
Type Systems for Regions • Memory is divided into regions • type of handle for region r hnd r
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
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
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
Tofte-Talpin Region Calculus [’94] • Regions are created and destroyedwith a lexically scoped construct
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
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
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
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
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
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
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
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}
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’
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}
Type-and-Effect System • Type-and-effects system ensures safety
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)
Monadic Type Systems • Monadic encapsulation of effects [L-PJ 94] • Embed imperative features in pure languages
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
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
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
FRGN = System F + RGN monad • System F • Monadic sub-language
RGN monad: Types • Monadic types
RGN monad: Types • Monadic types RGNst – computations in stack of regions s returning values of type t; a “stack” transformer
RGN monad: Types • Monadic types Hnds – handles for the region at the top of the stack of regions s
RGN monad: Types • Monadic types Refst – values of type t allocated in region at the top of the stack of regions s
RGN monad: Operations • Monadic unit and bind returnRGN :: 8s,a. a!RGNsa thenRGN :: 8s,a,b. RGNsa! (a!RGNsb) !RGNsb
RGN monad: Operations • Monadic unit and bind returnRGN :: 8s,a. a!RGNsa thenRGN :: 8s,a,b. RGNsa! (a!RGNsb) !RGNsb
RGN monad: Operations • Monadic unit and bind returnRGN :: 8s,a. a!RGNsa thenRGN :: 8s,a,b. RGNsa! (a!RGNsb) !RGNsb
RGN monad: Operations • Create and read region allocated values new :: 8s,a. Hnds!a!RGNs (Refsa) read :: 8s,a. Refsa!RGNsa
RGN monad: Operations • Create and read region allocated values new :: 8s,a. Hnds!a!RGNs (Refsa) read :: 8s,a. Refsa!RGNsa
RGN monad: Encapsulation • Encapsulate and run a monadic computation runRGN :: 8a. (8s. RGNsa) !a
RGN monad: Encapsulation • Encapsulate and run a monadic computation runRGN :: 8a. (8s. RGNsa) !a
RGN monad: Encapsulation • Encapsulate and run a monadic computation runRGN :: 8a. (8s. RGNsa) !a “for all stacks” )no assumptions about stack of regions