Skip to content

Commit

Permalink
resp2: get rid of all usage of bytesutil.MultiWrite
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Picciano committed Feb 21, 2019
1 parent c0a517d commit 3af04da
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 33 deletions.
13 changes: 0 additions & 13 deletions internal/bytesutil/bytesutil.go
Expand Up @@ -206,19 +206,6 @@ func ReadNDiscard(r io.Reader, n int) error {
}
}

// MultiWrite writes multiple byte slices into one writer.
//
// This is equivalent to calling w.Write for each byte slice in bb, but may be optimized to reduce calls
// for some types of io.Writer.
func MultiWrite(w io.Writer, bb ...[]byte) error {
for _, b := range bb {
if _, err := w.Write(b); err != nil {
return err
}
}
return nil
}

// ReadInt reads the next n bytes from r as a signed 64 bit integer.
func ReadInt(r io.Reader, n int) (int64, error) {
scratch := GetBytes()
Expand Down
63 changes: 43 additions & 20 deletions resp/resp2/resp.go
Expand Up @@ -50,7 +50,13 @@ type SimpleString struct {

// MarshalRESP implements the Marshaler method
func (ss SimpleString) MarshalRESP(w io.Writer) error {
return bytesutil.MultiWrite(w, simpleStrPrefix, []byte(ss.S), delim)
scratch := bytesutil.GetBytes()
*scratch = append(*scratch, simpleStrPrefix...)
*scratch = append(*scratch, ss.S...)
*scratch = append(*scratch, delim...)
_, err := w.Write(*scratch)
bytesutil.PutBytes(scratch)
return err
}

// UnmarshalRESP implements the Unmarshaler method
Expand Down Expand Up @@ -83,13 +89,15 @@ func (e Error) Error() string {

// MarshalRESP implements the Marshaler method
func (e Error) MarshalRESP(w io.Writer) error {
if e.E == nil {
return bytesutil.MultiWrite(w, errPrefix, delim)
}
scratch := bytesutil.GetBytes()
defer bytesutil.PutBytes(scratch)
*scratch = append(*scratch, e.E.Error()...)
return bytesutil.MultiWrite(w, errPrefix, *scratch, delim)
*scratch = append(*scratch, errPrefix...)
if e.E != nil {
*scratch = append(*scratch, e.E.Error()...)
}
*scratch = append(*scratch, delim...)
_, err := w.Write(*scratch)
bytesutil.PutBytes(scratch)
return err
}

// UnmarshalRESP implements the Unmarshaler method
Expand All @@ -112,9 +120,12 @@ type Int struct {
// MarshalRESP implements the Marshaler method
func (i Int) MarshalRESP(w io.Writer) error {
scratch := bytesutil.GetBytes()
defer bytesutil.PutBytes(scratch)
*scratch = append(*scratch, intPrefix...)
*scratch = strconv.AppendInt(*scratch, int64(i.I), 10)
return bytesutil.MultiWrite(w, intPrefix, *scratch, delim)
*scratch = append(*scratch, delim...)
_, err := w.Write(*scratch)
bytesutil.PutBytes(scratch)
return err
}

// UnmarshalRESP implements the Unmarshaler method
Expand Down Expand Up @@ -245,19 +256,27 @@ type BulkReader struct {
// MarshalRESP implements the Marshaler method
func (b BulkReader) MarshalRESP(w io.Writer) error {
if b.LR == nil {
return bytesutil.MultiWrite(w, nilBulkString)
_, err := w.Write(nilBulkString)
return err
}
scratch := bytesutil.GetBytes()
defer bytesutil.PutBytes(scratch)

l := b.LR.Len()
scratch := bytesutil.GetBytes()
*scratch = append(*scratch, bulkStrPrefix...)
*scratch = strconv.AppendInt(*scratch, l, 10)
if err := bytesutil.MultiWrite(w, bulkStrPrefix, *scratch, delim); err != nil {
*scratch = append(*scratch, delim...)
_, err := w.Write(*scratch)
bytesutil.PutBytes(scratch)
if err != nil {
return err
}

if _, err := io.CopyN(w, b.LR, l); err != nil {
return err
} else if _, err := w.Write(delim); err != nil {
return err
}
return bytesutil.MultiWrite(w, delim)
return nil
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -536,7 +555,8 @@ func (a Any) MarshalRESP(w io.Writer) error {
switch vv.Kind() {
case reflect.Slice, reflect.Array:
if vv.IsNil() && !a.MarshalNoArrayHeaders {
return bytesutil.MultiWrite(w, nilArray)
_, err := w.Write(nilArray)
return err
}
l := vv.Len()
arrHeader(l)
Expand All @@ -546,7 +566,8 @@ func (a Any) MarshalRESP(w io.Writer) error {

case reflect.Map:
if vv.IsNil() && !a.MarshalNoArrayHeaders {
return bytesutil.MultiWrite(w, nilArray)
_, err := w.Write(nilArray)
return err
}
kkv := vv.MapKeys()
arrHeader(len(kkv) * 2)
Expand Down Expand Up @@ -691,9 +712,10 @@ func (a Any) UnmarshalRESP(br *bufio.Reader) error {
return err
case simpleStrPrefix[0], intPrefix[0]:
reader := byteReaderPool.Get().(*bytes.Reader)
defer byteReaderPool.Put(reader)
reader.Reset(b)
return a.unmarshalSingle(reader, reader.Len())
err := a.unmarshalSingle(reader, reader.Len())
byteReaderPool.Put(reader)
return err
default:
return fmt.Errorf("unknown type prefix %q", b[0])
}
Expand Down Expand Up @@ -1050,9 +1072,10 @@ func (rm *RawMessage) unmarshal(br *bufio.Reader) error {
// all cases.
func (rm RawMessage) UnmarshalInto(u resp.Unmarshaler) error {
r := byteReaderPool.Get().(*bytes.Reader)
defer byteReaderPool.Put(r)
r.Reset(rm)
return u.UnmarshalRESP(bufio.NewReader(r))
err := u.UnmarshalRESP(bufio.NewReader(r))
byteReaderPool.Put(r)
return err
}

// IsNil returns true if the contents of RawMessage are one of the nil values.
Expand Down

0 comments on commit 3af04da

Please sign in to comment.