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
Fixing an O(N^2) scaling problem caused by TTree::Draw()
 * TTreeFormala::GetRealInstance repeatedly went trough a while loop
   that was trasversed again and again for each new query
   (with lineary increasing instances).
   This led to a real problem when drawing on a branch that consistent
   of many data objects (themselves containing small variable sized containers).

 * The problem is fixed by caching the state of a previous call.
  • Loading branch information
sawenzel committed Jun 21, 2017
commit c172e229906f34bf8544555c36f9cd20bef3d733
10 changes: 10 additions & 0 deletions tree/treeplayer/inc/TTreeFormula.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ friend class TTreeFormulaManager;
kMaxIf = 205
};

// Helper struct to hold a cache
// that can accelerate calculation of the RealIndex.
struct RealInstanceCache {
Int_t fInstanceCache = 0;
Int_t fLocalIndexCache = 0;
Int_t fVirtAccumCache = 0;
};

TTree *fTree; //! pointer to Tree
Int_t fCodes[kMAXCODES]; // List of leaf numbers referenced in formula
Int_t fNdata[kMAXCODES]; //! This caches the physical number of element in the leaf or datamember.
Expand Down Expand Up @@ -121,6 +129,8 @@ friend class TTreeFormulaManager;

LongDouble_t* fConstLD; //! local version of fConsts able to store bigger numbers

RealInstanceCache fRealInstanceCache; //! Cache accelerating the GetRealInstance function

TTreeFormula(const char *name, const char *formula, TTree *tree, const std::vector<std::string>& aliases);
void Init(const char *name, const char *formula);
Bool_t BranchHasMethod(TLeaf* leaf, TBranch* branch, const char* method,const char* params, Long64_t readentry) const;
Expand Down
19 changes: 14 additions & 5 deletions tree/treeplayer/src/TTreeFormula.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3412,8 +3412,14 @@ Int_t TTreeFormula::GetRealInstance(Int_t instance, Int_t codeindex) {
}
break;
case -1: {
local_index = 0;
Int_t virt_accum = 0;
if (instance <= fRealInstanceCache.fInstanceCache) {
fRealInstanceCache.fLocalIndexCache = 0;
fRealInstanceCache.fVirtAccumCache = 0;
}
fRealInstanceCache.fInstanceCache = instance;
local_index = fRealInstanceCache.fLocalIndexCache;
Int_t virt_accum = fRealInstanceCache.fVirtAccumCache;

Int_t maxloop = fManager->fCumulUsedVarDims->GetSize();
if (maxloop == 0) {
local_index--;
Expand All @@ -3424,12 +3430,15 @@ Int_t TTreeFormula::GetRealInstance(Int_t instance, Int_t codeindex) {
virt_accum += fManager->fCumulUsedVarDims->GetArray()[local_index];
local_index++;
} while( instance >= virt_accum && local_index<maxloop);
if (local_index==maxloop && (instance >= virt_accum)) {
local_index--;
local_index--;
// update the cache
fRealInstanceCache.fVirtAccumCache = virt_accum - fManager->fCumulUsedVarDims->GetArray()[local_index];
fRealInstanceCache.fLocalIndexCache = local_index;

if (local_index==(maxloop-1) && (instance >= virt_accum)) {
instance = fNdata[0]+1; // out of bounds.
if (check) return fNdata[0]+1;
} else {
local_index--;
if (fManager->fCumulUsedVarDims->At(local_index)) {
instance -= (virt_accum - fManager->fCumulUsedVarDims->At(local_index));
} else {
Expand Down