File Open and Close
The file control environment consists of a number of structures containing all the parameters required to access the file. Each time a file is opened or closed, these data structures have to be manipulated, the amount of execution time increasing with the number of open files.
Certain operations, although not explicit file open or file close, manipulate these data structures and should also be considered. These are:
-
VAR2=VAR1
whereVAR1
is a file variable. This causes the file control structure elements referenced byVAR1
to be duplicated, andVAR2
set to refer to the duplicate copies. Both variables then refer to the same file. However, ifVAR1
is used in a subsequent OPEN statement, effectively closing the current file and opening a new one,VAR2
will still refer to the original file. -
A file opened in a external subroutine will be closed by the RETURN statement unless the variable is passed back to the calling program.
-
VAR1=X
whereVAR1
is a file variable. This causes the file referenced byVAR1
to be closed.
Beware of constructs in which assignment of file variables may not be obvious, such as:
OPEN . . . TO F1 . . .
OPEN . . . TO F2 . . .
. . .
X=F1
GOSUB 1000
X=F2
GOSUB 1000
. . .
1000 REM SUBR: PROCESS FILE
. . .
Remote Files
The overhead of opening remote files is considerably higher than that of local files. The elapsed time taken to open a remote file depends on the type of network being used, and can be very noticeable when using a wide area network.
Any performance problems caused by the way files are opened and closed within programs become considerably worse when the files are remote, therefore more care must be taken if remote files are, or may be, used.
Reality Files
On Reality, files are stored in the UNIX or Windows file system (as appropriate), thus opening a file in the Reality environment results in a corresponding file open in the UNIX or Windows environment. This results in a noticeable overhead when opening and closing files, thus exaggerating any performance problems with file open and close.
Deferred Close
REALITY 7.2 and all releases of Reality on UNIX and Windows implement a deferred close mechanism to reduce the overheads in applications that repeatedly open and close a set of files. This works by retaining the data structures, but marking them as closed, so that reopening the file does not have to rebuild them.
Opening Multiple Files
If many files are to be opened, choose carefully the point in the program where the files are opened. There are two approaches:
-
Open all files at once at the start of the program. This causes an initial delay but avoids slow responses later in the program.
-
Do not open files until they need to be used. This avoids opening files that are not used, but may cause poor response when the files are opened.
The most appropriate approach depends on the application, and a combination of the two may be the best solution.
Considerations for Efficiency
To avoid performance problems associated with opening and closing files:
-
Do not close files that will be used again.
-
Try not to open or close files within a loop.
-
Avoid opening the same file more than once; for example, place in COMMON space.
-
Avoid assignment statements containing file variables.
-
Do not reuse file variables for other purposes.
-
Avoid opening files in external subroutines that will be repeatedly called, as a RETURN closes files opened to local file variables.