Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Strings are still in heap?

65 views
Skip to first unread message

Vanegicloh J

unread,
Oct 11, 2024, 7:25:43 AM10/11/24
to Protocol Buffers
Hello.
Protobuf docs says that in c++ impl. strings and bytes are allocated in heap even if an arena is used. https://protobuf.dev/reference/cpp/arenas/#arenastring
Is this still relevant?

When i read the protobuf source code, i got a bit confused, because in arenastring.h file i found that:
```
// The string pointer is tagged to be either a default, externally owned value,
// a mutable heap allocated value, or an arena allocated value.
```
1. So, if i use arena there would be no heap allocations for string fields. Because for std::string allocation memory would be taken from the arena instead of the heap. Is this correct?

2. Also, there are Set(const char* s, size_t n, Arena* arena) methods that don't use std::string at all. Although, pointer will go to the Arena::Create<std::string>(&arena, s.data(), s.length()) method. I didn't find what is going on after this, but semantically it shouldn't use heap too. Is this correct?

Thank you.

Samuel Benzaquen

unread,
Oct 11, 2024, 9:44:23 AM10/11/24
to Vanegicloh J, Protocol Buffers
On Fri, Oct 11, 2024 at 7:25 AM Vanegicloh J <vaneg...@gmail.com> wrote:
Hello.
Protobuf docs says that in c++ impl. strings and bytes are allocated in heap even if an arena is used. https://protobuf.dev/reference/cpp/arenas/#arenastring
Is this still relevant?

When i read the protobuf source code, i got a bit confused, because in arenastring.h file i found that:
```
// The string pointer is tagged to be either a default, externally owned value,
// a mutable heap allocated value, or an arena allocated value.
```
1. So, if i use arena there would be no heap allocations for string fields. Because for std::string allocation memory would be taken from the arena instead of the heap. Is this correct?

There's two pieces of memory: the std::string itself and the payload that the std::string allocates.
We can allocate the std::string in the arena, but the std::string manages its own heap memory internally. The allocator for the string is not modified.
 
2. Also, there are Set(const char* s, size_t n, Arena* arena) methods that don't use std::string at all. Although, pointer will go to the Arena::Create<std::string>(&arena, s.data(), s.length()) method. I didn't find what is going on after this, but semantically it shouldn't use heap too. Is this correct?

The setter signature is not relevant. The internals of this class will always use a std::string because that is what the public API of the message needs.
Any way you end up populating the string field will end up creating a std::string.
 

Thank you.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/2fca20f4-523b-44e7-920a-ec1712576cc5n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages