Skip to content

Optimize memory allocations for strings with default values #73

@mikir

Description

@mikir

Consider the following schema:

struct AllocationStruct
{
    string         stringField;
    string         defaultStringField = "Structure Default String Field Must Be Longer Than 32 Bytes";
};

Then, the generated data constructor will allocate memory for the default string value:

AllocationStruct::AllocationStruct(const AllocatorType& allocator) noexcept :
        stringField(allocator),
        defaultStringField(::std::string_view{"Structure Default String Field Must Be Longer Than 32 Bytes"}, allocator)
{}

If such constructed data are used as an input for reading (zserio::read), then the defaultStringField is reallocated and initialized with the new read value. This introduces undesired heap memory fragmantation during reading.

This problem can be probably solved by a new constructor which won't fill default values. However, such constructor can be misused.

In all cases, heap memory fragmantation can be considered as a problem, so we should pay attention to it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions