message_lite.h

#include <google/protobuf/message_lite.h>
namespace google::protobuf

Defines MessageLite, the abstract interface implemented by all (lite and non-lite) protocol message objects.

Classes in this file

RepeatedPtrField is like RepeatedField, but used for repeated strings or Messages.
Interface to light weight protocol messages.

template class RepeatedPtrField

#include <google/protobuf/message_lite.h>
namespace google::protobuf

template <typename Element>

RepeatedPtrField is like RepeatedField, but used for repeated strings or Messages.

Members

typedef
internal::RepeatedPtrIterator< Element > iterator
STL-like iterator support.
typedef
internal::RepeatedPtrIterator< const Element > const_iterator
typedef
Element value_type
typedef
value_type & reference
typedef
value_type * pointer
typedef
int size_type
typedef
ptrdiff_t difference_type
typedef
std::reverse_iterator< const_iterator > const_reverse_iterator
Reverse iterator support.
typedef
std::reverse_iterator< iterator > reverse_iterator
typedef
internal::RepeatedPtrOverPtrsIterator< Element *, void * > pointer_iterator
Custom STL-like iterator that iterates over and returns the underlying pointers to Element rather than Element itself.
typedef
internal::RepeatedPtrOverPtrsIterator< const Element *const, const void *const > const_pointer_iterator
const typedef value_type &
const_reference
const typedef value_type *
const_pointer
RepeatedPtrField()
explicit
RepeatedPtrField(Arena * arena)
RepeatedPtrField(const RepeatedPtrField & other)
template
RepeatedPtrField(Iter begin, const Iter & end)
~RepeatedPtrField()
RepeatedPtrField &
operator=(const RepeatedPtrField & other)
RepeatedPtrField(RepeatedPtrField && other)
RepeatedPtrField &
operator=(RepeatedPtrField && other)
bool
empty() const
int
size() const
const Element &
Get(int index) const
Element *
Mutable(int index)
Element *
Add()
void
Add(Element && value)
const Element &
operator[](int index) const
Element &
operator[](int index)
const Element &
at(int index) const
Element &
at(int index)
void
RemoveLast()
Remove the last element in the array. more...
void
DeleteSubrange(int start, int num)
Delete elements with indices in the range [[]start . more...
void
Clear()
void
MergeFrom(const RepeatedPtrField & other)
void
CopyFrom(const RepeatedPtrField & other)
void
Reserve(int new_size)
Reserve space to expand the field to at least the given size. more...
int
Capacity() const
Element **
mutable_data()
Gets the underlying array. more...
const Element *const *
data() const
void
Swap(RepeatedPtrField * other)
Swap entire contents with "other". more...
void
UnsafeArenaSwap(RepeatedPtrField * other)
Swap entire contents with "other". more...
void
SwapElements(int index1, int index2)
Swap two elements.
iterator
begin()
const_iterator
begin() const
const_iterator
cbegin() const
iterator
end()
const_iterator
end() const
const_iterator
cend() const
reverse_iterator
rbegin()
const_reverse_iterator
rbegin() const
reverse_iterator
rend()
const_reverse_iterator
rend() const
pointer_iterator
pointer_begin()
const_pointer_iterator
pointer_begin() const
pointer_iterator
pointer_end()
const_pointer_iterator
pointer_end() const
size_t
SpaceUsedExcludingSelfLong() const
Returns (an estimate of) the number of bytes used by the repeated field, excluding sizeof(*this).
int
SpaceUsedExcludingSelf() const

Advanced memory management

When hardcore memory management becomes necessary – as it sometimes does here at Google – the following methods may be useful.
void
AddAllocated(Element * value)
Add an already-allocated object, passing ownership to the RepeatedPtrField. more...
Element *
ReleaseLast()
Remove the last element and return it, passing ownership to the caller. more...
void
UnsafeArenaAddAllocated(Element * value)
Add an already-allocated object, skipping arena-ownership checks. more...
Element *
UnsafeArenaReleaseLast()
Remove the last element and return it. more...
void
ExtractSubrange(int start, int num, Element ** elements)
Extract elements with indices in the range "[[]start .. start+num-1]". more...
void
UnsafeArenaExtractSubrange(int start, int num, Element ** elements)
Identical to ExtractSubrange() described above, except that when this repeated field is on an arena, no object copies are performed. more...
int
ClearedCount() const
Get the number of cleared objects that are currently being kept around for reuse.
void
AddCleared(Element * value)
Add an element to the pool of cleared objects, passing ownership to the RepeatedPtrField. more...
Element *
ReleaseCleared()
Remove a single element from the cleared pool and return it, passing ownership to the caller. more...
iterator
erase(const_iterator position)
Removes the element referenced by position. more...
iterator
erase(const_iterator first, const_iterator last)
Removes the elements in the range [[]first, last). more...
Arena *
GetArena() const
Gets the arena on which this RepeatedPtrField stores its elements.
void
InternalSwap(RepeatedPtrField * other)
For internal use only. more...

void RepeatedPtrField::RemoveLast()

Remove the last element in the array.

Ownership of the element is retained by the array.


void RepeatedPtrField::DeleteSubrange(
        int start,
        int num)

Delete elements with indices in the range [[]start .

. start+num-1]. Caution: implementation moves all elements with indices [[]start+num .. ]. Calling this routine inside a loop can cause quadratic behavior.


void RepeatedPtrField::Reserve(
        int new_size)

Reserve space to expand the field to at least the given size.

This only resizes the pointer array; it doesn't allocate any objects. If the array is grown, it will always be at least doubled in size.


Element ** RepeatedPtrField::mutable_data()

Gets the underlying array.

This pointer is possibly invalidated by any add or remove operation.


void RepeatedPtrField::Swap(
        RepeatedPtrField * other)

Swap entire contents with "other".

If they are on separate arenas, then copies data.


void RepeatedPtrField::UnsafeArenaSwap(
        RepeatedPtrField * other)

Swap entire contents with "other".

Caller should guarantee that either both fields are on the same arena or both are on the heap. Swapping between different arenas with this function is disallowed and is caught via GOOGLE_DCHECK.


void RepeatedPtrField::AddAllocated(
        Element * value)

Add an already-allocated object, passing ownership to the RepeatedPtrField.

Note that some special behavior occurs with respect to arenas:

(i) if this field holds submessages, the new submessage will be copied if
the original is in an arena and this RepeatedPtrField is either in a
different arena, or on the heap.
(ii) if this field holds strings, the passed-in string *must* be
heap-allocated, not arena-allocated. There is no way to dynamically check
this at runtime, so User Beware.

Element * RepeatedPtrField::ReleaseLast()

Remove the last element and return it, passing ownership to the caller.

Requires: size() > 0

If this RepeatedPtrField is on an arena, an object copy is required to pass ownership back to the user (for compatible semantics). Use UnsafeArenaReleaseLast() if this behavior is undesired.


void RepeatedPtrField::UnsafeArenaAddAllocated(
        Element * value)

Add an already-allocated object, skipping arena-ownership checks.

The user must guarantee that the given object is in the same arena as this RepeatedPtrField. It is also useful in legacy code that uses temporary ownership to avoid copies. Example:

RepeatedPtrField<T> temp_field;
temp_field.AddAllocated(new T);
... // Do something with temp_field
temp_field.ExtractSubrange(0, temp_field.size(), nullptr);

If you put temp_field on the arena this fails, because the ownership transfers to the arena at the "AddAllocated" call and is not released anymore causing a double delete. UnsafeArenaAddAllocated prevents this.


Element * RepeatedPtrField::UnsafeArenaReleaseLast()

Remove the last element and return it.

Works only when operating on an arena. The returned pointer is to the original object in the arena, hence has the arena's lifetime. Requires: current_size_ > 0


void RepeatedPtrField::ExtractSubrange(
        int start,
        int num,
        Element ** elements)

Extract elements with indices in the range "[[]start .. start+num-1]".

The caller assumes ownership of the extracted elements and is responsible for deleting them when they are no longer needed. If "elements" is non-NULL, then pointers to the extracted elements are stored in "elements[[]0 .. num-1]" for the convenience of the caller. If "elements" is NULL, then the caller must use some other mechanism to perform any further operations (like deletion) on these elements. Caution: implementation also moves elements with indices [[]start+num ..]. Calling this routine inside a loop can cause quadratic behavior.

Memory copying behavior is identical to ReleaseLast(), described above: if this RepeatedPtrField is on an arena, an object copy is performed for each returned element, so that all returned element pointers are to heap-allocated copies. If this copy is not desired, the user should call UnsafeArenaExtractSubrange().


void RepeatedPtrField::UnsafeArenaExtractSubrange(
        int start,
        int num,
        Element ** elements)

Identical to ExtractSubrange() described above, except that when this repeated field is on an arena, no object copies are performed.

Instead, the raw object pointers are returned. Thus, if on an arena, the returned objects must not be freed, because they will not be heap-allocated objects.


void RepeatedPtrField::AddCleared(
        Element * value)

Add an element to the pool of cleared objects, passing ownership to the RepeatedPtrField.

The element must be cleared prior to calling this method.

This method cannot be called when the repeated field is on an arena or when |value| is; both cases will trigger a GOOGLE_DCHECK-failure.


Element * RepeatedPtrField::ReleaseCleared()

Remove a single element from the cleared pool and return it, passing ownership to the caller.

The element is guaranteed to be cleared. Requires: ClearedCount() > 0

This method cannot be called when the repeated field is on an arena; doing so will trigger a GOOGLE_DCHECK-failure.


iterator RepeatedPtrField::erase(
        const_iterator position)

Removes the element referenced by position.

Returns an iterator to the element immediately following the removed element.

Invalidates all iterators at or after the removed element, including end().


iterator RepeatedPtrField::erase(
        const_iterator first,
        const_iterator last)

Removes the elements in the range [[]first, last).

Returns an iterator to the element immediately following the removed range.

Invalidates all iterators at or after the removed range, including end().


void RepeatedPtrField::InternalSwap(
        RepeatedPtrField * other)

For internal use only.

This is public due to it being called by generated code.

class MessageLite

#include <google/protobuf/message_lite.h>
namespace google::protobuf

Interface to light weight protocol messages.

This interface is implemented by all protocol message objects. Non-lite messages additionally implement the Message interface, which is a subclass of MessageLite. Use MessageLite instead when you only need the subset of features which it supports – namely, nothing that uses descriptors or reflection. You can instruct the protocol compiler to generate classes which implement only MessageLite, not the full Message interface, by adding the following line to the .proto file:

option optimize_for = LITE_RUNTIME;
This is particularly useful on resource-constrained systems where
the full protocol buffers runtime library is too big.

Note that on non-constrained systems (e.g. servers) when you need
to link in lots of protocol definitions, a better way to reduce
total code footprint is to use optimize_for = CODE_SIZE.  This
will make the generated code smaller while still supporting all the
same features (at the expense of speed).  optimize_for = LITE_RUNTIME
is best when you only have a small number of message types linked
into your binary, in which case the size of the protocol buffers
runtime itself is the biggest problem. 

Known subclasses:

Members

enum
ParseFlags
MessageLite()
virtual
~MessageLite()
template bool
ParseFrom(const T & input)
virtual uint8 *
InternalSerializeWithCachedSizesToArray(uint8 * ptr, io::EpsCopyOutputStream * stream) const = 0
Fast path when conditions match (ie. more...
protected template static T *
CreateMaybeMessage(Arena * arena)

Basic Operations

virtual std::string
GetTypeName() const = 0
Get the name of this message type, e.g. "foo.bar.BazProto".
virtual MessageLite *
New() const = 0
Construct a new instance of the same type. more...
virtual MessageLite *
New(Arena * arena) const
Construct a new instance on the arena. more...
virtual Arena *
GetArena() const
Get the arena, if any, associated with this message. more...
virtual void *
GetMaybeArenaPointer() const
Get a pointer that may be equal to this message's arena, or may not be. more...
virtual void
Clear() = 0
Clear all fields of the message and set them to their default values. more...
virtual bool
IsInitialized() const = 0
Quickly check if all required fields have values set.
virtual std::string
InitializationErrorString() const
This is not implemented for Lite messages – it just returns "(cannot determine missing fields for lite message)". more...
virtual void
CheckTypeAndMergeFrom(const MessageLite & other) = 0
If |other| is the exact same class as this, calls MergeFrom(). more...
std::string
DebugString() const
These methods return a human-readable summary of the message. more...
std::string
ShortDebugString() const
std::string
Utf8DebugString() const
MessageLite::DebugString is already Utf8 Safe. more...

Parsing

Methods for parsing in protocol buffer format.

Most of these are just simple wrappers around MergeFromCodedStream(). Clear() will be called before merging the input.

bool
ParseFromCodedStream(io::CodedInputStream * input)
Fill the message with a protocol buffer parsed from the given input stream. more...
bool
ParsePartialFromCodedStream(io::CodedInputStream * input)
Like ParseFromCodedStream(), but accepts messages that are missing required fields.
bool
ParseFromZeroCopyStream(io::ZeroCopyInputStream * input)
Read a protocol buffer from the given zero-copy input stream. more...
bool
ParsePartialFromZeroCopyStream(io::ZeroCopyInputStream * input)
Like ParseFromZeroCopyStream(), but accepts messages that are missing required fields.
bool
ParseFromFileDescriptor(int file_descriptor)
Parse a protocol buffer from a file descriptor. more...
bool
ParsePartialFromFileDescriptor(int file_descriptor)
Like ParseFromFileDescriptor(), but accepts messages that are missing required fields.
bool
ParseFromIstream(std::istream * input)
Parse a protocol buffer from a C++ istream. more...
bool
ParsePartialFromIstream(std::istream * input)
Like ParseFromIstream(), but accepts messages that are missing required fields.
bool
MergePartialFromBoundedZeroCopyStream(io::ZeroCopyInputStream * input, int size)
Read a protocol buffer from the given zero-copy input stream, expecting the message to be exactly "size" bytes long. more...
bool
MergeFromBoundedZeroCopyStream(io::ZeroCopyInputStream * input, int size)
Like ParseFromBoundedZeroCopyStream(), but accepts messages that are missing required fields.
bool
ParseFromBoundedZeroCopyStream(io::ZeroCopyInputStream * input, int size)
bool
ParsePartialFromBoundedZeroCopyStream(io::ZeroCopyInputStream * input, int size)
Like ParseFromBoundedZeroCopyStream(), but accepts messages that are missing required fields.
bool
ParseFromString(const std::string & data)
Parses a protocol buffer contained in a string. more...
bool
ParsePartialFromString(const std::string & data)
Like ParseFromString(), but accepts messages that are missing required fields.
bool
ParseFromArray(const void * data, int size)
Parse a protocol buffer contained in an array of bytes.
bool
ParsePartialFromArray(const void * data, int size)
Like ParseFromArray(), but accepts messages that are missing required fields.
bool
MergeFromCodedStream(io::CodedInputStream * input)
Reads a protocol buffer from the stream and merges it into this Message. more...
bool
MergePartialFromCodedStream(io::CodedInputStream * input)
Like MergeFromCodedStream(), but succeeds even if required fields are missing in the input. more...
bool
MergeFromString(const std::string & data)
Merge a protocol buffer contained in a string.

Serialization

Methods for serializing in protocol buffer format.

Most of these are just simple wrappers around ByteSize() and SerializeWithCachedSizes().

bool
SerializeToCodedStream(io::CodedOutputStream * output) const
Write a protocol buffer of this message to the given output. more...
bool
SerializePartialToCodedStream(io::CodedOutputStream * output) const
Like SerializeToCodedStream(), but allows missing required fields.
bool
SerializeToZeroCopyStream(io::ZeroCopyOutputStream * output) const
Write the message to the given zero-copy output stream. more...
bool
SerializePartialToZeroCopyStream(io::ZeroCopyOutputStream * output) const
Like SerializeToZeroCopyStream(), but allows missing required fields.
bool
SerializeToString(std::string * output) const
Serialize the message and store it in the given string. more...
bool
SerializePartialToString(std::string * output) const
Like SerializeToString(), but allows missing required fields.
bool
SerializeToArray(void * data, int size) const
Serialize the message and store it in the given byte array. more...
bool
SerializePartialToArray(void * data, int size) const
Like SerializeToArray(), but allows missing required fields.
std::string
SerializeAsString() const
Make a string encoding the message. more...
std::string
SerializePartialAsString() const
Like SerializeAsString(), but allows missing required fields.
bool
SerializeToFileDescriptor(int file_descriptor) const
Serialize the message and write it to the given file descriptor. more...
bool
SerializePartialToFileDescriptor(int file_descriptor) const
Like SerializeToFileDescriptor(), but allows missing required fields.
bool
SerializeToOstream(std::ostream * output) const
Serialize the message and write it to the given C++ ostream. more...
bool
SerializePartialToOstream(std::ostream * output) const
Like SerializeToOstream(), but allows missing required fields.
bool
AppendToString(std::string * output) const
Like SerializeToString(), but appends to the data to the string's existing contents. more...
bool
AppendPartialToString(std::string * output) const
Like AppendToString(), but allows missing required fields.
virtual size_t
ByteSizeLong() const = 0
Computes the serialized size of the message. more...
int
ByteSize() const
Legacy ByteSize() API.
void
SerializeWithCachedSizes(io::CodedOutputStream * output) const
Serializes the message without recomputing the size. more...
uint8 *
SerializeWithCachedSizesToArray(uint8 * target) const
Like SerializeWithCachedSizes, but writes directly to *target, returning a pointer to the byte immediately after the last byte written. more...
virtual int
GetCachedSize() const = 0
Returns the result of the last call to ByteSize(). more...
virtual const char *
_InternalParse(const char * , internal::ParseContext * )

enum MessageLite::ParseFlags {
  kMerge = = 0,
  kParse = = 1,
  kMergePartial = = 2,
  kParsePartial = = 3,
  kMergeWithAliasing = = 4,
  kParseWithAliasing = = 5,
  kMergePartialWithAliasing = = 6,
  kParsePartialWithAliasing = = 7
}

kMerge
kParse
kMergePartial
kParsePartial
kMergeWithAliasing
kParseWithAliasing
kMergePartialWithAliasing
kParsePartialWithAliasing

virtual uint8 * MessageLite::InternalSerializeWithCachedSizesToArray(
        uint8 * ptr,
        io::EpsCopyOutputStream * stream) const = 0

Fast path when conditions match (ie.

non-deterministic) uint8* InternalSerializeWithCachedSizesToArray(uint8* ptr) const;


virtual MessageLite * MessageLite::New() const = 0

Construct a new instance of the same type.

Ownership is passed to the caller.


virtual MessageLite * MessageLite::New(
        Arena * arena) const

Construct a new instance on the arena.

Ownership is passed to the caller if arena is a NULL. Default implementation for backwards compatibility.


virtual Arena * MessageLite::GetArena() const

Get the arena, if any, associated with this message.

Virtual method required for generic operations but most arena-related operations should use the GetArenaNoVirtual() generated-code method. Default implementation to reduce code size by avoiding the need for per-type implementations when types do not implement arena support.


virtual void * MessageLite::GetMaybeArenaPointer() const

Get a pointer that may be equal to this message's arena, or may not be.

If the value returned by this method is equal to some arena pointer, then this message is on that arena; however, if this message is on some arena, this method may or may not return that arena's pointer. As a tradeoff, this method may be more efficient than GetArena(). The intent is to allow underlying representations that use e.g. tagged pointers to sometimes store the arena pointer directly, and sometimes in a more indirect way, and allow a fastpath comparison against the arena pointer when it's easy to obtain.


virtual void MessageLite::Clear() = 0

Clear all fields of the message and set them to their default values.

Clear() avoids freeing memory, assuming that any memory allocated to hold parts of the message will be needed again to hold the next message. If you actually want to free the memory used by a Message, you must delete it.


virtual std::string MessageLite::InitializationErrorString() const

This is not implemented for Lite messages – it just returns "(cannot determine missing fields for lite message)".

However, it is implemented for full messages. See message.h.


virtual void MessageLite::CheckTypeAndMergeFrom(
        const MessageLite & other) = 0

If |other| is the exact same class as this, calls MergeFrom().

Otherwise, results are undefined (probably crash).


std::string MessageLite::DebugString() const

These methods return a human-readable summary of the message.

Note that since the MessageLite interface does not support reflection, there is very little information that these methods can provide. They are shadowed by methods of the same name on the Message interface which provide much more information. The methods here are intended primarily to facilitate code reuse for logic that needs to interoperate with both full and lite protos.

The format of the returned string is subject to change, so please do not assume it will remain stable over time.


std::string MessageLite::Utf8DebugString() const

MessageLite::DebugString is already Utf8 Safe.

This is to add compatibility with Message.


bool MessageLite::ParseFromCodedStream(
        io::CodedInputStream * input)

Fill the message with a protocol buffer parsed from the given input stream.

Returns false on a read error or if the input is in the wrong format. A successful return does not indicate the entire input is consumed, ensure you call ConsumedEntireMessage() to check that if applicable.


bool MessageLite::ParseFromZeroCopyStream(
        io::ZeroCopyInputStream * input)

Read a protocol buffer from the given zero-copy input stream.

If successful, the entire input will be consumed.


bool MessageLite::ParseFromFileDescriptor(
        int file_descriptor)

Parse a protocol buffer from a file descriptor.

If successful, the entire input will be consumed.


bool MessageLite::ParseFromIstream(
        std::istream * input)

Parse a protocol buffer from a C++ istream.

If successful, the entire input will be consumed.


bool MessageLite::MergePartialFromBoundedZeroCopyStream(
        io::ZeroCopyInputStream * input,
        int size)

Read a protocol buffer from the given zero-copy input stream, expecting the message to be exactly "size" bytes long.

If successful, exactly this many bytes will have been consumed from the input.


bool MessageLite::ParseFromString(
        const std::string & data)

Parses a protocol buffer contained in a string.

Returns true on success. This function takes a string in the (non-human-readable) binary wire format, matching the encoding output by MessageLite::SerializeToString(). If you'd like to convert a human-readable string into a protocol buffer object, see google::protobuf::TextFormat::ParseFromString().


bool MessageLite::MergeFromCodedStream(
        io::CodedInputStream * input)

Reads a protocol buffer from the stream and merges it into this Message.

Singular fields read from the what is already in the Message and repeated fields are appended to those already present.

It is the responsibility of the caller to call input->LastTagWas() (for groups) or input->ConsumedEntireMessage() (for non-groups) after this returns to verify that the message's end was delimited correctly.

ParseFromCodedStream() is implemented as Clear() followed by MergeFromCodedStream().


bool MessageLite::MergePartialFromCodedStream(
        io::CodedInputStream * input)

Like MergeFromCodedStream(), but succeeds even if required fields are missing in the input.

MergeFromCodedStream() is just implemented as MergePartialFromCodedStream() followed by IsInitialized().


bool MessageLite::SerializeToCodedStream(
        io::CodedOutputStream * output) const

Write a protocol buffer of this message to the given output.

Returns false on a write error. If the message is missing required fields, this may GOOGLE_CHECK-fail.


bool MessageLite::SerializeToZeroCopyStream(
        io::ZeroCopyOutputStream * output) const

Write the message to the given zero-copy output stream.

All required fields must be set.


bool MessageLite::SerializeToString(
        std::string * output) const

Serialize the message and store it in the given string.

All required fields must be set.


bool MessageLite::SerializeToArray(
        void * data,
        int size) const

Serialize the message and store it in the given byte array.

All required fields must be set.


std::string MessageLite::SerializeAsString() const

Make a string encoding the message.

Is equivalent to calling SerializeToString() on a string and using that. Returns the empty string if SerializeToString() would have returned an error. Note: If you intend to generate many such strings, you may reduce heap fragmentation by instead re-using the same string object with calls to SerializeToString().


bool MessageLite::SerializeToFileDescriptor(
        int file_descriptor) const

Serialize the message and write it to the given file descriptor.

All required fields must be set.


bool MessageLite::SerializeToOstream(
        std::ostream * output) const

Serialize the message and write it to the given C++ ostream.

All required fields must be set.


bool MessageLite::AppendToString(
        std::string * output) const

Like SerializeToString(), but appends to the data to the string's existing contents.

All required fields must be set.


virtual size_t MessageLite::ByteSizeLong() const = 0

Computes the serialized size of the message.

This recursively calls ByteSizeLong() on all embedded messages.

ByteSizeLong() is generally linear in the number of fields defined for the proto.


void MessageLite::SerializeWithCachedSizes(
        io::CodedOutputStream * output) const

Serializes the message without recomputing the size.

The message must not have changed since the last call to ByteSize(), and the value returned by ByteSize must be non-negative. Otherwise the results are undefined.


uint8 * MessageLite::SerializeWithCachedSizesToArray(
        uint8 * target) const

Like SerializeWithCachedSizes, but writes directly to *target, returning a pointer to the byte immediately after the last byte written.

"target" must point at a byte array of at least ByteSize() bytes. Whether to use deterministic serialization, e.g., maps in sorted order, is determined by CodedOutputStream::IsDefaultSerializationDeterministic().


virtual int MessageLite::GetCachedSize() const = 0

Returns the result of the last call to ByteSize().

An embedded message's size is needed both to serialize it (because embedded messages are length-delimited) and to compute the outer message's size. Caching the size avoids computing it multiple times.

ByteSize() does not automatically use the cached size when available because this would require invalidating it every time the message was modified, which would be too hard and expensive. (E.g. if a deeply-nested sub-message is changed, all of its parents' cached sizes would need to be invalidated, which is too much work for an otherwise inlined setter method.)