Skip to content

Commit 3a458fe

Browse files
jnthntatumcopybara-github
authored andcommitted
Add Unsafe factories for string values.
BytesValue::WrapUnsafe and StringValue::WrapUnsafe do not attempt to validate ownership, but avoid copying the underlying string data as much as possible. PiperOrigin-RevId: 828657464
1 parent db7d658 commit 3a458fe

14 files changed

+1160
-87
lines changed

common/internal/byte_string.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,19 @@ ByteString::ByteString(const ReferenceCount* absl_nonnull refcount,
174174
kMetadataOwnerReferenceCountBit);
175175
}
176176

177+
ByteString::ByteString(ByteString::ExternalStringTag,
178+
absl::string_view string) {
179+
if (string.size() <= kSmallByteStringCapacity) {
180+
SetSmall(nullptr, string);
181+
} else {
182+
SetExternalMedium(string);
183+
}
184+
}
185+
186+
ByteString ByteString::FromExternal(absl::string_view string) {
187+
return ByteString(ExternalStringTag{}, string);
188+
}
189+
177190
google::protobuf::Arena* absl_nullable ByteString::GetArena() const {
178191
switch (GetKind()) {
179192
case ByteStringKind::kSmall:
@@ -965,6 +978,14 @@ void ByteString::SetMedium(google::protobuf::Arena* absl_nullable arena,
965978
}
966979
}
967980

981+
void ByteString::SetExternalMedium(absl::string_view string) {
982+
ABSL_DCHECK_GT(string.size(), kSmallByteStringCapacity);
983+
rep_.header.kind = ByteStringKind::kMedium;
984+
rep_.medium.size = string.size();
985+
rep_.medium.data = string.data();
986+
rep_.medium.owner = 0;
987+
}
988+
968989
void ByteString::SetMedium(google::protobuf::Arena* absl_nullable arena,
969990
std::string&& string) {
970991
ABSL_DCHECK_GT(string.size(), kSmallByteStringCapacity);

common/internal/byte_string.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ ByteString final {
232232
const absl::Cord& cord ABSL_ATTRIBUTE_LIFETIME_BOUND)
233233
: ByteString(Borrowed(borrower, cord)) {}
234234

235+
// Creates a medium byte string that is backed by an external string. Should
236+
// only be called from explicit 'Unsafe' factories.
237+
static ByteString FromExternal(absl::string_view string);
238+
235239
~ByteString() { Destroy(); }
236240

237241
ByteString& operator=(const ByteString& other) noexcept {
@@ -358,6 +362,8 @@ ByteString final {
358362
google::protobuf::Arena* absl_nonnull arena);
359363
friend struct cel::ArenaTraits<ByteString>;
360364

365+
struct ExternalStringTag {};
366+
361367
static ByteString Borrowed(Borrower borrower,
362368
absl::string_view string
363369
ABSL_ATTRIBUTE_LIFETIME_BOUND);
@@ -368,6 +374,8 @@ ByteString final {
368374
ByteString(const ReferenceCount* absl_nonnull refcount,
369375
absl::string_view string);
370376

377+
ByteString(ExternalStringTag, absl::string_view string);
378+
371379
constexpr ByteStringKind GetKind() const { return rep_.header.kind; }
372380

373381
absl::string_view GetSmall() const {
@@ -451,6 +459,10 @@ ByteString final {
451459

452460
void SetMedium(google::protobuf::Arena* absl_nullable arena, absl::string_view string);
453461

462+
// This is used to create a medium byte string that is backed by an external
463+
// string. Should only be called from explicit 'Unsafe' factories.
464+
void SetExternalMedium(absl::string_view string);
465+
454466
void SetMedium(google::protobuf::Arena* absl_nullable arena, std::string&& string);
455467

456468
void SetMedium(google::protobuf::Arena* absl_nonnull arena, const absl::Cord& cord);

0 commit comments

Comments
 (0)