Skip to content

Commit

Permalink
[ntuple] Fix fSize calculation in RRecordField::AttachItemFields
Browse files Browse the repository at this point in the history
It was missing the trailing padding, which was covered up by the typed
RField specializations overriding the fSize member. Convert them into
assertions, which can only compare inequality because the alignment of
RMapField and RSetField are pessimized since commit 49ef6f9.
  • Loading branch information
hahnjo committed Oct 28, 2024
1 parent cd2865a commit bcc29e8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
11 changes: 7 additions & 4 deletions tree/ntuple/v7/inc/ROOT/RField/RFieldRecord.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ protected:
fTraits &= itemFields[i]->GetTraits();
Attach(std::move(itemFields[i]));
}
// Trailing padding: although this is implementation-dependent, most add enough padding to comply with the
// requirements of the type with strictest alignment
fSize += GetItemPadding(fSize, fMaxAlignment);
}

public:
Expand Down Expand Up @@ -145,8 +148,8 @@ public:
static std::string TypeName() { return "std::pair<" + RField<T1>::TypeName() + "," + RField<T2>::TypeName() + ">"; }
explicit RField(std::string_view name) : RPairField(name, BuildItemFields(), BuildItemOffsets())
{
fMaxAlignment = std::max(alignof(T1), alignof(T2));
fSize = sizeof(ContainerT);
R__ASSERT(fMaxAlignment >= std::max(alignof(T1), alignof(T2)));
R__ASSERT(fSize >= sizeof(ContainerT));
}
RField(RField &&other) = default;
RField &operator=(RField &&other) = default;
Expand Down Expand Up @@ -221,8 +224,8 @@ public:
static std::string TypeName() { return "std::tuple<" + BuildItemTypes<ItemTs...>() + ">"; }
explicit RField(std::string_view name) : RTupleField(name, BuildItemFields(), BuildItemOffsets())
{
fMaxAlignment = std::max({alignof(ItemTs)...});
fSize = sizeof(ContainerT);
R__ASSERT(fMaxAlignment >= std::max({alignof(ItemTs)...}));
R__ASSERT(fSize >= sizeof(ContainerT));
}
RField(RField &&other) = default;
RField &operator=(RField &&other) = default;
Expand Down
3 changes: 3 additions & 0 deletions tree/ntuple/v7/src/RField.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2522,6 +2522,9 @@ void ROOT::Experimental::RRecordField::RRecordField::AttachItemFields(
fTraits &= item->GetTraits();
Attach(std::move(item));
}
// Trailing padding: although this is implementation-dependent, most add enough padding to comply with the
// requirements of the type with strictest alignment
fSize += GetItemPadding(fSize, fMaxAlignment);
}

ROOT::Experimental::RRecordField::RRecordField(std::string_view fieldName,
Expand Down
8 changes: 8 additions & 0 deletions tree/ntuple/v7/test/ntuple_types.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,14 @@ TEST(RNTuple, StdTuple)
"tupleTupleField");
EXPECT_STREQ("std::tuple<std::tuple<std::int64_t,float,char,float>,std::vector<std::tuple<char,char,char>>>",
tupleTupleField.GetTypeName().c_str());
using TupleMapSetType = std::tuple<std::map<char, int64_t>, std::set<int64_t>>;
auto tupleMapSetField = RField<TupleMapSetType>("tupleMapSetField");
EXPECT_LE(sizeof(TupleMapSetType), tupleMapSetField.GetValueSize());
EXPECT_LE(alignof(TupleMapSetType), tupleMapSetField.GetAlignment());
using TupleUnorderedMapSetType = std::tuple<std::unordered_map<char, int64_t>, std::unordered_set<int64_t>>;
auto tupleUnorderedMapSetField = RField<TupleUnorderedMapSetType>("tupleUnorderedMapSetField");
EXPECT_LE(sizeof(TupleUnorderedMapSetType), tupleUnorderedMapSetField.GetValueSize());
EXPECT_LE(alignof(TupleUnorderedMapSetType), tupleUnorderedMapSetField.GetAlignment());

FileRaii fileGuard("test_ntuple_rfield_stdtuple.root");
{
Expand Down

0 comments on commit bcc29e8

Please sign in to comment.