
Fast AmigaDOS IO
----------------

by Martin Taillefer

Reading and writing data is crucial to most applications and is in many cases a
major bottleneck. Using the AmigaDOS' sophisticated file system architecture
can help reduce, and sometimes eliminate, the time spent waiting for IO to
complete. This article presents six small routines that can greatly improve an
application's IO performance.

Normally, an application processes a file in a manner similar to the following:

  1 - Open the file

  2 - Read some data

  3 - Process data just read

  4 - Repeat steps 2 and 3 until all data is processed

  5 - Close file

Although the above sequence works fine, it doesn't make full use of the Amiga's
multitasking abilities. Step 2 in the above can become a serious bottleneck.
Whenever the application needs some data by using the DOS Read() function,
AmigaDOS has to put that task to sleep, and initiate a request to the file
system to have it fetch the data. The file system then starts up the disk
hardware and reads the data. Once the data is read, the application is woken
up and can start processing the data just read.

The point to note in the above paragraph is that when the file system is
reading data from disk, the application is asleep. Wouldn't it be nice if
the application could keep running while data is being fetched for it?

Most Amiga hard drives make use of DMA (Direct Memory Access). DMA enables
a hard drive to transfer data to memory _at the same time_ as the CPU does
some work. This parallelism is what makes the set of accompanying routines
so efficient. They exploit the fact that data can be transfered to memory while
the application is busy processing other data.

Using the asynchronous IO routines, an application's IO happens like this:

  1 - Open the file, ask the file system to start reading ahead

  2 - Read some data, ask the file system to read more data

  3 - Process data

  4 - Repeat steps 2 and 3 until all data is processed

  5 - Close file

Immediately after opening the file, a request is sent to the file system to get
it reading data in the background. By the time the application gets around to
reading the first byte of data, it is likely already in memory. That means the
application doesn't need to wait and can start processing the data. As soon as
the application starts processing data from the file, a second request is sent
out to the file system to fill up a second buffer. Once the application is done
processing the first buffer, it starts processing the second one. When this
happens, the file system starts filling up the first buffer again with new
data. This process continues until all data has been read.

The whole technique is known as "double-buffered asynchronous IO" since it uses
two buffers, and happens in the background (asynchronously).

The set of functions presented below offer high-performance IO using the
technique described above. The interface is very similar to standard
AmigaDOS files. These routines enable full asynchronous read/write of
any file.
