Implementation:Alibaba MNN Protobuf Unknown Field Set CC
Metadata
| Attribute | Value |
|---|---|
| Implementation File | 3rd_party/protobuf/src/google/protobuf/unknown_field_set.cc
|
| Line Count | 334 |
| Domains | Serialization, Forward_Compatibility |
| Key Class | UnknownFieldSet
|
| Created | 2026-02-10 |
| Knowledge Sources | Repo, Doc |
Overview
Unknown field preservation implementation. Contains the runtime logic for storing and re-serializing fields that are not recognized by the current message schema, enabling forward compatibility.
Usage note: Vendored dependency used internally by MNN for parsing protobuf-based model formats. Not directly imported by end users.
Description
This source file implements the methods of UnknownFieldSet and UnknownField declared in unknown_field_set.h. When a protobuf parser encounters a field number that is not defined in the message's schema (e.g., because the schema is from an older version), the field data is preserved in an UnknownFieldSet rather than being discarded.
Key implemented operations:
- Adding unknown fields --
AddVarint,AddFixed32,AddFixed64,AddLengthDelimited,AddGroup-- each stores the field number and raw data. - Merging --
MergeFromcombines unknown fields from two sets, used duringMessage::MergeFrom. - Serialization --
SerializeToString,SerializeToCodedStreamre-emit the preserved fields in wire format, maintaining round-trip fidelity. - Byte size calculation --
SpaceUsedExcludingSelfand serialized size computation for accurate size prefixing. - Clear and delete -- Memory management for the stored field data, including deep cleanup of nested groups.
Usage
#include "google/protobuf/unknown_field_set.h"
Used internally by the protobuf runtime during parsing and serialization. Accessed via Reflection::GetUnknownFields().
Code Reference
// Merge unknown fields from another set.
void UnknownFieldSet::MergeFrom(const UnknownFieldSet& other) {
for (int i = 0; i < other.field_count(); ++i) {
const UnknownField& field = other.field(i);
switch (field.type()) {
case UnknownField::TYPE_VARINT:
AddVarint(field.number(), field.varint());
break;
case UnknownField::TYPE_FIXED32:
AddFixed32(field.number(), field.fixed32());
break;
case UnknownField::TYPE_FIXED64:
AddFixed64(field.number(), field.fixed64());
break;
case UnknownField::TYPE_LENGTH_DELIMITED:
AddLengthDelimited(field.number(), field.length_delimited());
break;
case UnknownField::TYPE_GROUP:
AddGroup(field.number())->MergeFrom(field.group());
break;
}
}
}
// Serialize all unknown fields to a coded output stream.
void UnknownFieldSet::SerializeToCodedStream(
io::CodedOutputStream* output) const {
for (int i = 0; i < field_count(); ++i) {
fields_[i].SerializeTo(output);
}
}
I/O Contract
| Direction | Type | Description |
|---|---|---|
| Input | Wire-format field data | Unrecognized fields captured during parsing |
| Output | Wire-format bytes | Preserved fields re-serialized during output |
| Storage | std::vector<UnknownField> |
In-memory representation of unknown field data |
Usage Examples
// Accessing unknown fields via reflection
const UnknownFieldSet& unknowns =
msg.GetReflection()->GetUnknownFields(msg);
for (int i = 0; i < unknowns.field_count(); ++i) {
const UnknownField& f = unknowns.field(i);
std::cout << "Unknown field #" << f.number()
<< " type=" << f.type() << std::endl;
}