Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add option to enable span cache and disable it by default #25

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading