structure MLton: sig val eq: 'a * 'a -> bool val isMLton: bool val share: 'a -> unit val shareAll: unit -> unit val size: 'a -> int structure Array: MLTON_ARRAY structure BinIO: MLTON_BIN_IO structure Cont: MLTON_CONT structure Exn: MLTON_EXN structure Finalizable: MLTON_FINALIZABLE structure GC: MLTON_GC structure IntInf: MLTON_INT_INF structure Itimer: MLTON_ITIMER structure Platform: MLTON_PLATFORM structure Pointer: MLTON_POINTER structure ProcEnv: MLTON_PROC_ENV structure Process: MLTON_PROCESS structure Profile: MLTON_PROFILE structure Random: MLTON_RANDOM structure Rlimit: MLTON_RLIMIT structure Rusage: MLTON_RUSAGE structure Signal: MLTON_SIGNAL structure Socket: MLTON_SOCKET structure Syslog: MLTON_SYSLOG structure TextIO: MLTON_TEXT_IO structure Thread: MLTON_THREAD structure Vector: MLTON_VECTOR structure Weak: MLTON_WEAK structure Word: MLTON_WORD where type word = Word.word structure Word8: MLTON_WORD where type word = Word8.word structure World: MLTON_WORLD end
Substructures
Values
-
eq (x, y)
-
isMLton
-
share x
-
shareAll ()
-
size x
-
returns true if x and y are equal as pointers. For simple types like char, int, and word, this is the same as equals. For arrays, datatypes, strings, tuples, and vectors, this is a simple pointer equality. The semantics is a bit murky.
-
is always true in a MLton implementation, and is always false in a stub implementation.
-
maximizes sharing in the heap for the object graph reachable from x.
-
maximizes sharing in the heap by sharing space for equivalent immutable objects. A call to shareAll performs a major garbage collection, and takes time proportional to the size of the heap.
-
returns the amount of heap space (in bytes) taken by the value of x, including all objects reachable from x by following pointers. It takes time proportional to the size of x. See below for an example.
Example of MLton.size
This example, size.sml, demonstrates the application of MLton.size to many different kinds of objects.
fun 'a printSize (name: string, min: int, value: 'a): unit= if MLton.size value >= min then (print "The size of " ; print name ; print " is >= " ; print (Int.toString min) ; print " bytes.\n") else () val l = [1, 2, 3, 4] val _ = ( printSize ("a char", 0, #"c") ; printSize ("an int list of length 4", 48, l) ; printSize ("a string of length 10", 24, "0123456789") ; printSize ("an int array of length 10", 52, Array.tabulate (10, fn _ => 0)) ; printSize ("a double array of length 10", 92, Array.tabulate (10, fn _ => 0.0)) ; printSize ("an array of length 10 of 2-ples of ints", 92, Array.tabulate (10, fn i => (i, i + 1))) ; printSize ("a useless function", 0, fn _ => 13) ) (* This is here so that the list is "useful". * If it were removed, then the optimizer (remove-unused-constructors) * would remove l entirely. *) val _ = if 10 = foldl (op +) 0 l then () else raise Fail "bug" local open MLton.Cont in val rc: int option t option ref = ref NONE val _ = case callcc (fn k: int option t => (rc := SOME k; throw (k, NONE))) of NONE => () | SOME i => print (concat [Int.toString i, "\n"]) end val _ = (print "The size of a continuation option ref is " ; if MLton.size rc > 1000 then print "> 1000.\n" else print "< 1000.\n") val _ = case !rc of NONE => () | SOME k => (rc := NONE; MLton.Cont.throw (k, SOME 13))
Compile and run as usual.
% mlton size.sml % ./size The size of a char is >= 0 bytes. The size of an int list of length 4 is >= 48 bytes. The size of a string of length 10 is >= 24 bytes. The size of an int array of length 10 is >= 52 bytes. The size of a double array of length 10 is >= 92 bytes. The size of an array of length 10 of 2-ples of ints is >= 92 bytes. The size of a useless function is >= 0 bytes. The size of a continuation option ref is > 1000. 13 The size of a continuation option ref is < 1000.