Documentation ¶
Overview ¶
The `fwd` package provides a buffered reader and writer. Each has methods that help improve the encoding/decoding performance of some binary protocols.
The `fwd.Writer` and `fwd.Reader` type provide similar functionality to their counterparts in `bufio`, plus a few extra utility methods that simplify read-ahead and write-ahead. I wrote this package to improve serialization performance for http://github.com/tinylib/msgp, where it provided about a 2x speedup over `bufio` for certain workloads. However, care must be taken to understand the semantics of the extra methods provided by this package, as they allow the user to access and manipulate the buffer memory directly.
The extra methods for `fwd.Reader` are `Peek`, `Skip` and `Next`. `(*fwd.Reader).Peek`, unlike `(*bufio.Reader).Peek`, will re-allocate the read buffer in order to accommodate arbitrarily large read-ahead. `(*fwd.Reader).Skip` skips the next `n` bytes in the stream, and uses the `io.Seeker` interface if the underlying stream implements it. `(*fwd.Reader).Next` returns a slice pointing to the next `n` bytes in the read buffer (like `Peek`), but also increments the read position. This allows users to process streams in arbitrary block sizes without having to manage appropriately-sized slices. Additionally, obviating the need to copy the data from the buffer to another location in memory can improve performance dramatically in CPU-bound applications.
`fwd.Writer` only has one extra method, which is `(*fwd.Writer).Next`, which returns a slice pointing to the next `n` bytes of the writer, and increments the write position by the length of the returned slice. This allows users to write directly to the end of the buffer.
Index ¶
- Constants
- type Reader
- func (r *Reader) BufferSize() int
- func (r *Reader) Buffered() int
- func (r *Reader) Next(n int) ([]byte, error)
- func (r *Reader) Peek(n int) ([]byte, error)
- func (r *Reader) Read(b []byte) (int, error)
- func (r *Reader) ReadByte() (byte, error)
- func (r *Reader) ReadFull(b []byte) (int, error)
- func (r *Reader) Reset(rd io.Reader)
- func (r *Reader) Skip(n int) (int, error)
- func (r *Reader) WriteTo(w io.Writer) (int64, error)
- type Writer
- func (w *Writer) BufferSize() int
- func (w *Writer) Buffered() int
- func (w *Writer) Flush() error
- func (w *Writer) Next(n int) ([]byte, error)
- func (w *Writer) ReadFrom(r io.Reader) (int64, error)
- func (w *Writer) Write(p []byte) (int, error)
- func (w *Writer) WriteByte(b byte) error
- func (w *Writer) WriteString(s string) (int, error)
Constants ¶
const (
// DefaultReaderSize is the default size of the read buffer
DefaultReaderSize = 2048
)
const ( // DefaultWriterSize is the // default write buffer size. DefaultWriterSize = 2048 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader is a buffered look-ahead reader
func NewReaderSize ¶
NewReaderSize returns a new *Reader that reads from 'r' and has a buffer size 'n'
func (*Reader) BufferSize ¶
BufferSize returns the total size of the buffer
func (*Reader) Next ¶
Next returns the next 'n' bytes in the stream. Unlike Peek, Next advances the reader position. The returned bytes point to the same data as the buffer, so the slice is only valid until the next reader method call. An EOF is considered an unexpected error. If an the returned slice is less than the length asked for, an error will be returned, and the reader position will not be incremented.
func (*Reader) Peek ¶
Peek returns the next 'n' buffered bytes, reading from the underlying reader if necessary. It will only return a slice shorter than 'n' bytes if it also returns an error. Peek does not advance the reader. EOF errors are *not* returned as io.ErrUnexpectedEOF.
func (*Reader) ReadFull ¶
ReadFull attempts to read len(b) bytes into 'b'. It returns the number of bytes read into 'b', and an error if it does not return len(b). EOF is considered an unexpected error.
func (*Reader) Skip ¶
Skip moves the reader forward 'n' bytes. Returns the number of bytes skipped and any errors encountered. It is analogous to Seek(n, 1). If the underlying reader implements io.Seeker, then that method will be used to skip forward.
If the reader encounters an EOF before skipping 'n' bytes, it returns io.ErrUnexpectedEOF. If the underlying reader implements io.Seeker, then those rules apply instead. (Many implementations will not return `io.EOF` until the next call to Read.)
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer is a buffered writer
func NewWriter ¶
NewWriter returns a new writer that writes to 'w' and has a buffer that is `DefaultWriterSize` bytes.
func NewWriterSize ¶
NewWriterSize returns a new writer that writes to 'w' and has a buffer that is 'size' bytes.
func (*Writer) BufferSize ¶
BufferSize returns the maximum size of the buffer.
func (*Writer) Next ¶
Next returns the next 'n' free bytes in the write buffer, flushing the writer as necessary. Next will return `io.ErrShortBuffer` if 'n' is greater than the size of the write buffer. Calls to 'next' increment the write position by the size of the returned buffer.