Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RFieldSequenceContainer.cxx
Go to the documentation of this file.
1/// \file RFieldSequenceContainer.cxx
2/// \author Jonas Hahnfeld <jonas.hahnfeld@cern.ch>
3/// \date 2024-11-19
4
5#include <ROOT/BitUtils.hxx>
6#include <ROOT/RField.hxx>
7#include <ROOT/RFieldBase.hxx>
10
11#include <limits>
12#include <memory>
13#include <new> // hardware_destructive_interference_size, operator new
14
15namespace {
16
17std::vector<ROOT::RFieldBase::RValue> SplitVector(const std::shared_ptr<void> &valuePtr, ROOT::RFieldBase &itemField)
18{
19 auto *vec = static_cast<std::vector<char> *>(valuePtr.get());
20 const auto itemSize = itemField.GetValueSize();
21 R__ASSERT(itemSize > 0);
22 R__ASSERT((vec->size() % itemSize) == 0);
23 const auto nItems = vec->size() / itemSize;
24 std::vector<ROOT::RFieldBase::RValue> result;
25 result.reserve(nItems);
26 for (unsigned i = 0; i < nItems; ++i) {
27 result.emplace_back(itemField.BindValue(std::shared_ptr<void>(valuePtr, vec->data() + (i * itemSize))));
28 }
29 return result;
30}
31
32std::size_t GetSizeOfVector()
33{
34 return sizeof(std::vector<char>);
35}
36
37std::size_t GetAlignOfVector()
38{
39 return alignof(std::vector<char>);
40}
41
42void ConstructVector(void *where, std::size_t alignOfValue)
43{
44 static_assert(ROOT::RVectorField::kMaxItemAlignment == 4096);
45 // clang-format off
46 switch (alignOfValue) {
47 case 1: new (where) std::vector<ROOT::Internal::RAlignedStorage< 1>>(); break;
48 case 2: new (where) std::vector<ROOT::Internal::RAlignedStorage< 2>>(); break;
49 case 4: new (where) std::vector<ROOT::Internal::RAlignedStorage< 4>>(); break;
50 case 8: new (where) std::vector<ROOT::Internal::RAlignedStorage< 8>>(); break;
51 case 16: new (where) std::vector<ROOT::Internal::RAlignedStorage< 16>>(); break;
52 case 32: new (where) std::vector<ROOT::Internal::RAlignedStorage< 32>>(); break;
53 case 64: new (where) std::vector<ROOT::Internal::RAlignedStorage< 64>>(); break;
54 case 128: new (where) std::vector<ROOT::Internal::RAlignedStorage< 128>>(); break;
55 case 256: new (where) std::vector<ROOT::Internal::RAlignedStorage< 256>>(); break;
56 case 512: new (where) std::vector<ROOT::Internal::RAlignedStorage< 512>>(); break;
57 case 1024: new (where) std::vector<ROOT::Internal::RAlignedStorage<1024>>(); break;
58 case 2048: new (where) std::vector<ROOT::Internal::RAlignedStorage<2048>>(); break;
59 case 4096: new (where) std::vector<ROOT::Internal::RAlignedStorage<4096>>(); break;
60 default: throw ROOT::RException(R__FAIL(std::string("Unsupported alignment: ") + std::to_string(alignOfValue)));
61 }
62 // clang-format on
63}
64
65} // anonymous namespace
66
67ROOT::RArrayField::RArrayField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField,
68 std::size_t arrayLength)
69 : ROOT::RFieldBase(fieldName,
70 "std::array<" + itemField->GetTypeName() + "," +
71 Internal::GetNormalizedInteger(static_cast<unsigned long long>(arrayLength)) + ">",
72 ROOT::ENTupleStructure::kPlain, false /* isSimple */, arrayLength),
73 fItemSize(itemField->GetValueSize()),
74 fArrayLength(arrayLength)
75{
76 Internal::EnsureTypedChildField(*itemField, *this);
77 if (arrayLength == 0) {
78 throw RException(R__FAIL("invalid zero-length array field " + GetQualifiedFieldName()));
79 }
80 fTraits |= itemField->GetTraits() & ~kTraitMappable;
81 if (!itemField->GetTypeAlias().empty()) {
82 fTypeAlias = "std::array<" + itemField->GetTypeAlias() + "," +
83 Internal::GetNormalizedInteger(static_cast<unsigned long long>(arrayLength)) + ">";
84 }
85 Attach(std::move(itemField), "_0");
86}
87
88std::unique_ptr<ROOT::RFieldBase> ROOT::RArrayField::CloneImpl(std::string_view newName) const
89{
90 auto newItemField = fSubfields[0]->Clone(fSubfields[0]->GetFieldName());
91 return std::make_unique<RArrayField>(newName, std::move(newItemField), fArrayLength);
92}
93
94std::size_t ROOT::RArrayField::AppendImpl(const void *from)
95{
96 std::size_t nbytes = 0;
97 if (fSubfields[0]->IsSimple()) {
98 GetPrincipalColumnOf(*fSubfields[0])->AppendV(from, fArrayLength);
99 nbytes += fArrayLength * GetPrincipalColumnOf(*fSubfields[0])->GetElement()->GetPackedSize();
100 } else {
101 auto arrayPtr = static_cast<const unsigned char *>(from);
102 for (unsigned i = 0; i < fArrayLength; ++i) {
103 nbytes += CallAppendOn(*fSubfields[0], arrayPtr + (i * fItemSize));
104 }
105 }
106 return nbytes;
107}
108
110{
111 if (fSubfields[0]->IsSimple()) {
112 GetPrincipalColumnOf(*fSubfields[0])->ReadV(globalIndex * fArrayLength, fArrayLength, to);
113 } else {
114 auto arrayPtr = static_cast<unsigned char *>(to);
115 for (unsigned i = 0; i < fArrayLength; ++i) {
116 CallReadOn(*fSubfields[0], globalIndex * fArrayLength + i, arrayPtr + (i * fItemSize));
117 }
118 }
119}
120
122{
123 if (fSubfields[0]->IsSimple()) {
124 GetPrincipalColumnOf(*fSubfields[0])->ReadV(localIndex * fArrayLength, fArrayLength, to);
125 } else {
126 auto arrayPtr = static_cast<unsigned char *>(to);
127 for (unsigned i = 0; i < fArrayLength; ++i) {
128 CallReadOn(*fSubfields[0], localIndex * fArrayLength + i, arrayPtr + (i * fItemSize));
129 }
130 }
131}
132
133std::size_t ROOT::RArrayField::ReadBulkImpl(const RBulkSpec &bulkSpec)
134{
135 if (!fSubfields[0]->IsSimple())
136 return RFieldBase::ReadBulkImpl(bulkSpec);
137
138 GetPrincipalColumnOf(*fSubfields[0])
139 ->ReadV(bulkSpec.fFirstIndex * fArrayLength, bulkSpec.fCount * fArrayLength, bulkSpec.fValues);
140 return RBulkSpec::kAllSet;
141}
142
144{
145 static const std::vector<std::string> prefixes = {"std::array<"};
146
147 EnsureMatchingOnDiskField(desc, kDiffTypeName).ThrowOnError();
148 EnsureMatchingTypePrefix(desc, prefixes).ThrowOnError();
149}
150
152{
153 if (fSubfields[0]->GetTraits() & kTraitTriviallyConstructible)
154 return;
155
156 auto arrayPtr = reinterpret_cast<unsigned char *>(where);
157 for (unsigned i = 0; i < fArrayLength; ++i) {
158 CallConstructValueOn(*fSubfields[0], arrayPtr + (i * fItemSize));
159 }
160}
161
162void ROOT::RArrayField::RArrayDeleter::operator()(void *objPtr, bool dtorOnly)
163{
164 if (fItemDeleter) {
165 for (unsigned i = 0; i < fArrayLength; ++i) {
166 fItemDeleter->operator()(reinterpret_cast<unsigned char *>(objPtr) + i * fItemSize, true /* dtorOnly */);
167 }
168 }
169 RDeleter::operator()(objPtr, dtorOnly);
170}
171
172std::unique_ptr<ROOT::RFieldBase::RDeleter> ROOT::RArrayField::GetDeleter() const
173{
174 if (!(fSubfields[0]->GetTraits() & kTraitTriviallyDestructible))
175 return std::make_unique<RArrayDeleter>(fItemSize, fArrayLength, GetAlignment(), GetDeleterOf(*fSubfields[0]));
176 return std::make_unique<RDeleter>(GetAlignment());
177}
178
179std::vector<ROOT::RFieldBase::RValue> ROOT::RArrayField::SplitValue(const RValue &value) const
180{
181 auto valuePtr = value.GetPtr<void>();
182 auto arrayPtr = static_cast<unsigned char *>(valuePtr.get());
183 std::vector<RValue> result;
184 result.reserve(fArrayLength);
185 for (unsigned i = 0; i < fArrayLength; ++i) {
186 result.emplace_back(fSubfields[0]->BindValue(std::shared_ptr<void>(valuePtr, arrayPtr + (i * fItemSize))));
187 }
188 return result;
189}
190
192{
193 visitor.VisitArrayField(*this);
194}
195
196//------------------------------------------------------------------------------
197
198ROOT::RRVecField::RRVecField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField)
199 : ROOT::RFieldBase(fieldName, "ROOT::VecOps::RVec<" + itemField->GetTypeName() + ">",
200 ROOT::ENTupleStructure::kCollection, false /* isSimple */),
201 fItemSize(itemField->GetValueSize()),
202 fNWritten(0)
203{
204 Internal::EnsureTypedChildField(*itemField, *this);
205 if (itemField->GetAlignment() > sizeof(std::max_align_t)) {
206 // RVec uses operator new without alignment info
207 throw RException(R__FAIL("RVec does not support over-aligned types"));
208 }
209
210 if (!(itemField->GetTraits() & kTraitTriviallyDestructible))
211 fItemDeleter = GetDeleterOf(*itemField);
212 if (!itemField->GetTypeAlias().empty())
213 fTypeAlias = "ROOT::VecOps::RVec<" + itemField->GetTypeAlias() + ">";
214 Attach(std::move(itemField), "_0");
215 fValueSize =
217
218 // Determine if we can optimimize bulk reading
219 if (fSubfields[0]->IsSimple()) {
220 fBulkSubfield = fSubfields[0].get();
221 } else {
222 if (auto f = dynamic_cast<RArrayField *>(fSubfields[0].get())) {
223 auto grandChildFields = fSubfields[0]->GetMutableSubfields();
224 if (grandChildFields[0]->IsSimple()) {
225 fBulkSubfield = grandChildFields[0];
226 fBulkNRepetition = f->GetLength();
227 }
228 }
229 }
230}
231
232std::unique_ptr<ROOT::RFieldBase> ROOT::RRVecField::CloneImpl(std::string_view newName) const
233{
234 auto newItemField = fSubfields[0]->Clone(fSubfields[0]->GetFieldName());
235 return std::make_unique<RRVecField>(newName, std::move(newItemField));
236}
237
238std::size_t ROOT::RRVecField::AppendImpl(const void *from)
239{
240 auto [beginPtr, sizePtr, _] = Internal::GetRVecDataMembers(from);
241
242 std::size_t nbytes = 0;
243 if (fSubfields[0]->IsSimple() && *sizePtr) {
244 GetPrincipalColumnOf(*fSubfields[0])->AppendV(*beginPtr, *sizePtr);
245 nbytes += *sizePtr * GetPrincipalColumnOf(*fSubfields[0])->GetElement()->GetPackedSize();
246 } else {
247 for (std::int32_t i = 0; i < *sizePtr; ++i) {
248 nbytes += CallAppendOn(*fSubfields[0], *beginPtr + i * fItemSize);
249 }
250 }
251
252 fNWritten += *sizePtr;
253 fPrincipalColumn->Append(&fNWritten);
254 return nbytes + fPrincipalColumn->GetElement()->GetPackedSize();
255}
256
257unsigned char *ROOT::RRVecField::ResizeRVec(void *rvec, std::size_t nItems, std::size_t itemSize,
258 const RFieldBase *itemField, RDeleter *itemDeleter)
259
260{
261 if (nItems > static_cast<std::size_t>(std::numeric_limits<std::int32_t>::max())) {
262 throw RException(R__FAIL("RVec too large: " + std::to_string(nItems)));
263 }
264
265 auto [beginPtr, sizePtr, capacityPtr] = Internal::GetRVecDataMembers(rvec);
266 const std::size_t oldSize = *sizePtr;
267
268 if (oldSize == nItems) {
269 // If neither shrink nor grow is necessary, do nothing.
270 // Note that this case preserves a memory adopting RVec as such. All real resizes in either direction
271 // transform a memory adopting RVec into an owning RVec.
272 return *beginPtr;
273 }
274
275 // See "semantics of reading non-trivial objects" in RNTuple's Architecture.md for details
276 // on the element construction/destrution.
277 const bool owns = (*capacityPtr != -1);
278 const bool needsConstruct = !(itemField->GetTraits() & kTraitTriviallyConstructible);
279 const bool needsDestruct = owns && itemDeleter;
280
281 // Destroy excess elements, if any
282 if (needsDestruct) {
283 for (std::size_t i = nItems; i < oldSize; ++i) {
284 itemDeleter->operator()(*beginPtr + (i * itemSize), true /* dtorOnly */);
285 }
286 }
287
288 // Resize RVec (capacity and size)
289 // In the regime between 0 and 16 items, grow by doubling the size. Grow larger vectors by a factor 1.5 at most.
290 // If the new size is larger than the grown capacity, set the new capacity to the new size (i.e., don't allocate
291 // a larger capacity for vectors the are suddenly vastly larger than their previous state).
292 // TODO(jblomer): investigate a longer look back window to avoid shrinking for an unusual small entry and consider
293 // returning to average or median size (see TBasket::WriteReset()).
294 static constexpr std::int32_t kSmallThreshold = 16;
295 if ((std::int32_t(nItems) > *capacityPtr) || /* must grow */
296 ((std::int32_t(nItems) > kSmallThreshold) && (std::int32_t(nItems) < *capacityPtr / 4))) /* must shrink */ {
297 // Destroy old elements: useless work for trivial types, but in case the element type's constructor
298 // allocates memory we need to release it here to avoid memleaks (e.g. if this is an RVec<RVec<int>>)
299 if (needsDestruct) {
300 // Excess elements were destructed earlier.
301 for (std::size_t i = 0u; i < std::min(oldSize, nItems); ++i) {
302 itemDeleter->operator()(*beginPtr + (i * itemSize), true /* dtorOnly */);
303 }
304 }
305
306 // Increment / decrement capacity geometrically
307 std::size_t newCapacity = nItems;
308 if (*capacityPtr > 0) {
309 constexpr std::size_t maxSize = static_cast<std::size_t>(std::numeric_limits<std::int32_t>::max());
310 const std::size_t oldCap = static_cast<std::size_t>(*capacityPtr);
311 if (nItems > oldCap) {
312 const std::size_t grown = oldCap + ((oldCap < kSmallThreshold) ? oldCap : (oldCap / 2));
313 newCapacity = std::min(maxSize, std::max(nItems, grown));
314 } else {
315 newCapacity = oldCap / 2;
316 assert(nItems <= newCapacity);
317 }
318 }
319
320 Internal::DestroyRVecWithChecks(itemField->GetAlignment(), beginPtr, capacityPtr);
321 // We checked that the item type is not over-aligned
322 *beginPtr = static_cast<unsigned char *>(::operator new(newCapacity * itemSize, std::nothrow));
323 R__ASSERT(*beginPtr != nullptr);
324 *capacityPtr = static_cast<std::int32_t>(newCapacity);
325
326 // Placement new for elements that were already there before the resize. When shrank, we only need to construct
327 // nItems items, because that's what we going to read. When grown, the remaining elements from oldSize until
328 // nItems is constructed a few lines down (we need to do this always when the size grows, even when not changing
329 // the capacity).
330 if (needsConstruct) {
331 for (std::size_t i = 0u; i < std::min(oldSize, nItems); ++i)
332 CallConstructValueOn(*itemField, *beginPtr + (i * itemSize));
333 }
334 }
335 *sizePtr = nItems;
336
337 // Placement new for new elements, if any
338 if (needsConstruct) {
339 for (std::size_t i = oldSize; i < nItems; ++i)
340 CallConstructValueOn(*itemField, *beginPtr + (i * itemSize));
341 }
342
343 return *beginPtr;
344}
345
347{
348 // TODO as a performance optimization, we could assign values to elements of the inline buffer:
349 // if size < inline buffer size: we save one allocation here and usage of the RVec skips a pointer indirection
350
351 // Read collection info for this entry
352 ROOT::NTupleSize_t nItems;
353 RNTupleLocalIndex collectionStart;
354 fPrincipalColumn->GetCollectionInfo(globalIndex, &collectionStart, &nItems);
355
356 auto begin = ResizeRVec(to, nItems, fItemSize, fSubfields[0].get(), fItemDeleter.get());
357
358 if (fSubfields[0]->IsSimple() && nItems) {
359 GetPrincipalColumnOf(*fSubfields[0])->ReadV(collectionStart, nItems, begin);
360 return;
361 }
362
363 // Read the new values into the collection elements
364 for (std::size_t i = 0; i < nItems; ++i) {
365 CallReadOn(*fSubfields[0], collectionStart + i, begin + (i * fItemSize));
366 }
367}
368
369std::size_t ROOT::RRVecField::ReadBulkImpl(const RBulkSpec &bulkSpec)
370{
371 if (!fBulkSubfield)
372 return RFieldBase::ReadBulkImpl(bulkSpec);
373
374 if (bulkSpec.fAuxData->empty()) {
375 /// Initialize auxiliary memory: the first sizeof(size_t) bytes store the value size of the item field.
376 /// The following bytes store the item values, consecutively.
377 bulkSpec.fAuxData->resize(sizeof(std::size_t));
378 *reinterpret_cast<std::size_t *>(bulkSpec.fAuxData->data()) = fBulkNRepetition * fBulkSubfield->GetValueSize();
379 }
380 const auto itemValueSize = *reinterpret_cast<std::size_t *>(bulkSpec.fAuxData->data());
381 unsigned char *itemValueArray = bulkSpec.fAuxData->data() + sizeof(std::size_t);
382 auto [beginPtr, sizePtr, capacityPtr] = Internal::GetRVecDataMembers(bulkSpec.fValues);
383
384 // Get size of the first RVec of the bulk
385 RNTupleLocalIndex firstItemIndex;
386 ROOT::NTupleSize_t collectionSize;
387 fPrincipalColumn->GetCollectionInfo(bulkSpec.fFirstIndex, &firstItemIndex, &collectionSize);
388 *beginPtr = itemValueArray;
389 *sizePtr = collectionSize;
390 *capacityPtr = -1;
391
392 // Set the size of the remaining RVecs of the bulk, going page by page through the RNTuple offset column.
393 // We optimistically assume that bulkSpec.fAuxData is already large enough to hold all the item values in the
394 // given range. If not, we'll fix up the pointers afterwards.
395 auto lastOffset = firstItemIndex.GetIndexInCluster() + collectionSize;
396 ROOT::NTupleSize_t nRemainingValues = bulkSpec.fCount - 1;
397 std::size_t nValues = 1;
398 std::size_t nItems = collectionSize;
399 while (nRemainingValues > 0) {
400 ROOT::NTupleSize_t nElementsUntilPageEnd;
401 const auto offsets =
402 fPrincipalColumn->MapV<ROOT::Internal::RColumnIndex>(bulkSpec.fFirstIndex + nValues, nElementsUntilPageEnd);
403 const std::size_t nBatch = std::min(nRemainingValues, nElementsUntilPageEnd);
404 for (std::size_t i = 0; i < nBatch; ++i) {
405 const auto size = offsets[i] - lastOffset;
406 std::tie(beginPtr, sizePtr, capacityPtr) = Internal::GetRVecDataMembers(
407 reinterpret_cast<unsigned char *>(bulkSpec.fValues) + (nValues + i) * fValueSize);
408 *beginPtr = itemValueArray + nItems * itemValueSize;
409 *sizePtr = size;
410 *capacityPtr = -1;
411
412 nItems += size;
413 lastOffset = offsets[i];
414 }
415 nRemainingValues -= nBatch;
416 nValues += nBatch;
417 }
418
419 bulkSpec.fAuxData->resize(sizeof(std::size_t) + nItems * itemValueSize);
420 // If the vector got reallocated, we need to fix-up the RVecs begin pointers.
421 const auto delta = itemValueArray - (bulkSpec.fAuxData->data() + sizeof(std::size_t));
422 if (delta != 0) {
423 auto beginPtrAsUChar = reinterpret_cast<unsigned char *>(bulkSpec.fValues);
424 for (std::size_t i = 0; i < bulkSpec.fCount; ++i) {
425 *reinterpret_cast<unsigned char **>(beginPtrAsUChar) -= delta;
426 beginPtrAsUChar += fValueSize;
427 }
428 }
429
430 GetPrincipalColumnOf(*fBulkSubfield)
431 ->ReadV(firstItemIndex * fBulkNRepetition, nItems * fBulkNRepetition, itemValueArray - delta);
432 return RBulkSpec::kAllSet;
433}
434
444
446{
447 GenerateColumnsImpl<ROOT::Internal::RColumnIndex>();
448}
449
451{
452 GenerateColumnsImpl<ROOT::Internal::RColumnIndex>(desc);
453}
454
455std::unique_ptr<ROOT::RFieldBase> ROOT::RRVecField::BeforeConnectPageSource(Internal::RPageSource &pageSource)
456{
457 if (GetOnDiskId() == kInvalidDescriptorId)
458 return nullptr;
459
460 auto descGuard = pageSource.GetSharedDescriptorGuard();
461 const auto &fieldDesc = descGuard->GetFieldDescriptor(GetOnDiskId());
462 if (fieldDesc.GetTypeName().rfind("std::array<", 0) == 0) {
463 auto substitute = std::make_unique<RArrayAsRVecField>(
464 GetFieldName(), fSubfields[0]->Clone(fSubfields[0]->GetFieldName()), fieldDesc.GetNRepetitions());
465 substitute->SetOnDiskId(GetOnDiskId());
466 return substitute;
467 }
468 return nullptr;
469}
470
472{
473 EnsureMatchingOnDiskCollection(desc).ThrowOnError();
474}
475
476void ROOT::RRVecField::ConstructValue(void *where) const
477{
478 // initialize data members fBegin, fSize, fCapacity
479 // currently the inline buffer is left uninitialized
480 void **beginPtr = new (where)(void *)(nullptr);
481 std::int32_t *sizePtr = new (reinterpret_cast<void *>(beginPtr + 1)) std::int32_t(0);
482 new (sizePtr + 1) std::int32_t(-1);
483}
484
485void ROOT::RRVecField::RRVecDeleter::operator()(void *objPtr, bool dtorOnly)
486{
487 auto [beginPtr, sizePtr, capacityPtr] = Internal::GetRVecDataMembers(objPtr);
488
489 if (fItemDeleter) {
490 for (std::int32_t i = 0; i < *sizePtr; ++i) {
491 fItemDeleter->operator()(*beginPtr + i * fItemSize, true /* dtorOnly */);
492 }
493 }
494
495 Internal::DestroyRVecWithChecks(fItemAlignment, beginPtr, capacityPtr);
496 RDeleter::operator()(objPtr, dtorOnly);
497}
498
499std::unique_ptr<ROOT::RFieldBase::RDeleter> ROOT::RRVecField::GetDeleter() const
500{
501 if (fItemDeleter)
502 return std::make_unique<RRVecDeleter>(fSubfields[0]->GetAlignment(), fItemSize, GetDeleterOf(*fSubfields[0]));
503 return std::make_unique<RRVecDeleter>(fSubfields[0]->GetAlignment());
504}
505
506std::vector<ROOT::RFieldBase::RValue> ROOT::RRVecField::SplitValue(const RValue &value) const
507{
508 auto [beginPtr, sizePtr, _] = Internal::GetRVecDataMembers(value.GetPtr<void>().get());
509
510 std::vector<RValue> result;
511 result.reserve(*sizePtr);
512 for (std::int32_t i = 0; i < *sizePtr; ++i) {
513 result.emplace_back(
514 fSubfields[0]->BindValue(std::shared_ptr<void>(value.GetPtr<void>(), *beginPtr + i * fItemSize)));
515 }
516 return result;
517}
518
520{
521 return fValueSize;
522}
523
525{
526 return Internal::EvalRVecAlignment(fSubfields[0]->GetAlignment());
527}
528
530{
531 visitor.VisitRVecField(*this);
532}
533
534//------------------------------------------------------------------------------
535
536ROOT::RVectorField::RVectorField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField,
537 const ROOT::RFieldDescriptor *emulatedFrom)
538 : ROOT::RFieldBase(fieldName,
539 emulatedFrom ? emulatedFrom->GetTypeName() : "std::vector<" + itemField->GetTypeName() + ">",
540 ROOT::ENTupleStructure::kCollection, false /* isSimple */),
541 fItemSize(itemField->GetValueSize()),
542 fNWritten(0)
543{
544 if (itemField->GetAlignment() > kMaxItemAlignment) {
545 throw RException(
546 R__FAIL(std::string("Unsupported vector item alignment: ") + std::to_string(itemField->GetAlignment())));
547 }
548
549 if (emulatedFrom && !emulatedFrom->GetTypeName().empty()) {
551 if (emulatedFrom->IsSoACollection())
553 fEmulatedTypeVersion = emulatedFrom->GetTypeVersion();
554 fEmulatedTypeChecksum = emulatedFrom->GetTypeChecksum().value_or(0);
555 }
556
557 if (!itemField->GetTypeAlias().empty())
558 fTypeAlias = "std::vector<" + itemField->GetTypeAlias() + ">";
559
560 if (!(itemField->GetTraits() & kTraitTriviallyDestructible))
561 fItemDeleter = GetDeleterOf(*itemField);
562 Attach(std::move(itemField), "_0");
563}
564
565ROOT::RVectorField::RVectorField(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField)
566 : RVectorField(fieldName, std::move(itemField), nullptr)
567{
569}
570
571std::unique_ptr<ROOT::RVectorField>
572ROOT::RVectorField::CreateUntyped(std::string_view fieldName, std::unique_ptr<RFieldBase> itemField)
573{
575 return std::unique_ptr<ROOT::RVectorField>(new RVectorField(fieldName, itemField->Clone("_0"), &desc));
576}
577
578std::unique_ptr<ROOT::RFieldBase> ROOT::RVectorField::CloneImpl(std::string_view newName) const
579{
580 auto newItemField = fSubfields[0]->Clone(fSubfields[0]->GetFieldName());
581 auto isUntyped = GetTypeName().empty() || ((fTraits & kTraitEmulatedField) != 0);
583 if (isUntyped) {
585 desc = builder.TypeVersion(GetTypeVersion())
586 .TypeChecksum(GetTypeChecksum())
587 .TypeName(GetTypeName())
588 // we need an Id for MoveDescriptor to succeed, but we're not going to use it.
589 .FieldId(42)
590 // we also need a Structure for the same reason.
593 .Unwrap();
594 }
595 return std::unique_ptr<ROOT::RVectorField>(
596 new RVectorField(newName, std::move(newItemField), isUntyped ? &desc : nullptr));
597}
598
599std::size_t ROOT::RVectorField::AppendImpl(const void *from)
600{
601 auto typedValue = static_cast<const std::vector<char> *>(from);
602 // The order is important here: Profiling showed that the integer division is on the critical path. By moving the
603 // computation of count before R__ASSERT, the compiler can use the result of a single instruction (on x86) also for
604 // the modulo operation. Otherwise, it must perform the division twice because R__ASSERT expands to an external call
605 // of Fatal() in case of failure, which could have side effects that the compiler cannot analyze.
606 auto count = typedValue->size() / fItemSize;
607 R__ASSERT((typedValue->size() % fItemSize) == 0);
608 std::size_t nbytes = 0;
609
610 if (fSubfields[0]->IsSimple() && count) {
611 GetPrincipalColumnOf(*fSubfields[0])->AppendV(typedValue->data(), count);
612 nbytes += count * GetPrincipalColumnOf(*fSubfields[0])->GetElement()->GetPackedSize();
613 } else {
614 for (unsigned i = 0; i < count; ++i) {
615 nbytes += CallAppendOn(*fSubfields[0], typedValue->data() + (i * fItemSize));
616 }
617 }
618
619 fNWritten += count;
620 fPrincipalColumn->Append(&fNWritten);
621 return nbytes + fPrincipalColumn->GetElement()->GetPackedSize();
622}
623
624void ROOT::RVectorField::ResizeVector(void *vec, std::size_t nItems, std::size_t itemSize, const RFieldBase &itemField,
625 RDeleter *itemDeleter)
626{
627 auto typedValue = static_cast<std::vector<char> *>(vec);
628
629 // See "semantics of reading non-trivial objects" in RNTuple's Architecture.md
630 assert(itemSize > 0);
631 const auto oldNItems = typedValue->size() / itemSize;
632 const auto availNItems = typedValue->capacity() / itemSize;
633 assert((typedValue->size() % itemSize == 0) && (typedValue->capacity() % itemSize == 0));
634
635 const bool canRealloc = availNItems < nItems;
636 bool allDeallocated = false;
637 if (itemDeleter) {
638 allDeallocated = canRealloc;
639 for (std::size_t i = allDeallocated ? 0 : nItems; i < oldNItems; ++i) {
640 itemDeleter->operator()(typedValue->data() + (i * itemSize), true /* dtorOnly */);
641 }
642 }
643
644 // Resize the vector with correct alignment
645 const auto itemAlignment = itemField.GetAlignment();
646 const auto nbytes = nItems * itemSize;
647
648 auto fnAlignedResize = [](auto &vecOfAlignedStorage, std::size_t targetSize) {
649 constexpr auto valueTypeSize = sizeof(typename std::decay_t<decltype(vecOfAlignedStorage)>::value_type);
650 constexpr auto valueTypeAlignment = alignof(typename std::decay_t<decltype(vecOfAlignedStorage)>::value_type);
651 // Ensure that this function is used with RAlignedStorage as a template argument.
652 // In general, the actual (user-defined) item type may have larger size than alignment.
653 static_assert(valueTypeSize == valueTypeAlignment);
654 assert(targetSize % valueTypeAlignment == 0);
655 vecOfAlignedStorage.resize(targetSize / valueTypeSize);
656 };
657
658 static_assert(kMaxItemAlignment == 4096);
659 // clang-format off
660 switch (itemAlignment) {
661 case 1: fnAlignedResize(*static_cast<std::vector<Internal::RAlignedStorage< 1>> *>(vec), nbytes); break;
662 case 2: fnAlignedResize(*static_cast<std::vector<Internal::RAlignedStorage< 2>> *>(vec), nbytes); break;
663 case 4: fnAlignedResize(*static_cast<std::vector<Internal::RAlignedStorage< 4>> *>(vec), nbytes); break;
664 case 8: fnAlignedResize(*static_cast<std::vector<Internal::RAlignedStorage< 8>> *>(vec), nbytes); break;
665 case 16: fnAlignedResize(*static_cast<std::vector<Internal::RAlignedStorage< 16>> *>(vec), nbytes); break;
666 case 32: fnAlignedResize(*static_cast<std::vector<Internal::RAlignedStorage< 32>> *>(vec), nbytes); break;
667 case 64: fnAlignedResize(*static_cast<std::vector<Internal::RAlignedStorage< 64>> *>(vec), nbytes); break;
668 case 128: fnAlignedResize(*static_cast<std::vector<Internal::RAlignedStorage< 128>> *>(vec), nbytes); break;
669 case 256: fnAlignedResize(*static_cast<std::vector<Internal::RAlignedStorage< 256>> *>(vec), nbytes); break;
670 case 512: fnAlignedResize(*static_cast<std::vector<Internal::RAlignedStorage< 512>> *>(vec), nbytes); break;
671 case 1024: fnAlignedResize(*static_cast<std::vector<Internal::RAlignedStorage<1024>> *>(vec), nbytes); break;
672 case 2048: fnAlignedResize(*static_cast<std::vector<Internal::RAlignedStorage<2048>> *>(vec), nbytes); break;
673 case 4096: fnAlignedResize(*static_cast<std::vector<Internal::RAlignedStorage<4096>> *>(vec), nbytes); break;
674 default: throw RException(R__FAIL(std::string("Unsupported alignment: ") + std::to_string(itemAlignment)));
675 }
676 // clang-format on
677
678 if (!(itemField.GetTraits() & kTraitTriviallyConstructible)) {
679 for (std::size_t i = allDeallocated ? 0 : oldNItems; i < nItems; ++i) {
680 CallConstructValueOn(itemField, typedValue->data() + (i * itemSize));
681 }
682 }
683}
684
686{
687 auto typedValue = static_cast<std::vector<char> *>(to);
688
689 ROOT::NTupleSize_t nItems;
690 RNTupleLocalIndex collectionStart;
691 fPrincipalColumn->GetCollectionInfo(globalIndex, &collectionStart, &nItems);
692
693 if (fSubfields[0]->IsSimple()) {
694 typedValue->resize(nItems * fItemSize);
695 if (nItems)
696 GetPrincipalColumnOf(*fSubfields[0])->ReadV(collectionStart, nItems, typedValue->data());
697 return;
698 }
699
700 ResizeVector(to, nItems, fItemSize, *fSubfields[0], fItemDeleter.get());
701
702 for (std::size_t i = 0; i < nItems; ++i) {
703 CallReadOn(*fSubfields[0], collectionStart + i, typedValue->data() + (i * fItemSize));
704 }
705}
706
716
718{
719 GenerateColumnsImpl<ROOT::Internal::RColumnIndex>();
720}
721
723{
724 GenerateColumnsImpl<ROOT::Internal::RColumnIndex>(desc);
725}
726
727std::unique_ptr<ROOT::RFieldBase> ROOT::RVectorField::BeforeConnectPageSource(Internal::RPageSource &pageSource)
728{
729 if (GetOnDiskId() == kInvalidDescriptorId)
730 return nullptr;
731
732 auto descGuard = pageSource.GetSharedDescriptorGuard();
733 const auto &fieldDesc = descGuard->GetFieldDescriptor(GetOnDiskId());
734 if (fieldDesc.GetTypeName().rfind("std::array<", 0) == 0) {
735 auto substitute = std::make_unique<RArrayAsVectorField>(
736 GetFieldName(), fSubfields[0]->Clone(fSubfields[0]->GetFieldName()), fieldDesc.GetNRepetitions());
737 substitute->SetOnDiskId(GetOnDiskId());
738 return substitute;
739 }
740 return nullptr;
741}
742
744{
745 EnsureMatchingOnDiskCollection(desc).ThrowOnError();
746}
747
749{
750 ConstructVector(where, fSubfields[0]->GetAlignment());
751}
752
754 : RDeleter(GetAlignOfVector()), fItemAlignment(itemAlignment)
755{
756}
757
758ROOT::RVectorField::RVectorDeleter::RVectorDeleter(std::size_t itemSize, std::size_t itemAlignment,
759 std::unique_ptr<RDeleter> itemDeleter)
760 : RDeleter(GetAlignOfVector()),
761 fItemSize(itemSize),
762 fItemAlignment(itemAlignment),
763 fItemDeleter(std::move(itemDeleter))
764{
765}
766
767void ROOT::RVectorField::RVectorDeleter::operator()(void *objPtr, bool dtorOnly)
768{
769 auto vecPtr = static_cast<std::vector<char> *>(objPtr);
770 if (fItemDeleter) {
771 assert(fItemSize > 0);
772 auto nItems = vecPtr->size() / fItemSize;
773 assert((vecPtr->size() % fItemSize) == 0);
774 for (std::size_t i = 0; i < nItems; ++i) {
775 fItemDeleter->operator()(vecPtr->data() + (i * fItemSize), true /* dtorOnly */);
776 }
777 }
778
779 static_assert(kMaxItemAlignment == 4096);
780 // clang-format off
781 switch (fItemAlignment) {
782 case 1: std::destroy_at(static_cast<std::vector<Internal::RAlignedStorage< 1>> *>(objPtr)); break;
783 case 2: std::destroy_at(static_cast<std::vector<Internal::RAlignedStorage< 2>> *>(objPtr)); break;
784 case 4: std::destroy_at(static_cast<std::vector<Internal::RAlignedStorage< 4>> *>(objPtr)); break;
785 case 8: std::destroy_at(static_cast<std::vector<Internal::RAlignedStorage< 8>> *>(objPtr)); break;
786 case 16: std::destroy_at(static_cast<std::vector<Internal::RAlignedStorage< 16>> *>(objPtr)); break;
787 case 32: std::destroy_at(static_cast<std::vector<Internal::RAlignedStorage< 32>> *>(objPtr)); break;
788 case 64: std::destroy_at(static_cast<std::vector<Internal::RAlignedStorage< 64>> *>(objPtr)); break;
789 case 128: std::destroy_at(static_cast<std::vector<Internal::RAlignedStorage< 128>> *>(objPtr)); break;
790 case 256: std::destroy_at(static_cast<std::vector<Internal::RAlignedStorage< 256>> *>(objPtr)); break;
791 case 512: std::destroy_at(static_cast<std::vector<Internal::RAlignedStorage< 512>> *>(objPtr)); break;
792 case 1024: std::destroy_at(static_cast<std::vector<Internal::RAlignedStorage<1024>> *>(objPtr)); break;
793 case 2048: std::destroy_at(static_cast<std::vector<Internal::RAlignedStorage<2048>> *>(objPtr)); break;
794 case 4096: std::destroy_at(static_cast<std::vector<Internal::RAlignedStorage<4096>> *>(objPtr)); break;
795 default: throw ROOT::RException(R__FAIL(std::string("Unsupported alignment: ") + std::to_string(fItemAlignment)));
796 }
797 // clang-format on
798
799 RDeleter::operator()(objPtr, dtorOnly);
800}
801
802std::unique_ptr<ROOT::RFieldBase::RDeleter> ROOT::RVectorField::GetDeleter() const
803{
804 if (fItemDeleter)
805 return std::make_unique<RVectorDeleter>(fItemSize, fSubfields[0]->GetAlignment(), GetDeleterOf(*fSubfields[0]));
806 return std::make_unique<RVectorDeleter>(fSubfields[0]->GetAlignment());
807}
808
809std::vector<ROOT::RFieldBase::RValue> ROOT::RVectorField::SplitValue(const RValue &value) const
810{
811 return SplitVector(value.GetPtr<void>(), *fSubfields[0]);
812}
813
815{
816 return GetSizeOfVector();
817}
818
820{
821 return GetAlignOfVector();
822}
823
825{
826 visitor.VisitVectorField(*this);
827}
828
829//------------------------------------------------------------------------------
830
831ROOT::RField<std::vector<bool>>::RField(std::string_view name)
832 : ROOT::RFieldBase(name, "std::vector<bool>", ROOT::ENTupleStructure::kCollection, false /* isSimple */)
833{
834 Attach(std::make_unique<RField<bool>>("_0"));
835}
836
837std::size_t ROOT::RField<std::vector<bool>>::AppendImpl(const void *from)
838{
839 auto typedValue = static_cast<const std::vector<bool> *>(from);
840 auto count = typedValue->size();
841 for (unsigned i = 0; i < count; ++i) {
842 bool bval = (*typedValue)[i];
843 CallAppendOn(*fSubfields[0], &bval);
844 }
845 fNWritten += count;
846 fPrincipalColumn->Append(&fNWritten);
847 return count + fPrincipalColumn->GetElement()->GetPackedSize();
848}
849
850void ROOT::RField<std::vector<bool>>::ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to)
851{
852 auto typedValue = static_cast<std::vector<bool> *>(to);
853
854 if (fOnDiskNRepetitions == 0) {
855 ROOT::NTupleSize_t nItems;
856 RNTupleLocalIndex collectionStart;
857 fPrincipalColumn->GetCollectionInfo(globalIndex, &collectionStart, &nItems);
858 typedValue->resize(nItems);
859 for (std::size_t i = 0; i < nItems; ++i) {
860 bool bval;
861 CallReadOn(*fSubfields[0], collectionStart + i, &bval);
862 (*typedValue)[i] = bval;
863 }
864 } else {
865 typedValue->resize(fOnDiskNRepetitions);
866 for (std::size_t i = 0; i < fOnDiskNRepetitions; ++i) {
867 bool bval;
868 CallReadOn(*fSubfields[0], globalIndex * fOnDiskNRepetitions + i, &bval);
869 (*typedValue)[i] = bval;
870 }
871 }
872}
873
874void ROOT::RField<std::vector<bool>>::ReadInClusterImpl(ROOT::RNTupleLocalIndex localIndex, void *to)
875{
876 auto typedValue = static_cast<std::vector<bool> *>(to);
877
878 if (fOnDiskNRepetitions == 0) {
879 ROOT::NTupleSize_t nItems;
880 RNTupleLocalIndex collectionStart;
881 fPrincipalColumn->GetCollectionInfo(localIndex, &collectionStart, &nItems);
882 typedValue->resize(nItems);
883 for (std::size_t i = 0; i < nItems; ++i) {
884 bool bval;
885 CallReadOn(*fSubfields[0], collectionStart + i, &bval);
886 (*typedValue)[i] = bval;
887 }
888 } else {
889 typedValue->resize(fOnDiskNRepetitions);
890 for (std::size_t i = 0; i < fOnDiskNRepetitions; ++i) {
891 bool bval;
892 CallReadOn(*fSubfields[0], localIndex * fOnDiskNRepetitions + i, &bval);
893 (*typedValue)[i] = bval;
894 }
895 }
896}
897
898const ROOT::RFieldBase::RColumnRepresentations &ROOT::RField<std::vector<bool>>::GetColumnRepresentations() const
899{
900 static RColumnRepresentations representations({{ENTupleColumnType::kSplitIndex64},
904 {{}});
905 return representations;
906}
907
908void ROOT::RField<std::vector<bool>>::GenerateColumns()
909{
910 R__ASSERT(fOnDiskNRepetitions == 0); // fOnDiskNRepetitions must only be used for reading
911 GenerateColumnsImpl<ROOT::Internal::RColumnIndex>();
912}
913
914void ROOT::RField<std::vector<bool>>::GenerateColumns(const ROOT::RNTupleDescriptor &desc)
915{
916 if (fOnDiskNRepetitions == 0)
917 GenerateColumnsImpl<ROOT::Internal::RColumnIndex>(desc);
918}
919
920void ROOT::RField<std::vector<bool>>::ReconcileOnDiskField(const RNTupleDescriptor &desc)
921{
922 const auto &fieldDesc = desc.GetFieldDescriptor(GetOnDiskId());
923
924 if (fieldDesc.GetTypeName().rfind("std::array<", 0) == 0) {
925 EnsureMatchingOnDiskField(desc, kDiffTypeName | kDiffStructure | kDiffNRepetitions).ThrowOnError();
926
927 if (fieldDesc.GetNRepetitions() == 0) {
928 throw RException(R__FAIL("fixed-size array --> std::vector<bool>: expected repetition count > 0\n" +
929 Internal::GetTypeTraceReport(*this, desc)));
930 }
931 if (fieldDesc.GetStructure() != ENTupleStructure::kPlain) {
932 throw RException(R__FAIL("fixed-size array --> std::vector<bool>: expected plain on-disk field\n" +
933 Internal::GetTypeTraceReport(*this, desc)));
934 }
935 fOnDiskNRepetitions = fieldDesc.GetNRepetitions();
936 } else {
937 EnsureMatchingOnDiskCollection(desc).ThrowOnError();
938 }
939}
940
941std::vector<ROOT::RFieldBase::RValue> ROOT::RField<std::vector<bool>>::SplitValue(const RValue &value) const
942{
943 const auto &typedValue = value.GetRef<std::vector<bool>>();
944 auto count = typedValue.size();
945 std::vector<RValue> result;
946 result.reserve(count);
947 for (unsigned i = 0; i < count; ++i) {
948 if (typedValue[i]) {
949 result.emplace_back(fSubfields[0]->BindValue(std::shared_ptr<bool>(new bool(true))));
950 } else {
951 result.emplace_back(fSubfields[0]->BindValue(std::shared_ptr<bool>(new bool(false))));
952 }
953 }
954 return result;
955}
956
957void ROOT::RField<std::vector<bool>>::AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const
958{
959 visitor.VisitVectorBoolField(*this);
960}
961
962//------------------------------------------------------------------------------
963
964ROOT::RArrayAsRVecField::RArrayAsRVecField(std::string_view fieldName, std::unique_ptr<ROOT::RFieldBase> itemField,
965 std::size_t arrayLength)
966 : ROOT::RFieldBase(fieldName, "ROOT::VecOps::RVec<" + itemField->GetTypeName() + ">",
967 ROOT::ENTupleStructure::kCollection, false /* isSimple */),
968 fItemSize(itemField->GetValueSize()),
969 fArrayLength(arrayLength)
970{
971 if (!itemField->GetTypeAlias().empty())
972 fTypeAlias = "ROOT::VecOps::RVec<" + itemField->GetTypeAlias() + ">";
973 Attach(std::move(itemField), "_0");
974 fValueSize =
978}
979
980std::unique_ptr<ROOT::RFieldBase> ROOT::RArrayAsRVecField::CloneImpl(std::string_view newName) const
981{
982 auto newItemField = fSubfields[0]->Clone(fSubfields[0]->GetFieldName());
983 return std::make_unique<RArrayAsRVecField>(newName, std::move(newItemField), fArrayLength);
984}
985
987{
988 // initialize data members fBegin, fSize, fCapacity
989 // currently the inline buffer is left uninitialized
990 void **beginPtr = new (where)(void *)(nullptr);
991 std::int32_t *sizePtr = new (static_cast<void *>(beginPtr + 1)) std::int32_t(0);
992 new (sizePtr + 1) std::int32_t(-1);
993}
994
995std::unique_ptr<ROOT::RFieldBase::RDeleter> ROOT::RArrayAsRVecField::GetDeleter() const
996{
997 if (fItemDeleter) {
998 return std::make_unique<RRVecField::RRVecDeleter>(fSubfields[0]->GetAlignment(), fItemSize,
999 GetDeleterOf(*fSubfields[0]));
1000 }
1001 return std::make_unique<RRVecField::RRVecDeleter>(fSubfields[0]->GetAlignment());
1002}
1003
1005{
1006 auto begin = RRVecField::ResizeRVec(to, fArrayLength, fItemSize, fSubfields[0].get(), fItemDeleter.get());
1007
1008 if (fSubfields[0]->IsSimple()) {
1009 GetPrincipalColumnOf(*fSubfields[0])->ReadV(globalIndex * fArrayLength, fArrayLength, begin);
1010 return;
1011 }
1012
1013 // Read the new values into the collection elements
1014 for (std::size_t i = 0; i < fArrayLength; ++i) {
1015 CallReadOn(*fSubfields[0], globalIndex * fArrayLength + i, begin + (i * fItemSize));
1016 }
1017}
1018
1020{
1021 auto begin = RRVecField::ResizeRVec(to, fArrayLength, fItemSize, fSubfields[0].get(), fItemDeleter.get());
1022
1023 if (fSubfields[0]->IsSimple()) {
1024 GetPrincipalColumnOf(*fSubfields[0])->ReadV(localIndex * fArrayLength, fArrayLength, begin);
1025 return;
1026 }
1027
1028 // Read the new values into the collection elements
1029 for (std::size_t i = 0; i < fArrayLength; ++i) {
1030 CallReadOn(*fSubfields[0], localIndex * fArrayLength + i, begin + (i * fItemSize));
1031 }
1032}
1033
1035{
1036 EnsureMatchingOnDiskField(desc, kDiffTypeName | kDiffStructure | kDiffNRepetitions).ThrowOnError();
1037 const auto &fieldDesc = desc.GetFieldDescriptor(GetOnDiskId());
1038 if (fieldDesc.GetTypeName().rfind("std::array<", 0) != 0) {
1039 throw RException(R__FAIL("RArrayAsRVecField " + GetQualifiedFieldName() + " expects an on-disk array field\n" +
1040 Internal::GetTypeTraceReport(*this, desc)));
1041 }
1042}
1043
1045{
1046 return Internal::EvalRVecAlignment(fSubfields[0]->GetAlignment());
1047}
1048
1049std::vector<ROOT::RFieldBase::RValue> ROOT::RArrayAsRVecField::SplitValue(const ROOT::RFieldBase::RValue &value) const
1050{
1051 auto arrayPtr = value.GetPtr<unsigned char>().get();
1052 std::vector<ROOT::RFieldBase::RValue> result;
1053 result.reserve(fArrayLength);
1054 for (unsigned i = 0; i < fArrayLength; ++i) {
1055 result.emplace_back(
1056 fSubfields[0]->BindValue(std::shared_ptr<void>(value.GetPtr<void>(), arrayPtr + (i * fItemSize))));
1057 }
1058 return result;
1059}
1060
1065
1066//------------------------------------------------------------------------------
1067
1068ROOT::RArrayAsVectorField::RArrayAsVectorField(std::string_view fieldName, std::unique_ptr<ROOT::RFieldBase> itemField,
1069 std::size_t arrayLength)
1070 : ROOT::RFieldBase(fieldName, "std::vector<" + itemField->GetTypeName() + ">", ROOT::ENTupleStructure::kCollection,
1071 false /* isSimple */),
1072 fItemSize(itemField->GetValueSize()),
1073 fArrayLength(arrayLength)
1074{
1075 if (!itemField->GetTypeAlias().empty())
1076 fTypeAlias = "std::vector<" + itemField->GetTypeAlias() + ">";
1077 Attach(std::move(itemField), "_0");
1080}
1081
1082std::unique_ptr<ROOT::RFieldBase> ROOT::RArrayAsVectorField::CloneImpl(std::string_view newName) const
1083{
1084 auto newItemField = fSubfields[0]->Clone(fSubfields[0]->GetFieldName());
1085 return std::make_unique<RArrayAsVectorField>(newName, std::move(newItemField), fArrayLength);
1086}
1087
1089{
1090 throw RException(R__FAIL("RArrayAsVectorField fields must only be used for reading"));
1091}
1092
1094{
1095 ConstructVector(where, fSubfields[0]->GetAlignment());
1096}
1097
1098std::unique_ptr<ROOT::RFieldBase::RDeleter> ROOT::RArrayAsVectorField::GetDeleter() const
1099{
1100 if (fItemDeleter) {
1101 return std::make_unique<RVectorField::RVectorDeleter>(fItemSize, fSubfields[0]->GetAlignment(),
1102 GetDeleterOf(*fSubfields[0]));
1103 }
1104 return std::make_unique<RVectorField::RVectorDeleter>(fSubfields[0]->GetAlignment());
1105}
1106
1108{
1109 auto typedValue = static_cast<std::vector<char> *>(to);
1110
1111 if (fSubfields[0]->IsSimple()) {
1112 typedValue->resize(fArrayLength * fItemSize);
1113 GetPrincipalColumnOf(*fSubfields[0])->ReadV(globalIndex * fArrayLength, fArrayLength, typedValue->data());
1114 return;
1115 }
1116
1117 RVectorField::ResizeVector(to, fArrayLength, fItemSize, *fSubfields[0], fItemDeleter.get());
1118
1119 for (std::size_t i = 0; i < fArrayLength; ++i) {
1120 CallReadOn(*fSubfields[0], globalIndex * fArrayLength + i, typedValue->data() + (i * fItemSize));
1121 }
1122}
1123
1125{
1126 auto typedValue = static_cast<std::vector<char> *>(to);
1127
1128 if (fSubfields[0]->IsSimple()) {
1129 typedValue->resize(fArrayLength * fItemSize);
1130 GetPrincipalColumnOf(*fSubfields[0])->ReadV(localIndex * fArrayLength, fArrayLength, typedValue->data());
1131 return;
1132 }
1133
1134 RVectorField::ResizeVector(to, fArrayLength, fItemSize, *fSubfields[0], fItemDeleter.get());
1135
1136 for (std::size_t i = 0; i < fArrayLength; ++i) {
1137 CallReadOn(*fSubfields[0], localIndex * fArrayLength + i, typedValue->data() + (i * fItemSize));
1138 }
1139}
1140
1142{
1143 EnsureMatchingOnDiskField(desc, kDiffTypeName | kDiffStructure | kDiffNRepetitions);
1144
1145 const auto &fieldDesc = desc.GetFieldDescriptor(GetOnDiskId());
1146 if (fieldDesc.GetTypeName().rfind("std::array<", 0) != 0) {
1147 throw RException(R__FAIL("RArrayAsVectorField " + GetQualifiedFieldName() + " expects an on-disk array field\n" +
1148 Internal::GetTypeTraceReport(*this, desc)));
1149 }
1150}
1151
1152std::vector<ROOT::RFieldBase::RValue> ROOT::RArrayAsVectorField::SplitValue(const ROOT::RFieldBase::RValue &value) const
1153{
1154 return SplitVector(value.GetPtr<void>(), *fSubfields[0]);
1155}
1156
1158{
1159 return GetSizeOfVector();
1160}
1161
1163{
1164 return GetAlignOfVector();
1165}
1166
size_t fValueSize
#define R__FAIL(msg)
Short-hand to return an RResult<T> in an error state; the RError is implicitly converted into RResult...
Definition RError.hxx:322
#define f(i)
Definition RSha256.hxx:104
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:139
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
char name[80]
Definition TGX11.cxx:142
#define _(A, B)
Definition cfortran.h:112
Abstract base class for classes implementing the visitor design pattern.
virtual void VisitVectorField(const ROOT::RVectorField &field)
virtual void VisitVectorBoolField(const ROOT::RField< std::vector< bool > > &field)
virtual void VisitRVecField(const ROOT::RRVecField &field)
virtual void VisitArrayAsRVecField(const ROOT::RArrayAsRVecField &field)
virtual void VisitArrayAsVectorField(const ROOT::RArrayAsVectorField &field)
virtual void VisitArrayField(const ROOT::RArrayField &field)
The in-memory representation of a 32bit or 64bit on-disk index column.
A helper class for piece-wise construction of an RFieldDescriptor.
RResult< RFieldDescriptor > MoveDescriptor()
Attempt to make a field descriptor.
RFieldDescriptorBuilder & Structure(const ROOT::ENTupleStructure &structure)
RFieldDescriptorBuilder & TypeVersion(std::uint32_t typeVersion)
Abstract interface to read data from an ntuple.
RSharedDescriptorGuard GetSharedDescriptorGuard() const
Takes the read lock for the descriptor.
std::unique_ptr< RDeleter > fItemDeleter
void ReadInClusterImpl(RNTupleLocalIndex localIndex, void *to) final
RArrayAsRVecField(std::string_view fieldName, std::unique_ptr< RFieldBase > itemField, std::size_t arrayLength)
Constructor of the field.
std::size_t GetAlignment() const final
What alignof(T) for this type returns.
std::unique_ptr< RDeleter > GetDeleter() const final
Returns an RRVecField::RRVecDeleter.
void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
The size of a value of this field, i.e. an RVec.
std::vector< RFieldBase::RValue > SplitValue(const RFieldBase::RValue &value) const final
Creates the list of direct child values given an existing value for this field.
void ReconcileOnDiskField(const RNTupleDescriptor &desc) final
For non-artificial fields, check compatibility of the in-memory field and the on-disk field.
std::size_t GetValueSize() const final
What sizeof(T) for this type returns.
void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final
std::size_t fValueSize
The length of the arrays in this field.
void ConstructValue(void *where) const final
Constructs value in a given location of size at least GetValueSize(). Called by the base class' Creat...
std::unique_ptr< RDeleter > GetDeleter() const final
Returns an RVectorField::RVectorDeleter.
void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final
std::unique_ptr< RDeleter > fItemDeleter
void ReconcileOnDiskField(const RNTupleDescriptor &desc) final
For non-artificial fields, check compatibility of the in-memory field and the on-disk field.
void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final
RArrayAsVectorField(std::string_view fieldName, std::unique_ptr< RFieldBase > itemField, std::size_t arrayLength)
The itemField argument represents the inner item of the on-disk array, i.e.
void GenerateColumns() final
Implementations in derived classes should create the backing columns corresponding to the field type ...
void ReadInClusterImpl(RNTupleLocalIndex localIndex, void *to) final
void ConstructValue(void *where) const final
Constructs value in a given location of size at least GetValueSize(). Called by the base class' Creat...
std::vector< RFieldBase::RValue > SplitValue(const RFieldBase::RValue &value) const final
Creates the list of direct child values given an existing value for this field.
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
The length of the arrays in this field.
std::size_t GetAlignment() const final
What alignof(T) for this type returns.
std::size_t GetValueSize() const final
What sizeof(T) for this type returns.
void operator()(void *objPtr, bool dtorOnly) final
Template specializations for C++ std::array and C-style arrays.
std::unique_ptr< RDeleter > GetDeleter() const final
RArrayField(std::string_view fieldName, std::unique_ptr< RFieldBase > itemField, std::size_t arrayLength)
void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final
void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final
std::size_t AppendImpl(const void *from) final
Operations on values of complex types, e.g.
std::size_t ReadBulkImpl(const RBulkSpec &bulkSpec) final
General implementation of bulk read.
void ReadInClusterImpl(RNTupleLocalIndex localIndex, void *to) final
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
Called by Clone(), which additionally copies the on-disk ID.
void ConstructValue(void *where) const final
Constructs value in a given location of size at least GetValueSize(). Called by the base class' Creat...
void ReconcileOnDiskField(const RNTupleDescriptor &desc) final
For non-artificial fields, check compatibility of the in-memory field and the on-disk field.
std::vector< RValue > SplitValue(const RValue &value) const final
Creates the list of direct child values given an existing value for this field.
Base class for all ROOT issued exceptions.
Definition RError.hxx:78
The list of column representations a field can have.
A functor to release the memory acquired by CreateValue() (memory and constructor).
Points to an object with RNTuple I/O support and keeps a pointer to the corresponding field.
A field translates read and write calls from/to underlying columns to/from tree values.
virtual size_t GetValueSize() const =0
What sizeof(T) for this type returns.
void Attach(std::unique_ptr< RFieldBase > child, std::string_view expectedChildName="")
Add a new subfield to the list of nested fields.
virtual size_t GetAlignment() const =0
What alignof(T) for this type returns.
std::vector< std::unique_ptr< RFieldBase > > fSubfields
Collections and classes own subfields.
static std::unique_ptr< RDeleter > GetDeleterOf(const RFieldBase &other)
std::string GetQualifiedFieldName() const
Returns the field name and parent field names separated by dots (grandparent.parent....
std::uint32_t fTraits
Properties of the type that allow for optimizations of collections of that type.
std::string fTypeAlias
A typedef or using name that was used when creating the field.
bool IsSimple() const
std::uint32_t GetTraits() const
RValue BindValue(std::shared_ptr< void > objPtr)
Creates a value from a memory location with an already constructed object.
@ kTraitEmulatedField
This field is a user defined type that was missing dictionaries and was reconstructed from the on-dis...
@ kTraitTriviallyDestructible
The type is cleaned up just by freeing its memory. I.e. the destructor performs a no-op.
@ kTraitSoACollection
The field represents a collection in SoA layout.
virtual std::size_t ReadBulkImpl(const RBulkSpec &bulkSpec)
General implementation of bulk read.
Metadata stored for every field of an RNTuple.
std::optional< std::uint32_t > GetTypeChecksum() const
std::uint32_t GetTypeVersion() const
const std::string & GetTypeName() const
Classes with dictionaries that can be inspected by TClass.
Definition RField.hxx:342
The on-storage metadata of an RNTuple.
const RFieldDescriptor & GetFieldDescriptor(ROOT::DescriptorId_t fieldId) const
Addresses a column element or field item relative to a particular cluster, instead of a global NTuple...
ROOT::NTupleSize_t GetIndexInCluster() const
void operator()(void *objPtr, bool dtorOnly) final
void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final
std::vector< RValue > SplitValue(const RValue &value) const final
Creates the list of direct child values given an existing value for this field.
void GenerateColumns() final
Implementations in derived classes should create the backing columns corresponding to the field type ...
size_t GetAlignment() const final
What alignof(T) for this type returns.
std::size_t ReadBulkImpl(const RBulkSpec &bulkSpec) final
General implementation of bulk read.
RRVecField(std::string_view fieldName, std::unique_ptr< RFieldBase > itemField)
void ReconcileOnDiskField(const RNTupleDescriptor &desc) final
For non-artificial fields, check compatibility of the in-memory field and the on-disk field.
std::unique_ptr< RFieldBase > BeforeConnectPageSource(ROOT::Internal::RPageSource &pageSource) final
Called by ConnectPageSource() before connecting; derived classes may override this as appropriate,...
size_t GetValueSize() const final
What sizeof(T) for this type returns.
std::size_t AppendImpl(const void *from) final
Operations on values of complex types, e.g.
static unsigned char * ResizeRVec(void *rvec, std::size_t nItems, std::size_t itemSize, const RFieldBase *itemField, RDeleter *itemDeleter)
std::unique_ptr< RDeleter > fItemDeleter
const RColumnRepresentations & GetColumnRepresentations() const final
Implementations in derived classes should return a static RColumnRepresentations object.
RFieldBase * fBulkSubfield
May be a direct PoD subfield or a sub-subfield of a fixed-size array of PoD.
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
Called by Clone(), which additionally copies the on-disk ID.
void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final
void ConstructValue(void *where) const final
Constructs value in a given location of size at least GetValueSize(). Called by the base class' Creat...
std::unique_ptr< RDeleter > GetDeleter() const final
void operator()(void *objPtr, bool dtorOnly) final
Template specializations for C++ std::vector.
static std::unique_ptr< RVectorField > CreateUntyped(std::string_view fieldName, std::unique_ptr< RFieldBase > itemField)
void GenerateColumns() final
Implementations in derived classes should create the backing columns corresponding to the field type ...
std::size_t GetValueSize() const final
What sizeof(T) for this type returns.
std::unique_ptr< RFieldBase > BeforeConnectPageSource(ROOT::Internal::RPageSource &pageSource) final
Called by ConnectPageSource() before connecting; derived classes may override this as appropriate,...
void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final
void ReconcileOnDiskField(const RNTupleDescriptor &desc) final
For non-artificial fields, check compatibility of the in-memory field and the on-disk field.
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
Called by Clone(), which additionally copies the on-disk ID.
static void ResizeVector(void *vec, std::size_t nItems, std::size_t itemSize, const RFieldBase &itemField, RDeleter *itemDeleter)
const RColumnRepresentations & GetColumnRepresentations() const final
Implementations in derived classes should return a static RColumnRepresentations object.
std::size_t AppendImpl(const void *from) final
Operations on values of complex types, e.g.
RVectorField(std::string_view fieldName, std::unique_ptr< RFieldBase > itemField, const ROOT::RFieldDescriptor *emulatedFrom)
Creates a possibly-untyped VectorField.
std::size_t GetAlignment() const final
What alignof(T) for this type returns.
void ConstructValue(void *where) const final
Constructs value in a given location of size at least GetValueSize(). Called by the base class' Creat...
std::vector< RValue > SplitValue(const RValue &value) const final
Creates the list of direct child values given an existing value for this field.
std::unique_ptr< RDeleter > fItemDeleter
static constexpr std::size_t kMaxItemAlignment
Maximum alignment of the vector's value type.
std::unique_ptr< RDeleter > GetDeleter() const final
void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final
std::tuple< unsigned char **, std::int32_t *, std::int32_t * > GetRVecDataMembers(void *rvecPtr)
Retrieve the addresses of the data members of a generic RVec from a pointer to the beginning of the R...
void DestroyRVecWithChecks(std::size_t alignOfT, unsigned char **beginPtr, std::int32_t *capacityPtr)
std::string GetNormalizedInteger(const std::string &intTemplateArg)
Appends 'll' or 'ull' to the where necessary and strips the suffix if not needed.
std::string GetTypeTraceReport(const RFieldBase &field, const RNTupleDescriptor &desc)
Prints the hierarchy of types with their field names and field IDs for the given in-memory field and ...
void EnsureTypedChildField(const RFieldBase &child, const RFieldBase &parent)
Makes sure that the type name is not empty. Used to prevent untyped fields as C++ inner types.
std::size_t EvalRVecAlignment(std::size_t alignOfSubfield)
std::size_t EvalRVecValueSize(std::size_t alignOfT, std::size_t sizeOfT, std::size_t alignOfRVecT)
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
constexpr DescriptorId_t kInvalidDescriptorId
ENTupleStructure
The fields in the RNTuple data model tree can carry different structural information about the type s...
Storage type whose alignment matches AlignT bytes.
Definition BitUtils.hxx:53
Input parameter to RFieldBase::ReadBulk() and RFieldBase::ReadBulkImpl().
RNTupleLocalIndex fFirstIndex
Start of the bulk range.
void * fValues
The destination area, which has to be an array of valid objects of the correct type large enough to h...
std::size_t fCount
Size of the bulk range.
std::vector< unsigned char > * fAuxData
Reference to memory owned by the RBulkValues class.