Skip to content

Conversation

Mario-DL
Copy link
Member

@Mario-DL Mario-DL commented Jul 23, 2025

Description

This PR makes Fast CDR look for external (de)serialize_array template specializations (now provided by the generated types) so that flat, non-primitive arrays or vectors can be optimally serialized (memcpy) if certain conditions are met.
These changes break API so must be in a new major release.

Related PRs:

Contributor Checklist

  • Commit messages follow the project guidelines.
  • The code follows the style guidelines of this project.
  • Tests that thoroughly check the new feature have been added/Regression tests checking the bug and its fix have been added; the added tests pass locally
  • Any new/modified methods have been properly documented using Doxygen.
  • NO Changes are backport compatible: they do NOT break ABI nor change library core behavior.
  • NO Changes are API compatible.
  • New feature has been added to the versions.md file (if applicable).
  • N/A Applicable backports have been included in the description.

Reviewer Checklist

  • The PR has a milestone assigned.
  • The title and description correctly express the PR's purpose.
  • Check contributor checklist is correct.
  • Check CI results: changes do not issue any warning.
  • Check CI results: CI pass and failing tests are unrelated with the changes.

@Mario-DL Mario-DL added this to the v3.0.0 milestone Jul 23, 2025
Signed-off-by: Mario Dominguez <[email protected]>
Copy link
Member

@MiguelCompany MiguelCompany left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice refactor!
I really like the traits and SFINAE being used.

Leaving some comments

Comment on lines +35 to +41
static_assert(static_is_multi_array_primitive<uint8_t const*>::value, "uint8_t* should be a multi primitive array");
static_assert(static_is_multi_array_primitive<uint16_t const*>::value,
"uint16_t* should be a multi primitive array");
static_assert(static_is_multi_array_primitive<uint32_t const*>::value,
"uint32_t* should be a multi primitive array");
static_assert(static_is_multi_array_primitive<uint64_t const*>::value,
"uint64_t* should be a multi primitive array");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing signed versions of these tests

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also missing tests for enum

Comment on lines +50 to +51
static_assert(!static_is_multi_array_primitive<std::string const*>::value,
"std::string* should not be a multi primitive array");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing fixed_string

Comment on lines +60 to +71
template<class _T>
extern void serialize_array(
Cdr&,
const _T*,
const size_t);

template<class _T>
extern void deserialize_array(
Cdr&,
_T*,
const size_t);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comments for all four declarations

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also change the declaration order. Either serialize, deserialize, serialize_array, deserialize_array or serialize, serialize_array, deserialize, deserialize_array

Cdr& serialize(
const std::array<_T, _Size>& array_t)
{
if (!is_multi_array_primitive(&array_t))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems is_multi_array_primitive is not used anymore (since we now rely on static_is_multi_array_primitive).
Might be a good idea to remove file container_recursive_inspector.hpp and perhaps also rename static_is_multi_array_primitive into is_multi_array_primitive.

Cdr::state dheader_state {allocate_xcdrv2_dheader()};

serialize_array(array_t.data(), array_t.size());
serialize(static_cast<int32_t>(vector_t.size()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
serialize(static_cast<int32_t>(vector_t.size()));
serialize(static_cast<uint32_t>(vector_t.size()));

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should check that the size is not bigger than the maximum value of a uint32_t.
I propose having a serialize_collection_length(size_t length) method that throws an exception when length > MAX_UINT32, and then serializes static_cast<uint32_t>(length).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this would imply changes in other methods unrelated to this PR. It might be a good idea to do that refactoring on a separate PR that we can backport.

Comment on lines +2025 to +2041
if ((end_ - offset_) < sequence_length)
{
set_state(state_before_error);
throw exception::NotEnoughMemoryException(
exception::NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
}

try
{
vector_t.resize(sequence_length);
eprosima::fastcdr::deserialize_array(*this, vector_t.data(), vector_t.size());
}
catch (exception::Exception& ex)
{
set_state(state_before_error);
ex.raise();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shall follow this same part in the XCDRv2 part of the if above.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, this seems to come from the old code, so a separate PR fixing it that can be backported should be done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants