Skip to content

Commit

Permalink
fix: add option to enable span cache and disable it by default (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppzqh authored Aug 19, 2024
1 parent dda1854 commit 172d004
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions fastpb_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,16 @@ var Impl impl
const speculativeLength = 1

var (
_ Protocol = impl{}
spanCache = span.NewSpanCache(1024 * 1024) // 1MB
_ Protocol = impl{}
spanCache = span.NewSpanCache(1024 * 1024) // 1MB
spanCacheEnable bool = false
)

// SetSpanCache enable/disable binary protocol bytes/string allocator
func SetSpanCache(enable bool) {
spanCacheEnable = enable
}

type impl struct{}

// WriteMessage implements TLV(tag, length, value) and V(value).
Expand Down Expand Up @@ -417,7 +423,11 @@ func (b impl) ReadString(buf []byte, _type int8) (value string, n int, err error
if EnforceUTF8() && !utf8.Valid(v) {
return value, 0, errInvalidUTF8
}
value = SliceByteToString(spanCache.Copy(v))
if spanCacheEnable {
value = SliceByteToString(spanCache.Copy(v))
} else {
value = string(v)
}
return value, n, nil
}

Expand All @@ -431,7 +441,12 @@ func (b impl) ReadBytes(buf []byte, _type int8) (value []byte, n int, err error)
if n < 0 {
return value, 0, errDecode
}
value = spanCache.Copy(v)
if spanCacheEnable {
value = spanCache.Copy(v)
} else {
value = make([]byte, len(v))
copy(value, v)
}
return value, n, nil
}

Expand Down

0 comments on commit 172d004

Please sign in to comment.