Skip to content

Inefficient de-registering of objects when plotting and using TPad #20604

@TomasDado

Description

@TomasDado

Explain what you would like to see improved and how.

Hello,

I did some profiling of the code performance when producing a lot of plots ~ 100k or so (note this is not that unusual in a real analysis).

The relevant code looks like this:

TCanvas c("c","c",800,600);
TPad pad0("pad0","pad0",0,0.30,1,1,0,0,0);

std::unique_ptr<TH1> nominal(...);
nominal->SetDirectory(nullptr);

nominal->Draw()

The code works well but the profiling shows that a lot of time is spent in the deleting/de-registering the objects, see this screenshot from VTune (a simplifed version of the real run)

Image

After a lot of help from @hageboeck, one needs to use the following code to improve the situation:

TObject *obj;

TIter next(pad0.GetListOfPrimitives());
while ((obj = next())) {
  if (auto hist = dynamic_cast<TH1*>(obj)) {
     hist->SetBit(kMustCleanup, false);
  }
  if (auto tl = dynamic_cast<TLatex*>(obj)) {
    tl->SetBit(kMustCleanup, false);
  }
  if (auto tli = dynamic_cast<TLine*>(obj)) {
    tli->SetBit(kMustCleanup, false);
  }
}

As you can see, this is not the most obvious code and it does not even solve the problem completely as there is still bottlenck coming from TPave destructor from TLegend. Using the same technique as above causes segfault.

There is probably no easy solution to this, I just wanted to open this issue so that these inefficiencies are not forgotten for the future updates.

ROOT version

ROOT Version: 6.39.01
Built for linuxx8664gcc on Nov 26 2025, 08:33:23
From heads/master@v6-39-01-296-gfac9b1c307b

Installation method

built from source

Operating system

Ubuntu 25.10

Additional context

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions