Project

General

Profile

Feature #11484

add output offset for readpartial/read_nonblock/etc

Added by normalperson (Eric Wong) about 5 years ago. Updated about 3 years ago.

Status:
Feedback
Priority:
Normal
Target version:
-
[ruby-core:<unknown>]

Description

Specifying a destination offset will allow allocating less garbage
strings to improve performance when reading partial data off a pipe
or socket.

For example:

    tmp = "".b
    buf = "".b
    case rv = io.read_nonblock(8192, tmp, exception: false)
    when String
      buf << tmp
      break if parsing_done?(buf)
    else
      return rv
    end while true

Can be written as:

    buf = "".b
    case rv = io.read_nonblock(8192, buf, exception: false, off_out: buf.size)
    when String
      break if parsing_done?(buf)
    else
      return rv
    end while true

For reference, here is the relevant part from "perldoc -f sysread"
    sysread FILEHANDLE,SCALAR,LENGTH,OFFSET
    sysread FILEHANDLE,SCALAR,LENGTH
    -snip-
            An OFFSET may be specified to place the read data at some place
            in the string other than the beginning. A negative OFFSET
            specifies placement at that many characters counting backwards
            from the end of the string. A positive OFFSET greater than the
            length of SCALAR results in the string being padded to the
            required size with "\0" bytes before the result of the read is
            appended.
    -snip-

Maybe we can shortcut by allowing "off_out: :end" to avoid redundant
words and potentially using the wrong variable:

  io.read_nonblock(8192, buf, exception: false, off_out: :end)

I'm not sure when I'll have time to work on this feature.
Lately I've been working in Perl again and rediscovered this API subtlety.

Also available in: Atom PDF