# CachingMethod

This feature requires Analytica Enterprise

## Attribute CachingMethod

When the results for variables are computed, Analytica automatically saves these results in memory so they don't need to be recomputed again later when they get used or viewed again. Analytica carries out this caching and dependency maintenance automatically, and you seldom have to worry about it. This represents a core feature of Analytica, is it provides many benefits, see Controlling When Result Values Are Cached.

Caching of results, however, has the downside that it consumes memory in order to store those computed results. When you are running low on memory, this may limit the size of the computations that you are able to carry out with your model. The CachingMethod attribute allows you to exert some control over which variables consume memory.

## Accessing the CachingMethod attribute

You can only access the CachingMethod attribute from Analytica Enterprise edition and higher. In addition, to access it, you must turn it on from the Attributes dialog. Follow these steps:

1. Select Edit → Attribute... on the menu to bring up the Attributes dialog.
2. With Variable selected, scroll down to locate CachingMethod
3. Double click on CachingMethod to make a check mark appear next to it.
4. Press OK

With this checked, CachingMethod will appear on the attribute selection pulldown and in object windows). To set the attribute for a particular variable, enter edit mode, click on the variable to select it, open the attribute pane or the object window, choose the desired caching method from the pull-down menu next to the Caching Method attribute.

## Possible Settings

The CachingMethod attribute, if set, must be set to a single integer value. The following are the possible values:

0 = Analytica's default (this is Always cache)
1 = Always cache result
2 = Never cache non-atomic results
3 = Release cached result after all children are fully computed
4 = Never cache results, even if scalar.

These values apply to both cached mid-value and sample-value results.

No memory is saved by selecting option 4 over option, but there might be rare cases where you want a fresh result every time the result is requested (e.g., a call to Today()).

You can also separately control how mid and sample values are cached. Basically, Analytica looks at bits 1-4 (the four least significant bits) of the integer value for the Mid-value setting, and at bits 5-8 for the prob-value setting. If bits 5-8 are all zero, then the prob-value uses the same configuration as the mid-value. If bits 5-8 contain a "2" (as is the case with the value 32 = 001000002), then the prob-value is set to never-cache. This scheme results in the following meaningful configurations:

18 = Always cache sample, Never non-atomic cache mid
19 = Always cache sample, release mid
20 = Always cache sample, Never cache any mid
33 = Never cache sample, always cache mid
35 = Never cache sample, release mid
49 = Release sample, always cache mid
50 = Release sample, never non-atomic cache mid
52 = Release sample, never cache any mid value

## How it works

When a variable is set to CachingMethod = 2, its value is never cached. This means that its definition must be reevaluated every time it is accessed.

When a variable is set to CachingMethod = 3, then its value is cached until all children of that variable have been fully computed. Specifically, as soon as Analytica can prove that no child of the variable will need access to the cached value, Analytica releases the cached value, freeing up that memory. Simple numeric results are not released, since doing so does not reclaim any extra memory.

For more details, see Controlling When Result Values Are Cached.

## Availability by Analytica Edition

To access the CachingMethod attribute, you must be using Analytica Enterprise, Analytica Optimizer, or ADE. It is only possible to change the value from these editions. The attribute's value can be changed in a button script (or a function called from a button script) in Analytica Power Player and ACP. The attribute cannot be viewed from the GUI from Analytica Professional or Analytica Player, although if you are running a model where the CachingMethod has been configured (i.e., by someone who built the model in Enterprise), the caching method will take effect when the model is evaluated in Analytica Professional, Analytica Player, or ACP.

## Use in Dynamic loops

Turning off caching in the wrong situations can decrease the efficiency of some calculations. This is especially true inside dynamic loops. If you turn caching off in the wrong places, you could cause a calculation that previously took a linear number of steps to take a quadratic number of steps to compute.

To illustrate this, consider a simple dynamic loop:

A → B → C → A

where A depends on C[Time-1], and B and C use no Time offset.

In a simple loop like this, you can safely turn off caching on 2 of the 3 variables of the loop without significantly impacting the efficiency of the computation. For example, suppose you turn off caching at A and B. When it comes time to compute C[Time = t], it'll need to compute B[Time = t], which needs A[Time = t], which needs C[Time = t - 1], which will already be cached.

On the other hand, if you were to turn off caching on all three variables, the evaluation would become horribly inefficient, taking $O(n^2)$ time, where n is size(Time). In this case, when it needs to compute C[Time = 10], it would need C[Time = 9], which since it isn't cached needs to compute C[Time = 8], which needs to compute C[Time = 7] and so on back to C[Time = 1]. Then when it has to compute C[Time = 11], it would need to recompute all those previous values again.

In a simple dynamic loop, with a single time one offset, you are safe from this inefficiency as long as every cycle has at least one cached variable. With more complex offsets, such as dependencies involving both [Time - 1] and [Time - 2] offsets, a single cycle breaker may not be enough.

In general, this effect isn't limited to dynamic loops -- you are just more likely to experience the increase in inefficiency in a dramatic way in a dynamic if used inappropriately. A non-dynamic model with a structure as follows would be susceptible.

If B1, B2, C1 and C2 are not cached, B1 and B2 would each need to be computed four times when evaluating Result.

## Related Attributes

In order to track when child variables have been computed, and the dependencies of children across evaluation modes, the following two read-only attributes are used:

### Att_EvalMode_Dep

This record dependency information for children of variable configured to release-cache (CachingMethod = 3). The attribute only appears on variables whose parent has CachingMethod = 3 (in either bits 1-4 or bits 5-8). The attribute is maintained by Analytica, and cannot be set by the user.

The attribute contains a tuple, where the odd-element contain parent variable identifiers, and the even elements contain integers encoding the nature of the dependency. The integers have the following possible values, or combinations of these values added together:

• 1 = Context
• 2 = Mid
• 4 = Sample

Suppose the Att_EvalMode_Dep value for Y is: [X, 5]. This indicates that Y depends on both the context value of X and on the sample value of Y (5 = 4 + 1). From this we know that to compute Mid(Y), we need both Mid(X) and Sample(X). However, to compute Sample(Y), we only need Sample(X) -- not Mid(X). In this case, the cached value for Mid(X) is eligible to be released when Mid(Y) is fully computed.

### Att_Computed_Relsd

The Att_Computed_Relsd attribute is used to track values that have been computed but not cached, as can happen when CachingMethod = 2 or CachingMethod = 3. A variable with CachingMethod = 3 needs to know when its children have been computed. If the child is configured with CachingMethod = 2, then it can't tell that the value is computed by looking for a cached value. Instead, this attribute records that the result has been computed.

Possible values:

• 1 = Mid value has been computed and released
• 2 = Sample value has been computed and released
• 3 = Both Mid and Sample have been computed and released.

## History

The CachingMethod attribute was introduced in Analytica 4.2.