Skip to content

Keep Swift unknown fields behind one pointer - #3823

Open
oldergod wants to merge 1 commit into
masterfrom
bquenaudon.2026-11-24.swift-unknown-fields-size
Open

oldergod wants to merge 1 commit into
masterfrom
bquenaudon.2026-11-24.swift-unknown-fields-size

Conversation

@oldergod

Copy link
Copy Markdown
Member

This makes UnknownFields one pointer in size again.
#3813 stored all unknown fields in one Data value, which made UnknownFields 16 bytes and ExtensibleUnknownFields 24 bytes.
Every generated message holds one of these, and nested messages are stored inline, so large messages grew a lot.
In debug builds, code that copies large messages could overflow the 512 KB stack of a Swift concurrency thread.

UnknownFields now holds an optional reference to a final class box that owns the Data.
The reference is nil when there are no fields, so a message without unknown fields does not allocate and isEmpty is a nil check.
Copies share the box.
A mutation reuses the box when isKnownUniquelyReferenced is true, and makes a new box otherwise.
UnknownFields is now 8 bytes and ExtensibleUnknownFields is 16 bytes.

The public API does not change.
One buffer, tag order, fieldsByTag, Equatable, Hashable, Codable, the debug assertion and the encoded bytes behave as in #3813.
Decoding a message without unknown fields does not allocate more than before.
Decoding a message with unknown fields allocates one more small object for the box.
The reader still builds the bytes in its own buffer, so appending during decode does not copy the data for each field.

Release note: Swift UnknownFields is now 8 bytes and ExtensibleUnknownFields is 16 bytes, as they were before 8.0.0-alpha01, so generated Swift messages no longer grow.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant