Date: Wed, 10 Aug 1994 09:11:50 -0400 (EDT)
From: Edwin Chee <echee@isnet.is.wfu.edu>
Subject: Macintosh Disk Cache fix -- 25 times speedup

Below is an interesting thread on making the Macintosh Disk Cache much 
more efficient with a simple hack.  Stuart Cheshire has written a simple, 
useful system extension that he is distributing as freeware.  I would 
welcome any discussion on his idea particularly regarding data integrity, 
reliability, stability, and efficiency.  Also, does anyone have any 
ideas on how to improve on his program?  If his modification to the write 
cache is really as good as I suspect, I would hope that Apple would 
include this in their next version of the Mac OS.

Please forward replies to echee@isnet.is.wfu.edu and I will compile a 
complete record of this thread and distribute it at a later date.


==================================================================


Here's a message I posted on Thursday to the Nuntius mailing list:

 -----------------------------------------------------------------

Has anyone else noticed that at the end of extracting a binary in
Nuntius the disk light comes on, stays on for a few seconds, and
freezes the Mac for that duration? It gets unbearable if you have
a large disk cache, but even with only a 256K cache it can freeze
the Mac for up to 5 seconds.

This is not the fault of Nuntius -- many other programs like BinHex
decoders, uudecoders, archive expanders etc. seem to suffer the same
problem.

This really annoys me. One of the good features of Nuntius is the way
it lets you continue working while it is doing other things in the
background, so having it freeze like this is particularly galling.

The problem is that Nuntius (and other programs) write their data to
disk in chunks (say 4K each) and the Mac caches the blocks in its
disk cache. When the file is closed the data is finally written to disk,
and this is what causes the big freeze up. It would be much better if
the data were written continually to disk, instead of in one big burst
at the end.

Yesterday morning I wrote a little INIT which sets the File Manager
"don't cache" bit for disk writes of 1K or more. It does this by
installing the following patch on the _Write system call:

                 tst.w   IOParam.ioRefNum(a0)         ; Is this a file write?
                 bmi.s   @sys_write
                 cmp.l   #1024, IOParam.ioReqCount(a0); Is it at leask 1K?
                 blo.s   @sys_write
                 ori.w   #0x20, IOParam.ioPosMode(a0) ; Set "Don't cache" bit
extern sys_write:jmp     0x12345678                   ; Resume the system 
call

One surprising artifact of this is that it not only amortises the disk
time over all the writes, but it also makes it 25 times faster.

What?

Yes, it's true.

I set my disk cache to 768K, and wrote a test program which wrote to a
file in 32 blocks of 16K each, making a total of 512K.

Without the INIT, the writes took almost no time, but the Close call
took 11 seconds, averaging about 45K/second write rate.

With the INIT, the whole thing took under half a second, averaging
over a megabyte per second.

Go figure.

This may not make much difference to people connecting over modem, but
for people on Ethernet it makes a huge difference.

The INIT is below, in BinHex form. Decode it, install it, and let me
know what you think.

It has no ICON, because the total size of the INIT (including balloon
help) is just under 1K, and it seemed a pity to spoil that with a big
fat colour icon. (Besides, I couldn't be bothered to draw one.)

 -----------------------------------------------------------------

Here is one reply I got, which proves I wasn't hallucinating:

>> From: joanna@mail.utexas.edu (Joanna L. Castillo)
>> Subject: Re: Long Mac freezes with Nuntius (and other program)
>>
>> Hi, Stuart -
>>
>> >Yesterday morning I wrote a little INIT  [ ... ]
>>
>> Wow!  I installed the INIT... I tried copying a folder that had several
>> files and sub-folders (a little over 900K total) to a floppy.  With the
>> Wow!  I installed the INIT... I tried copying a folder that had several
>> files and sub-folders (a little over 900K total) to a floppy.  With the
>> INIT installed, it took about 30 seconds.  Without, it took 2 minutes.
>> Thank you so much.
>>
>> Joanna.
>>
>> FYI:  I'm running a Mac IIci, 24MB RAM, cache card, ethernet, tuned
>>       7.0.1 system, several inits, and disk cache set at 512K.

I'll post the source code for the INIT, and the test program, to a
separate thread. This INIT itself is below.

Stuart Cheshire <cheshire@cs.stanford.edu>
 * <A HREF="file://brubeck.stanford.edu/www/cheshire-bio.html">WWW</A>
 * Stanford Distributed Systems Group Research Assistant
 * Escondido Village Resident Computer Coordinator
 * Macintosh Programmer

(This file must be converted with BinHex 4.0)

:$&GbDA4P9'KbEh9RD!"*6NP89h*8D!!!!!!!!!!!!rJBk3!!!!!"!!!!!i8!!!+
&!!!!F`!)6Ud!3Le!!!`-J!#!!!"M"(!!B%)-VJ!!1TJ!$'3S$&GbDA4P9'KbEh9
RD&S#!!!!58j*9&Gb9'J"!2rrrrm!!%P1594AFP4S!3$rrrrr!!!!!!!!!!!!!!!
!!!!!!!!!UFK3'J!!!!!!!!2i!)!!!'-'F!"J!!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