Version 4 of Result caching

Updated 2002-07-19 08:10:10

Richard Suchenwirth 2002-07-19 - If you repeatedly do the same time-consuming calculations (e.g. string similarity), it may be a good idea to keep the previous results in a cache and, for every call of the same command after the first, just retrieve the cached value.

This implementation uses an array in caller's scope (he might make it global, or not) whose name is explicitly given, indexed by the command. This way, you could have multiple caches in parallel, and selectively unset some when needed. Note that variable values are not substituted in a braced command - to tie the cached result to the variable values, use list to group the command:

 set result [cached c [list myLongCommand $i $j]]

 proc cached {cacheName command} {
    upvar 1 $cacheName cache
    if {![info exists cache($command)]} {
        set cache($command) [uplevel 1 $command]
    } else {
        set cache($command)
    }
 }

# testing:

 % time {cached c {stringSimilarity "Tool Command Language" "Tool Command Languages"}}
 422000 microseconds per iteration
 % time {cached c {stringSimilarity "Tool Command Language" "Tool Command Languages"}}
 0 microseconds per iteration
 % array get c
 {stringSimilarity "Tool Command Language" "Tool Command Languages"} 0.953488372093

The second call saves you more than 0.4 seconds...(on second try with cleared cache, it was only 63 ms, though). Note however that caching results makes only sense if the command always returns the same value - caching commands like

 gets $fp
 expr rand()
 clock seconds

is certainly a bad idea... And side effects are of course not produced, as the command isn't executed after the first time.

DKF notes: The above timings aren't very good (there's granularity problems) but on Solaris8 (on a not-very-fast processor) I get:

 % time {cached c {stringSimilarity "Tool Command Language" "Tool Command Languages"}}
 97151 microseconds per iteration
 % time {cached c {stringSimilarity "Tool Command Language" "Tool Command Languages"}} 200
 44 microseconds per iteration

Philip Greenspun calls this concept "memoization"...


Category Concept | Arts and crafts of Tcl-Tk programming