On Fri, Oct 11, 2024 at 7:25 AM Vanegicloh J <[email protected]> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/protobuf/2fca20f4-523b-44e7-920a-ec1712576cc5n%40googlegroups.com
> <https://groups.google.com/d/msgid/protobuf/2fca20f4-523b-44e7-920a-ec1712576cc5n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/protobuf/CAM9aRsK-CTP%3DJyBGM6x1HeesqbTKbsXSOMgFYsBH_-FdBwUf5A%40mail.gmail.com.

Reply via email to