Autoloading is convenient and avoids wasted memory for modules that should be available at the interactive prompt but have not actually been used so far. However, sometimes one wants to be even more aggressive and save the space needed for a function until--at runtime--that function is actually being dynamically invoked.
CM does not provide immediate support for this kind of dynamic linking, but it is quite simple to achieve the effect by carefully arranging some helper libraries and associated stub code.
Consider the following module:
structure F = struct
fun f (x: int): int =
G.g x + H.h (2 * x + 1)
end
Let us further assume that the implementations of structures G and H are rather large so that it would be worthwhile to avoid loading the code for G and H until F.f is called with some actual argument. Of course, if F were bigger, then we also want to avoid loading F itself.
To achieve this goal, we first define a hook module which will be the place where the actual implementation of our function will be registered once it has been loaded. This hook module is then wrapped into a hook library. Thus, we have f-hook.cm:
Library
structure F_Hook
is
f-hook.sml
and f-hook.sml:
structure F_Hook = struct
local
fun placeholder (i: int) : int =
raise Fail "F_Hook.f: unitinialized"
val r = ref placeholder
in
fun init f = r := f
fun f x = !r x
end
end
The hook module provides a reference cell into which a function of type equal to F.f can be installed. Here we have chosen to hide the actual reference cell behind a local construct. Accessor functions are provided to install something into the hook ( init) and to invoke the so-installed value ( f).
With this preparation we can write the implementation module f-impl.sml in such a way that not only does it provide the actual code but also installs itself into the hook:
structure F_Impl = struct
local
fun f (x: int): int =
G.g x + H.h (2 * x + 1)
in
val _ = F_Hook.init f
end
end
The implementation module is wrapped into its implementation
library f-impl.cm:
Library
structure F_Impl
is
f-impl.sml
f-hook.cm
g.cm (* imports G *)
h.cm (* imports H *)
Note that f-impl.cm must mention f-hook.cm for
f-impl.sml to be able to access structure F_Hook.
Finally, we replace the original contents of f.sml with a stub module that defines structure F:
structure F = struct
local
val initialized = ref false
in
fun f x =
(if !initialized then ()
else if CM.make "f-impl.cm" then initialized := true
else raise Fail "dynamic linkage for F.f failed";
F_Hook.f x)
end
end
The trick here is to explicitly invoke CM.make the
first time F.f is called. This will then cause f-impl.cm
(and therefore g.cm and also h.cm) to be loaded and the
``real'' implementation of F.f to be registered with the hook
module from where it will then be available to this and future calls
of F.f.
For the new f.sml to be compiled successfully it must be placed into a library f.cm that mentions f-hook.cm and $smlnj/cm.cm. As we have seen, f-hook.cm exports F_Hook.f and $smlnj/cm.cm is needed because it exports CM.make:
Library
structure F
is
f.sml
f-hook.cm
$smlnj/cm.cm (* or $smlnj/cm/full.cm *)
Beware! This solution makes use of $smlnj/cm.cm which in turn requires the SML/NJ compiler to be present. Therefore, is worthwhile only for really large program modules where the benefits of their absence are not outweighed be the need for the compiler.