i threw in the line reading code from /sys/src/cmd/read.c into rc.
here are the results:

9grid% cat builtinread
#!/tmp/rcread
for (i in `{seq 1 10000}) { echo $i | read } > /dev/null
9grid% cat origread
#!/bin/rc
for (i in `{seq 1 10000}) { echo $i | read } > /dev/null
9grid% cat noread
#!/bin/rc
for(i in `{seq 1 10000}) { echo $i } > /dev/null

9grid% time builtinread; time origread
1.49u 14.63s 17.61r      builtinread
1.63u 21.99s 18.58r      origread

I do see a decrease of the context switches by half, but that time is
certainly taken by rc itself.

just the effects of the echo preceding the read:
9grid% time noread
0.87u 9.36s 13.20r       noread
9grid%

and just by itself (the rc builtin doesn't support arguments, this
should illustrate that it is in fact a builtin):

9grid% read < /tmp/test
test
9grid% read /tmp/test
(broken)
(broken)
9grid% /bin/read /tmp/test
test
9grid%


under a debugging ramdisk we can see the difference in reads for the
two programs (you can guess that the context switch and the reading of
the binary are lost in the noise of reading a file char-by-char:
9grid% read < test
ramfs 518560:<-Twalk tag 12 fid 579 newfid 693 nwname 1 0:test
ramfs 518560:->Rwalk tag 12 nwqid 1 0:(0000000000000003 3 )
ramfs 518560:<-Topen tag 12 fid 693 mode 0
ramfs 518560:->Ropen tag 12 qid (0000000000000003 3 ) iounit 8192
ramfs 518560:<-Tread tag 12 fid 693 offset 0 count 1
ramfs 518560:->Rread tag 12 count 1 't'
ramfs 518560:<-Tread tag 12 fid 693 offset 1 count 1
ramfs 518560:->Rread tag 12 count 1 'e'
ramfs 518560:<-Tread tag 12 fid 693 offset 2 count 1
ramfs 518560:->Rread tag 12 count 1 's'
ramfs 518560:<-Tread tag 12 fid 693 offset 3 count 1
ramfs 518560:->Rread tag 12 count 1 't'
ramfs 518560:<-Tread tag 12 fid 693 offset 4 count 1
ramfs 518560:->Rread tag 12 count 1 '
'
test
ramfs 518560:<-Tclunk tag 12 fid 693
ramfs 518560:->Rclunk tag 12
9grid% ./read < test
ramfs 518560:<-Twalk tag 12 fid 579 newfid 693 nwname 1 0:test
ramfs 518560:->Rwalk tag 12 nwqid 1 0:(0000000000000003 3 )
ramfs 518560:<-Topen tag 12 fid 693 mode 0
ramfs 518560:->Ropen tag 12 qid (0000000000000003 3 ) iounit 8192
ramfs 518560:<-Twalk tag 12 fid 579 newfid 644 nwname 1 0:read
ramfs 518560:->Rwalk tag 12 nwqid 1 0:(0000000000000002 8 )
ramfs 518560:<-Topen tag 12 fid 644 mode 3
ramfs 518560:->Ropen tag 12 qid (0000000000000002 8 ) iounit 8192
ramfs 518560:<-Tread tag 12 fid 644 offset 0 count 32
ramfs 518560:->Rread tag 12 count 32 '000001eb 00007e20 00001084
00000420 00003e21 000013b7 00000000 000016c6'
ramfs 518560:<-Tclunk tag 12 fid 644
ramfs 518560:->Rclunk tag 12
ramfs 518560:<-Tread tag 12 fid 693 offset 0 count 1
ramfs 518560:->Rread tag 12 count 1 't'
ramfs 518560:<-Tread tag 12 fid 693 offset 1 count 1
ramfs 518560:->Rread tag 12 count 1 'e'
ramfs 518560:<-Tread tag 12 fid 693 offset 2 count 1
ramfs 518560:->Rread tag 12 count 1 's'
ramfs 518560:<-Tread tag 12 fid 693 offset 3 count 1
ramfs 518560:->Rread tag 12 count 1 't'
ramfs 518560:<-Tread tag 12 fid 693 offset 4 count 1
ramfs 518560:->Rread tag 12 count 1 '
'
test
ramfs 518560:<-Tclunk tag 12 fid 693
ramfs 518560:->Rclunk tag 12
9grid%

so, an optimized /sys/src/cmd/read.c that doesn't read char-by-char
should give us an improvement, right? right:
9grid% newaread
1.52u 22.56s 15.66r      newaread

and that's just for the silly "test" string. the improvement would be
bigger for longer strings.

code for aread is on sources: /contrib/andrey/aread.c

YMMV.

Reply via email to