SKIP and SEEK on Ports

Carl Sassenrath, CTO
REBOL Technologies
16-Apr-2008 17:38 GMT

Article #0128
Main page || Index || Prior Article [0127] || Next Article [0129] || 4 Comments || Send feedback

As you may or may not know, in R3, a port value no longer stores the series offset. Instead, the offset is determined at a lower level within the port object.

As a clear example, if you write:

port: open %file
d1: read/part port 100
d2: read/part port 100

You will read the first 100 bytes, then the next 100 bytes, etc.

But, what if you want to read 100 bytes offset at a specific location, say 1000 bytes into the file. There are two possible solutions.

Method 1: use a refinement

We can add the /at refinement:

data: read/part/at port 100 1000

The advantage is that the seek operation occurs during the read action itself, so the port handler and device are only called once. This eliminates a small amount of overhead, at the small cost of an extra refinement.

Another advantage is that you can read part of an unopened file:

data: read/part/at %file.txt 100 1000

So, everything happens on that one line. It can be useful at times.

Method 2: Make the AT action work for ports

This would be defined to specify the absolute position, and would effect the port state.

at port 1000
data: read/part port 100

And, because at returns the port, you can write:

data: read/part at port 1000 100

The advantage is that we can eliminate one refinement from read, but we must make the additional call for at, which is an extra call to the port handler, dispatched to the device.

SKIP also could be allowed

For both of these, it would also be possible to support skip, a relative offset function:

data: read/part/skip port 100 512


data: read/part skip port 512 100

My opinion...

I would say that I am leaning toward the refinement. The two main reasons are:

  1. It eliminates an extra call to the port handler and device, and it calls the seek immediately before the actual low level read.
  2. It works for un-opened files, so you can easily fetch a chunk from a file without opening it beforehand.

Note that it may also be possible to allow both the refinement and the at but is there an advantage in that?

But, I'm writing all of this to get your comments, so let me know your thoughts.


Updated 11-Apr-2024 - Edit - Copyright REBOL Technologies -