internal/impl: add runtime support for *[]byte unknown representation
This CL adds runtime support for unknown fields to be represented
as *[]byte in addition to the current representation as []byte.
This CL does not change generated code to use *[]byte.
Comparison between using *[]byte and []byte:
• Every message supports unknown fields, so use of []byte
expands a message size by 24B (for 64-bit systems).
In contrast, *[]byte only expands a message by 8B.
This has significant memory implications for small messages.
• If unknown fields are encountered, *[]byte has extra overhead
allocating the 24B slice header. However, it is assumed
that messages rarely see any unknown fields at runtime,
or generally do so for a temporary period of time.
Change-Id: I81935e4ea7394166e61ff4579f76f59fa792dfc9
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/244937
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/internal/impl/merge.go b/internal/impl/merge.go
index cdc4267..c65bbc0 100644
--- a/internal/impl/merge.go
+++ b/internal/impl/merge.go
@@ -77,9 +77,9 @@
}
}
if mi.unknownOffset.IsValid() {
- du := dst.Apply(mi.unknownOffset).Bytes()
- su := src.Apply(mi.unknownOffset).Bytes()
- if len(*su) > 0 {
+ su := mi.getUnknownBytes(src)
+ if su != nil && len(*su) > 0 {
+ du := mi.mutableUnknownBytes(dst)
*du = append(*du, *su...)
}
}