Skip to content

Commit 318faa0

Browse files
committed
[RF] Don't recreate hash tables in RooLinkedList::Add()
Thanks to #7818, where the `RooHashTable` was migrated to `std::unordered_map`, we can now use proper `reserve()` functions to increase the hash table size when adding elements. The current mechanism deleting and recreating the hash tables from scratch when the list size changed incurred a pretty large overhead. In fact, it made adding to the list a `O(n)` operation, because it has to iterate over the collection to fill the hash table again. Consequently, copying a `RooLinkedList` was `O(n^2)`, which is unexpected to the user. This change will speed up the handling of objects in a RooWorkspace, which uses `RooLinkedLists` to manage the elemens.
1 parent 80ffecd commit 318faa0

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

roofit/roofitcore/src/RooLinkedList.cxx

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -327,33 +327,25 @@ RooLinkedList& RooLinkedList::operator=(const RooLinkedList& other)
327327

328328
void RooLinkedList::setHashTableSize(Int_t size)
329329
{
330-
if (size<0) {
331-
coutE(InputArguments) << "RooLinkedList::setHashTable() ERROR size must be positive" << std::endl ;
332-
return ;
333-
}
334-
if (size==0) {
335-
if (!_htableName) {
336-
// No hash table present
337-
return ;
338-
} else {
330+
if (size < 0) {
331+
coutE(InputArguments) << "RooLinkedList::setHashTable() ERROR size must be positive" << std::endl;
332+
return;
333+
}
334+
if (size == 0) {
339335
// Remove existing hash table
340336
_htableName.reset();
341337
_htableLink.reset();
342-
}
343-
} else {
338+
return;
339+
}
344340

345-
// (Re)create hash tables
346-
_htableName = std::make_unique<HashTableByName>(size) ;
347-
_htableLink = std::make_unique<HashTableByLink>(size) ;
341+
if (!_htableName) {
342+
// (Re)create hash tables
343+
_htableName = std::make_unique<HashTableByName>(size);
344+
_htableLink = std::make_unique<HashTableByLink>(size);
345+
}
348346

349-
// Fill hash table with existing entries
350-
RooLinkedListElem* ptr = _first ;
351-
while(ptr) {
352-
_htableName->insert({ptr->_arg->GetName(), ptr->_arg}) ;
353-
_htableLink->insert({ptr->_arg, reinterpret_cast<TObject*>(ptr)}) ;
354-
ptr = ptr->_next ;
355-
}
356-
}
347+
_htableName->reserve(size);
348+
_htableLink->reserve(size);
357349
}
358350

359351
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)