Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions tree/ntuple/src/RFieldSequenceContainer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,21 @@ unsigned char *ROOT::RRVecField::ResizeRVec(void *rvec, std::size_t nItems, std:
}
}

// TODO Increment capacity by a factor rather than just enough to fit the elements.
// Increment capacity geometrically rather than just enough to fit the elements.
std::size_t newCapacity = nItems;
if (*capacityPtr > 0) {
constexpr std::size_t maxSize = static_cast<std::size_t>(std::numeric_limits<std::int32_t>::max());
const std::size_t oldCap = static_cast<std::size_t>(*capacityPtr);
const std::size_t grown = oldCap <= maxSize / 2 ? oldCap * 2 : maxSize;
newCapacity = std::min(maxSize, std::max(nItems, grown));
}

Internal::DestroyRVecWithChecks(itemField->GetAlignment(), beginPtr, capacityPtr);
// We trust that malloc returns a buffer with large enough alignment.
// This might not be the case if T in RVec<T> is over-aligned.
*beginPtr = static_cast<unsigned char *>(malloc(nItems * itemSize));
*beginPtr = static_cast<unsigned char *>(malloc(newCapacity * itemSize));
R__ASSERT(*beginPtr != nullptr);
*capacityPtr = nItems;
*capacityPtr = static_cast<std::int32_t>(newCapacity);

// Placement new for elements that were already there before the resize
if (needsConstruct) {
Expand Down
41 changes: 40 additions & 1 deletion tree/ntuple/test/rfield_vector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ TEST(RNTuple, InsideCollection)
// TODO: test reading of "klassVec.v1"
}


TEST(RNTuple, RVec)
{
FileRaii fileGuard("test_ntuple_rvec.root");
Expand Down Expand Up @@ -304,6 +303,46 @@ TEST(RNTuple, ReadVectorAsRVec)
EXPECT_TRUE(rvec->empty());
}

TEST(RNTuple, RVecGeometricGrowth)
{
FileRaii fileGuard("test_ntuple_rvec_geometric_growth.root");

{
auto m = RNTupleModel::Create();
auto rvec = m->MakeField<ROOT::RVec<int>>("v");
auto w = RNTupleWriter::Recreate(std::move(m), "ntpl", fileGuard.GetPath());

*rvec = {1, 2};
w->Fill();
*rvec = {1, 2, 3};
w->Fill();
*rvec = {1, 2, 3, 4};
w->Fill();
*rvec = {10};
w->Fill();
}

auto m = RNTupleModel::Create();
auto rvec = m->MakeField<ROOT::RVec<int>>("v");
auto reader = RNTupleReader::Open(std::move(m), "ntpl", fileGuard.GetPath());

reader->LoadEntry(0);
EXPECT_EQ(rvec->size(), 2u);
EXPECT_EQ(rvec->capacity(), 2u);

reader->LoadEntry(1);
EXPECT_EQ(rvec->size(), 3u);
EXPECT_GE(rvec->capacity(), 4u);

reader->LoadEntry(2);
EXPECT_EQ(rvec->size(), 4u);
EXPECT_GE(rvec->capacity(), 4u);

reader->LoadEntry(3);
EXPECT_EQ(rvec->size(), 1u);
EXPECT_GE(rvec->capacity(), 4u);
}

TEST(RNTuple, BoolVector)
{
FileRaii fileGuard("test_ntuple_boolvec.root");
Expand Down
Loading