Skip to content

Commit de7234c

Browse files
sbenzaquencopybara-github
authored andcommitted
Improve the way we mask the bits for _internal_metadata_.
For the arena, no need to mask anything. For the container, we use arithmetic instead of masks. The arithmetic is smaller and can be merged with another operation. PiperOrigin-RevId: 847830188
1 parent 530bd14 commit de7234c

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/google/protobuf/metadata_lite.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <string>
1212

1313
#include "absl/base/optimization.h"
14+
#include "absl/log/absl_check.h"
1415
#include "google/protobuf/arena.h"
1516
#include "google/protobuf/port.h"
1617

@@ -145,7 +146,19 @@ class PROTOBUF_EXPORT InternalMetadata {
145146

146147
template <typename U>
147148
U* PtrValue() const {
148-
return reinterpret_cast<U*>(ptr_ & kPtrValueMask);
149+
if constexpr (std::is_same_v<U, Arena>) {
150+
// No mask to remove.
151+
ABSL_DCHECK_EQ(ptr_ & kPtrTagMask, 0);
152+
return reinterpret_cast<U*>(ptr_);
153+
} else {
154+
static_assert(kPtrTagMask == 1);
155+
ABSL_DCHECK_EQ(ptr_ & kPtrTagMask, kPtrTagMask);
156+
// We can remove the mask via -1, which is smaller asm and can be merged
157+
// with other arithmetic operations.
158+
// Eg PtrValue<Container>()->unknown_fields can merge the offset into the
159+
// mask removal.
160+
return reinterpret_cast<U*>(ptr_ - kPtrTagMask);
161+
}
149162
}
150163

151164
// If ptr_'s tag is kTagContainer, it points to an instance of this struct.

0 commit comments

Comments
 (0)