Skip to content

Reset CDR state #128

Closed
Closed

Description

@Kofemolka

Hello!

We are using CDR and FastBuffer inside our language bindings: Python, Golang, Java. For that we have a C-wrapper, consisting of struct to hold CDR and FastBuffer, and set of methods to serialize/deserialize different types.

Since we can't allocate this on stack, every time we need to serialize or deserialize some data model a new instance of this wrapping struct is allocated in the heap and deleted afterwards.
For performance reasons, we would like to create this struct and subsequently CDR and FastBuffer only once per our data model, and "reset" it with pointer to raw buffer during each serialize/deserialize sequence.

FastBuffer has perfectly legal way of re-init, since implements move ctor and assignment operator.
CDR has method reset() which also does what we need, except re-evaluation of m_lastPosition, which should point to the end of a FastBuffer.

Here is a sample usage:

struct hel_t::Pimpl
{
    Pimpl(unsigned char* buffer, unsigned int max_size, hel_mode mode)
        : fastbuffer(reinterpret_cast<char*>(buffer), max_size)
        , serializer(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::Cdr::DDS_CDR)
    {
        init(mode);
    }

    void init(hel_mode mode)
    {
        switch (mode)
        {
        case kHelModeSer:
            serializer.serialize_encapsulation();
            break;
        case kHelModeDes:
            serializer.read_encapsulation();
            break;
        default:
            throw std::invalid_argument("Unknown HEL mode value");
        }
    }

    eprosima::fastcdr::FastBuffer fastbuffer;
    eprosima::fastcdr::Cdr serializer;
};

void hel_reset(hel handle, unsigned char* buffer, uint32_t max_size, hel_mode mode)
{
    assert(handle != nullptr);

    handle->pimpl_->fastbuffer = eprosima::fastcdr::FastBuffer(reinterpret_cast<char*>(buffer), max_size);
    handle->pimpl_->serializer.reset();
    handle->pimpl_->init(mode);

    handle->status_ = kHelStatusOk;
}

After calling serializer.reset() it's m_lastPosition will point to the end of a previous buffer, not current.

Attaching one-liner patch that solves this.

Activity

  1. MiguelCompany commented on Sep 19, 2022

    @MiguelCompany
    Member

    @Kofemolka You already have the patch, so it would be great if you create a PR with two commits: one adding a test reproducing your problem, and another one with the attached patch.

    You could let us do the changes, but that would probably mean this will linger for a longer time, as we have little time to dedicate to a not so common use case.

  2. JLBuenoLopez commented on Oct 2, 2023

    @JLBuenoLopez
    Contributor

    Moving this ticket to the Ideas discussion forum where enhancements and feature requests are discussed.

  3. locked and limited conversation to collaborators on Oct 2, 2023
  4. converted this issue into a discussion #167 on Oct 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions