Skip to content

VTK algorithms' dataset cache: an attribute of the VTK dataset's wrapper, no observer - #340

Merged
berkgeveci merged 3 commits into
Kitware:mainfrom
berkgeveci:vis/vtk-cache-attribute
Nov 5, 2026
Merged

berkgeveci merged 3 commits into
Kitware:mainfrom
berkgeveci:vis/vtk-cache-attribute

Conversation

@berkgeveci

Copy link
Copy Markdown
Collaborator

Stacked on #301 (its commits show here until it merges; this merges after it).

The cache of Tack datasets per VTK dataset (one Tack dataset per VTK dataset, so locators, faces and point links are built once) kept entries by address and dropped them from a DeleteEvent observer. #301 kept those observers reachable to work around VTK bugs 19 (the collector cleared an observer as garbage, then the deletion called it). This replaces the observers altogether: the entry is an attribute of the VTK dataset's Python wrapper. VTK keeps a wrapper's attributes while its C++ object lives, on every later wrapper of it (the one GetInputData gives the next filter), and frees them with the object, so an entry lives exactly as long as its dataset.

Measured on yavin, VTK 9.7.1 (wheel) and VTK master (a local build):

9.7.1 master
a fresh wrapper, 10,000 objects with attributes 50 us 0.6 us
Contour over 1,000 boxes, cached: observer / attribute 2,975 / 2,859 ms 943 / 951 ms
Contour over 5,000 boxes, cached: observer / attribute 61,907 / 57,080 ms 4,817 / 4,803 ms

The two caches cost the same on both; 9.7.1's growth with the box count is its sweep of the attributes the pythonic API gives every wrapper, with either cache.

Staleness is as before: an entry is converted again when the dataset's modification time or time step differs from when it was cached. The comment and the changelog say what that sees (Modified(), arrays added or removed; not values written in place without Modified(), nor InsertNextCell, which VTK's own pipeline would not rerun for either) and ask for Modified() after changing a dataset in place.

Tested: the entry outlives the wrapper and goes with the dataset; it goes in the collector's cycle (VTK bugs 19's case, which crashed before #301); it is stale after Modified() or a new time step, not after a write without it. VTK tests 201 passed on 9.7.1 and 217 on master; 3 MPI ranks; quick tier 3081 passed.

🤖 Generated with Claude Code

…ped at the next use

The cache of Tack datasets per VTK dataset drops an entry from a DeleteEvent
observer, a function only the VTK object referred to. When Python's collector
freed such a dataset in a reference cycle made after it was read (a failing
test's traceback holding its frames), it took the observer for garbage too and
cleared its globals and closure first; the deletion then called it, and Python
segfaulted (_PyEval_EvalFrameDefault from vtkPythonCommand::Execute, from
vtkObject::ObjectFinalize, from the collector). VTK 9.7.1 and master both; a
VTK-only reproduction is VTK bugs 19 in the vault.

The observers are now kept in a module-level dict until they fire, and do no
more than note the address; the cache drops the entry, and the observer, at its
next use, outside whatever freed the dataset. The regression test makes the
cycle after the dataset was read, as the crash needs; it segfaulted before.
… the input

The summaries reduce everything they are given -- every dataset of a partitioned
dataset or a tree, on every MPI rank (the run's world) -- to one answer, as VTK's
vtkDescriptiveStatistics and vtkIntegrateAttributes reduce composite data; every
rank gets it.

- Statistics: a vtkTable, a row per field and a vector field's components one by
  one (ExtractComponent), columns field, where, component, td.statistics' values
  under Tack's names, and the unit.
- Entropy: the same rows, with the entropy in bits over `bins` bins.
- Integrate: vtkIntegrateAttributes' output, one vertex at the measure-weighted
  centroid (the integral of position over the measure), each point field's
  integral as point data and each cell field's as cell data, the measure as
  Length, Area or Volume by the cells' dimension.
- Fields: `fields`, SetInputArrayToProcess, or every field.

Held to td.statistics and td.entropy called directly, Integrate to
vtkIntegrateAttributes (centroid, volume, a point and a vector cell field), pieces
and trees to the whole (each shared point counted once where GhostCells or
GenerateGlobalIds mark its owner), a surface's measure to its area, and 3 MPI
ranks to the whole.
…per, no observer

The cache of Tack datasets per VTK dataset kept entries by address and dropped
them from a DeleteEvent observer, which needed a registry to keep the observers
reachable (VTK bugs 19: the collector cleared one as garbage, then the deletion
called it) and deferred drops. The entry is now an attribute of the dataset's
Python wrapper: VTK keeps a wrapper's attributes while its C++ object lives, on
every later wrapper of it (GetInputData's in the next filter), and frees them with
the object, so an entry lives exactly as long as its dataset with no observer.

Measured (yavin, the wheel and a VTK master build): on master a wrapper costs
0.6 us however many objects keep attributes (10,000), and Contour over 1,000 and
5,000 boxes runs as fast with the attribute as with the observer (951 against 943
ms, 4,803 against 4,817); VTK 9.7.1 sweeps kept attributes at every wrapper (50 us
with 10,000), which it does for the attributes the pythonic API gives every wrapper
anyway, and the two caches cost the same there too.

An entry is stale when the dataset's modification time or time step differs from
when it was cached, as before. The comment says what that sees: Modified() (an
array's, the points'), arrays added or removed; not values written in place without
it, nor InsertNextCell, which VTK's own pipeline would not run again for either.
Tests: the entry outlives the wrapper and goes with the dataset, goes in the
collector's cycle (the crash's case), and is stale after Modified() or a new time
step but not after a write without it.
@berkgeveci berkgeveci closed this Nov 4, 2026
@berkgeveci berkgeveci reopened this Nov 4, 2026
@berkgeveci
berkgeveci merged commit 6a05128 into Kitware:main Nov 5, 2026
61 of 62 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant