One idea is to do your own garbage collection.
For simplicity, suppose that all arguments are of the same type k .
In addition to the main weak table containing memoized results with the key k * k , create a secondary weak table containing single arguments of type k . The idea is to check the main table from time to time and remove the bindings that are no longer needed. This is done by looking for arguments in the secondary table; then if any of them are gone, you will remove the binding from the main table.
(Disclaimer: I have not tested this, it may not work, or there may be better solutions)
Martin jambon
source share