Cacheing subroutine results in the OS
Nov. 21st, 2013 09:38 pmImagine that a computer program's run-time environment is granted sufficient knowledge of the program's internals to become aware that:
- A particular subroutine takes a significantly long amount of time or processes a significantly large amount of data.
- The subroutine is known to always produce the same result from the same input.
- All sources of input to that subroutine are knowable before the subroutine runs.
- None of the inputs have changed since the last run.
The runtime may ask the operating system to keep a cache of the results in memory after the process exits. The cache may be marked as a low-priority so it will be overwritten if the process does not run that often. The OS will keep a list of cache locations somewhere so that the next run of the run-time environment will know to rewrite the program to look for a cache when it reaches a specific instruction.
Potential benefits:
- The parsing process will be skipped for any process that reads from a data file.
- Applied to the program itself, some of the program's setup code could be replaced by a memcpy of a cached result.
Potential drawbacks:
- Excess CPU and memory use from cacheing unused results.
- The cache creates a security hole.
- Excessive complication potentially leading to net slowdowns in operation.