|
22 | 22 | #include <string> |
23 | 23 | #include <variant> |
24 | 24 |
|
| 25 | +#include "absl/base/attributes.h" |
25 | 26 | #include "absl/base/nullability.h" |
26 | 27 | #include "absl/base/optimization.h" |
27 | 28 | #include "absl/container/flat_hash_set.h" |
@@ -258,7 +259,9 @@ class UnknownTree { |
258 | 259 | UnknownTree(const UnknownTree&) = delete; |
259 | 260 | UnknownTree& operator=(const UnknownTree&) = delete; |
260 | 261 |
|
261 | | - explicit UnknownTree(google::protobuf::Arena* absl_nonnull arena) : arena_(arena) {} |
| 262 | + explicit UnknownTree(google::protobuf::Arena* absl_nonnull arena) : arena_(arena) { |
| 263 | + ABSL_DCHECK(arena != nullptr); |
| 264 | + } |
262 | 265 |
|
263 | 266 | void MaybeReset(google::protobuf::Arena* absl_nonnull arena) { |
264 | 267 | ABSL_DCHECK(arena != nullptr); |
@@ -345,21 +348,41 @@ class UnknownTree { |
345 | 348 | // address for efficiency. This function does that grafting when it is |
346 | 349 | // required. |
347 | 350 | [[nodiscard]] |
348 | | - UnknownSet Graft(const UnknownSet& in) { |
| 351 | + ABSL_ATTRIBUTE_ALWAYS_INLINE UnknownSet Graft(const UnknownSet& in) { |
349 | 352 | // Fast path, should be taken almost always. |
| 353 | + UnknownSet out; |
| 354 | + if (ABSL_PREDICT_TRUE(in.root == nullptr || in.root == RootIfPresent())) { |
| 355 | + ABSL_DCHECK(in.root != nullptr || in.attributes == nullptr); |
| 356 | + ABSL_DCHECK(in.root != nullptr || in.functions == nullptr); |
| 357 | + out = in; |
| 358 | + } else { |
| 359 | + // Slow path, should be taken extremely rarely. |
| 360 | + GraftImpl(in, out); |
| 361 | + } |
| 362 | + return out; |
| 363 | + } |
| 364 | + |
| 365 | + // In the rare event that somebody manages to pass an unknown set from one |
| 366 | + // expression into another, we need to be able to graft those nodes into our |
| 367 | + // tree. The unknown attribute set and unknown function set sort nodes by |
| 368 | + // address for efficiency. This function does that grafting when it is |
| 369 | + // required. |
| 370 | + ABSL_ATTRIBUTE_ALWAYS_INLINE void GraftAt(UnknownSet& in) { |
350 | 371 | if (ABSL_PREDICT_TRUE(in.root == nullptr || in.root == RootIfPresent())) { |
351 | | - return in; |
| 372 | + // Fast path, should be taken almost always. |
| 373 | + ABSL_DCHECK(in.root != nullptr || in.attributes == nullptr); |
| 374 | + ABSL_DCHECK(in.root != nullptr || in.functions == nullptr); |
| 375 | + } else { |
| 376 | + // Slow path, should be taken extremely rarely. |
| 377 | + GraftImpl(in, in); |
352 | 378 | } |
353 | | - // Slow path, should be taken extremely rarely. |
354 | | - return GraftImpl(in); |
355 | 379 | } |
356 | 380 |
|
357 | 381 | private: |
358 | 382 | friend class UnknownAttributeNode; |
359 | 383 | friend class UnknownRoot; |
360 | 384 |
|
361 | | - [[nodiscard]] |
362 | | - UnknownSet GraftImpl(const UnknownSet& in); |
| 385 | + ABSL_ATTRIBUTE_NOINLINE void GraftImpl(const UnknownSet& in, UnknownSet& out); |
363 | 386 |
|
364 | 387 | [[nodiscard]] |
365 | 388 | UnknownAttributeNode* GraftImpl(const UnknownAttributeNode* in); |
|
0 commit comments