Skip to content

Commit

Permalink
Make byteWriter smaller
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Jan 11, 2020
1 parent 7ff946b commit 7497e46
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 20 deletions.
36 changes: 16 additions & 20 deletions encode.go
Expand Up @@ -12,34 +12,28 @@ import (
type writer interface {
io.Writer
WriteByte(byte) error
WriteString(string) (int, error)
}

type byteWriter struct {
io.Writer

buf []byte
bootstrap [64]byte
buf [1]byte
}

func newByteWriter(w io.Writer) *byteWriter {
bw := &byteWriter{
Writer: w,
}
bw.buf = bw.bootstrap[:]
bw := new(byteWriter)
bw.Reset(w)
return bw
}

func (w *byteWriter) WriteByte(c byte) error {
w.buf = w.buf[:1]
w.buf[0] = c
_, err := w.Write(w.buf)
return err
func (bw *byteWriter) Reset(w io.Writer) {
bw.Writer = w
}

func (w *byteWriter) WriteString(s string) (int, error) {
w.buf = append(w.buf[:0], s...)
return w.Write(w.buf)
func (bw *byteWriter) WriteByte(c byte) error {
bw.buf[0] = c
_, err := bw.Write(bw.buf[:])
return err
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -80,11 +74,13 @@ func NewEncoder(w io.Writer) *Encoder {
}

func (e *Encoder) Reset(w io.Writer) {
bw, ok := w.(writer)
if !ok {
bw = newByteWriter(w)
if v, ok := w.(writer); ok {
e.w = v
} else if bw, ok := e.w.(*byteWriter); ok {
bw.Reset(w)
} else {
e.w = newByteWriter(w)
}
e.w = bw

for k := range e.intern {
delete(e.intern, k)
Expand Down Expand Up @@ -185,6 +181,6 @@ func (e *Encoder) write(b []byte) error {
}

func (e *Encoder) writeString(s string) error {
_, err := e.w.WriteString(s)
_, err := e.w.Write(stringToBytes(s))
return err
}
13 changes: 13 additions & 0 deletions safe.go
@@ -0,0 +1,13 @@
// +build appengine

package msgpack

// bytesToString converts byte slice to string.
func bytesToString(b []byte) string {
return string(b)
}

// stringToBytes converts string to byte slice.
func stringToBytes(s string) []byte {
return []byte(s)
}
22 changes: 22 additions & 0 deletions unsafe.go
@@ -0,0 +1,22 @@
// +build !appengine

package msgpack

import (
"unsafe"
)

// bytesToString converts byte slice to string.
func bytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}

// stringToBytes converts string to byte slice.
func stringToBytes(s string) []byte {
return *(*[]byte)(unsafe.Pointer(
&struct {
string
Cap int
}{s, len(s)},
))
}

0 comments on commit 7497e46

Please sign in to comment.