Skip to content

Commit 2d9cbdc

Browse files
Add support for moving lazy fields in static reflection message movers.
PiperOrigin-RevId: 844643215
1 parent 4459a20 commit 2d9cbdc

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/google/protobuf/extension_set.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,14 @@ class PROTOBUF_EXPORT ExtensionSet {
639639
return extension != nullptr && extension->is_lazy;
640640
}
641641

642+
// Returns a pointer to the LazyField for the given extension number, or
643+
// nullptr if the extension is not lazy.
644+
// If the extension does not exist, it is created as a lazy extension.
645+
// This function returns nullptr if lazy parsing is not supported, if the
646+
// extension exists but is not lazy, or if the extension is not a message
647+
// type.
648+
LazyField* TryGetLazyField(Arena* arena, int number, FieldType type);
649+
642650
private:
643651
template <typename Type>
644652
friend class PrimitiveTypeTraits;
@@ -742,6 +750,8 @@ class PROTOBUF_EXPORT ExtensionSet {
742750
io::EpsCopyOutputStream* stream) const = 0;
743751

744752

753+
virtual LazyField* GetUnderlyingField() = 0;
754+
745755
private:
746756
virtual void UnusedKeyMethod(); // Dummy key method to avoid weak vtable.
747757
};
@@ -1868,6 +1878,19 @@ class ExtensionIdentifier {
18681878
typename TypeTraits::InitType default_value_;
18691879
};
18701880

1881+
template <typename ExtendeeType, typename TypeTraitsType,
1882+
internal::FieldType field_type, bool is_packed>
1883+
auto TryGetLazyMessageFromExtensionSet(
1884+
Arena* arena,
1885+
const google::protobuf::internal::ExtensionIdentifier<
1886+
ExtendeeType, TypeTraitsType, field_type, is_packed>& extension,
1887+
ExtensionSet& set) {
1888+
static_assert(std::is_base_of_v<
1889+
MessageLite,
1890+
std::decay_t<typename TypeTraitsType::Singular::ConstType>>);
1891+
return set.TryGetLazyField(arena, extension.number(), field_type);
1892+
}
1893+
18711894
// -------------------------------------------------------------------
18721895
// Generated accessors
18731896

src/google/protobuf/extension_set_unittest.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,6 +1808,7 @@ TEST(ExtensionSetTest, MoveExtensionWithDynamicDescriptor) {
18081808
}
18091809

18101810

1811+
18111812
TEST_P(FindExtensionTest,
18121813
FindExtensionInfoFromFieldNumber_FindExistingExtension) {
18131814
ExtensionInfo extension_info;

src/google/protobuf/generated_message_util.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,19 @@ constexpr absl::optional<uintptr_t> EncodePlacementArenaOffsets(
401401
// message classes without making them public. This is useful for highly
402402
// optimized code paths that need to access internals.
403403
struct PrivateAccess {
404+
template <typename T, int number>
405+
static constexpr bool IsLazyField() {
406+
constexpr auto l =
407+
[](auto& msg) -> decltype(msg._lazy_internal_mutable(
408+
std::integral_constant<int, number>{})) {};
409+
return std::is_invocable_v<decltype(l), T&>;
410+
}
411+
412+
template <int number, typename T>
413+
static auto& MutableLazy(T& msg) {
414+
return msg._lazy_internal_mutable(std::integral_constant<int, number>{});
415+
}
416+
404417
template <typename T>
405418
static auto& GetExtensionSet(T& msg) {
406419
return msg._impl_._extensions_;

0 commit comments

Comments
 (0)