1Ephemeron(3) OCaml library Ephemeron(3)
2
3
4
6 Ephemeron - Ephemerons and weak hash tables.
7
9 Module Ephemeron
10
12 Module Ephemeron
13 : sig end
14
15
16 Ephemerons and weak hash tables.
17
18 Ephemerons and weak hash tables are useful when one wants to cache or
19 memorize the computation of a function, as long as the arguments and
20 the function are used, without creating memory leaks by continuously
21 keeping old computation results that are not useful anymore because one
22 argument or the function is freed. An implementation using Hashtbl.t is
23 not suitable because all associations would keep the arguments and the
24 result in memory.
25
26 Ephemerons can also be used for "adding" a field to an arbitrary boxed
27 OCaml value: you can attach some information to a value created by an
28 external library without memory leaks.
29
30 Ephemerons hold some keys and one or no data. They are all boxed OCaml
31 values. The keys of an ephemeron have the same behavior as weak point‐
32 ers according to the garbage collector. In fact OCaml weak pointers are
33 implemented as ephemerons without data.
34
35 The keys and data of an ephemeron are said to be full if they point to
36 a value, or empty if the value has never been set, has been unset, or
37 was erased by the GC. In the function that accesses the keys or data
38 these two states are represented by the option type.
39
40 The data is considered by the garbage collector alive if all the full
41 keys are alive and if the ephemeron is alive. When one of the keys is
42 not considered alive anymore by the GC, the data is emptied from the
43 ephemeron. The data could be alive for another reason and in that case
44 the GC will not free it, but the ephemeron will not hold the data any‐
45 more.
46
47 The ephemerons complicate the notion of liveness of values, because it
48 is not anymore an equivalence with the reachability from root value by
49 usual pointers (not weak and not ephemerons). With ephemerons the no‐
50 tion of liveness is constructed by the least fixpoint of: A value is
51 alive if:
52
53 -it is a root value
54
55 -it is reachable from alive value by usual pointers
56
57 -it is the data of an alive ephemeron with all its full keys alive
58
59 Notes:
60
61 -All the types defined in this module cannot be marshaled using out‐
62 put_value or the functions of the Marshal module.
63
64 Ephemerons are defined in a language agnostic way in this paper: B.
65 Hayes, Ephemerons: A New Finalization Mechanism, OOPSLA'97
66
67
68 Since 4.03.0
69
70
71
72
73
74 module type S = sig end
75
76
77 The output signature of the functor Ephemeron.K1.Make and
78 Ephemeron.K2.Make . These hash tables are weak in the keys. If all the
79 keys of a binding are alive the binding is kept, but if one of the keys
80 of the binding is dead then the binding is removed.
81
82
83 module type SeededS = sig end
84
85
86 The output signature of the functor Ephemeron.K1.MakeSeeded and
87 Ephemeron.K2.MakeSeeded .
88
89
90 module K1 : sig end
91
92
93 Ephemerons with one key.
94
95
96 module K2 : sig end
97
98
99 Emphemerons with two keys.
100
101
102 module Kn : sig end
103
104
105 Emphemerons with arbitrary number of keys of the same type.
106
107
108 module GenHashTable : sig end
109
110
111 Hash tables on generic containers with notion of death and aliveness.
112
113
114
115
116
117OCamldoc 2021-07-22 Ephemeron(3)