summaryrefslogtreecommitdiffhomepage
path: root/pkg/lisafs/sample_message.go
diff options
context:
space:
mode:
authorAyush Ranjan <ayushranjan@google.com>2021-11-05 10:40:53 -0700
committergVisor bot <gvisor-bot@google.com>2021-11-05 10:43:49 -0700
commitce4f4283badb6b07baf9f8e6d99e7a5fd15c92db (patch)
tree848dc50da62da59dc4a5781f9eb7461c58b71512 /pkg/lisafs/sample_message.go
parent822a647018adbd994114cb0dc8932f2853b805aa (diff)
Make {Un}Marshal{Bytes/Unsafe} return remaining buffer.
Change marshal.Marshallable method signatures to return the remaining buffer. This makes it easier to implement these method manually. Without this, we would have to manually do buffer shifting which is error prone. tools/go_marshal/test:benchmark test does not show change in performance. Additionally fixed some marshalling bugs in fsimpl/fuse. Updated multiple callpoints to get rid of redundant slice indexing work and simplified code using this new signature. Updates #6450 PiperOrigin-RevId: 407857019
Diffstat (limited to 'pkg/lisafs/sample_message.go')
-rw-r--r--pkg/lisafs/sample_message.go41
1 files changed, 22 insertions, 19 deletions
diff --git a/pkg/lisafs/sample_message.go b/pkg/lisafs/sample_message.go
index 3868dfa08..745736b6d 100644
--- a/pkg/lisafs/sample_message.go
+++ b/pkg/lisafs/sample_message.go
@@ -53,18 +53,24 @@ func (m *MsgDynamic) SizeBytes() int {
}
// MarshalBytes implements marshal.Marshallable.MarshalBytes.
-func (m *MsgDynamic) MarshalBytes(dst []byte) {
- m.N.MarshalUnsafe(dst)
- dst = dst[m.N.SizeBytes():]
- MarshalUnsafeMsg1Slice(m.Arr, dst)
+func (m *MsgDynamic) MarshalBytes(dst []byte) []byte {
+ dst = m.N.MarshalUnsafe(dst)
+ n, err := MarshalUnsafeMsg1Slice(m.Arr, dst)
+ if err != nil {
+ panic(err)
+ }
+ return dst[n:]
}
// UnmarshalBytes implements marshal.Marshallable.UnmarshalBytes.
-func (m *MsgDynamic) UnmarshalBytes(src []byte) {
- m.N.UnmarshalUnsafe(src)
- src = src[m.N.SizeBytes():]
+func (m *MsgDynamic) UnmarshalBytes(src []byte) []byte {
+ src = m.N.UnmarshalUnsafe(src)
m.Arr = make([]MsgSimple, m.N)
- UnmarshalUnsafeMsg1Slice(m.Arr, src)
+ n, err := UnmarshalUnsafeMsg1Slice(m.Arr, src)
+ if err != nil {
+ panic(err)
+ }
+ return src[n:]
}
// Randomize randomizes the contents of m.
@@ -90,21 +96,18 @@ func (v *P9Version) SizeBytes() int {
}
// MarshalBytes implements marshal.Marshallable.MarshalBytes.
-func (v *P9Version) MarshalBytes(dst []byte) {
- v.MSize.MarshalUnsafe(dst)
- dst = dst[v.MSize.SizeBytes():]
+func (v *P9Version) MarshalBytes(dst []byte) []byte {
+ dst = v.MSize.MarshalUnsafe(dst)
versionLen := primitive.Uint16(len(v.Version))
- versionLen.MarshalUnsafe(dst)
- dst = dst[versionLen.SizeBytes():]
- copy(dst, v.Version)
+ dst = versionLen.MarshalUnsafe(dst)
+ return dst[copy(dst, v.Version):]
}
// UnmarshalBytes implements marshal.Marshallable.UnmarshalBytes.
-func (v *P9Version) UnmarshalBytes(src []byte) {
- v.MSize.UnmarshalUnsafe(src)
- src = src[v.MSize.SizeBytes():]
+func (v *P9Version) UnmarshalBytes(src []byte) []byte {
+ src = v.MSize.UnmarshalUnsafe(src)
var versionLen primitive.Uint16
- versionLen.UnmarshalUnsafe(src)
- src = src[versionLen.SizeBytes():]
+ src = versionLen.UnmarshalUnsafe(src)
v.Version = string(src[:versionLen])
+ return src[versionLen:]
}