Optimization of table references
The COBOL compiler optimizes table references in several ways.
For the table element reference ELEMENT(S1 S2 S3)
,
where S1
, S2
, and S3
are
subscripts, the compiler evaluates the following expression:
comp_s1 * d1 + comp_s2 * d2 + comp_s3 * d3 + base_address
Here comp_s1
is the value of S1
after
conversion to binary, comp-s2
is the value of S2
after
conversion to binary, and so on. The strides for each dimension are d1
, d2
,
and d3
. The stride of a given dimension
is the distance in bytes between table elements whose occurrence numbers
in that dimension differ by 1 and whose other occurrence numbers are
equal. For example, the stride d2
of the second dimension
in the above example is the distance in bytes between ELEMENT(S1
1 S3
)
and ELEMENT(S1
2 S3
).
Index computations are similar to subscript computations, except that no multiplication needs to be done. Index values have the stride factored into them. They involve loading the indexes into registers, and these data transfers can be optimized, much as the individual subscript computation terms are optimized.
Optimization of variable-length items
A group item that contains a subordinate OCCURS
DEPENDING ON
data item has a variable length. The program
must perform special code every time a variable-length data item is
referenced.
Because this code is out-of-line, it might interrupt optimization. Furthermore, the code to manipulate variable-length data items is much less efficient than that for fixed-size data items and can significantly increase processing time. For instance, the code to compare or move a variable-length data item might involve calling a library routine and is much slower than the same code for fixed-length data items.
Comparison of direct and relative indexing
Relative index references are as fast as or faster than direct index references.
The direct indexing in ELEMENT
(I5, J3, K2)
requires this preprocessing:
SET I5 TO I
SET I5 UP BY 5
SET J3 TO J
SET J3 DOWN BY 3
SET K2 TO K
SET K2 UP BY 2
This processing makes the direct indexing less efficient
than the relative indexing in ELEMENT (I + 5, J - 3, K + 2)
.