Skip to content

Avoid ListFields and heap allocation in proto2::internal::IsDescendant. - #1204

Draft
copybara-service[bot] wants to merge 1 commit into
masterfrom
test_988534153
Draft

copybara-service[bot] wants to merge 1 commit into
masterfrom
test_988534153

Conversation

@copybara-service

Copy link
Copy Markdown

Avoid ListFields and heap allocation in proto2::internal::IsDescendant.

IsDescendant is called twice by ABSL_DCHECK in every Message::CopyFrom and
generated CopyFrom in non-opt builds (2.15% mean / 3.10% pooled inclusive CPU
across k8-fastbuild Forge tests). Previously, IsDescendant allocated a
std::vector<const FieldDescriptor*> and called Reflection::ListFields to
collect and sort all set fields (including scalar, enum, and string fields, and
looking up extension FieldDescriptors in DescriptorPool), only to skip all
non-message fields.

Instead, short-circuit on default instances, scan descriptor fields directly
skipping non-CPPTYPE_MESSAGE fields before checking presence, and inspect
ExtensionSet entries in place via AnyOfNoPrefetch without allocating or sorting
a field vector.

IsDescendant is called twice by ABSL_DCHECK in every Message::CopyFrom and
generated CopyFrom in non-opt builds (2.15% mean / 3.10% pooled inclusive CPU
across k8-fastbuild Forge tests). Previously, IsDescendant allocated a
std::vector<const FieldDescriptor*> and called Reflection::ListFields to
collect and sort all set fields (including scalar, enum, and string fields, and
looking up extension FieldDescriptors in DescriptorPool), only to skip all
non-message fields.

Instead, short-circuit on default instances, scan descriptor fields directly
skipping non-CPPTYPE_MESSAGE fields before checking presence, and inspect
ExtensionSet entries in place via AnyOfNoPrefetch without allocating or sorting
a field vector.

PiperOrigin-RevId: 988534153

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