# Improvements to multi-block sources needed

**URL:** <https://discourse.kitware.com/t/improvements-to-multi-block-sources-needed/317>\
**Category:** Design\
**Created:** [September 11, 2019, 10:06pm UTC](https://discourse.kitware.com/t/improvements-to-multi-block-sources-needed/317 "2019-09-11T22:06:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dcthomp](https://discourse.kitware.com/user_avatar/discourse.kitware.com/dcthomp/32/10_2.png) [@dcthomp](https://discourse.kitware.com/u/dcthomp)\
**Post date:** [September 11, 2019, 10:06pm UTC](https://discourse.kitware.com/t/improvements-to-multi-block-sources-needed/317/1 "2019-09-11T22:06:37Z")

</div>

## Problem

The `vtkResourceMultiBlockSource` (and child classes `vtk{Model,Mesh,Attribute}MultiBlockSource`) are in need of some changes to prevent unnecessary regeneration of vtkPolyData objects they store in their output.

Currently, when the VTK source classes listed above are marked modified, they regenerate vtkPolyData objects for **every** component, whether it has been modified or not.

Currently, when model components are modified, operations are expected to update the component’s geometric bounds by calling `smtk::model::Resource::setBoundingBox()` or `smtk::model::Resource::setTessellationAndBoundingBox()`. This will cause an integer property (`SMTK_TESS_GEN_PROP`) to be incremented.

## Proposed Changes

We should use the integer property to improve performance by caching that integer at the time we create a vtkPolyData in vtkModelMultiBlockSource. Here’s a way to do that which should make life easier down the road for all types of resources:

1. Add methods to vtkResourceMultiBlockSource that subclasses can use to (a) provide a data object to cache as a component’s geometric representation and (b) fetch later, provided the generation number is new enough.
2. Remove the cache objects from vtkModelMultiBlockSource and all the logic that deleted the cache each time the multiblock source was marked modified.

The following API would work nicely for caching vtkDataObject instances for components:

```auto
class vtkResourceMultiBlockSource
{
public:
  void SetCachedData(
    const smtk::common::UUID& uid,
    vtkDataObject* data,
    int serialNumber);
  int GetCachedDataSerialNumber(
    const smtk::common::UUID& uid) const;
  vtkDataObject* GetCachedDataObject(
    const smtk::common::UUID& uid);

protected:
  std::map<smtk::common::UUID,
    std::pair<int, vtkSmartPointer<vtkDataObject>>>
      m_cachedData;
};

```

Then, for each model entity inside vtkModelMultiBlockSource, it would be possible to determine whether to use the cache or update it like so:

```auto
  smtk::model::EntityPtr ent;
  smtk::model::ResourcePtr rsrc = ent->modelResource();
  smtk::common::UUID uid = ent->id();
  bool useCache = false;
  int serialNum = 0;
  if (rsrc->hasIntegerProperty(uid, SMTK_TESS_GEN_PROP) &&
    rsrc->integerProperty(uid, SMTK_TESS_GEN_PROP)[0] <=
    (serialNum = this->GetCachedDataSerialNumber(uid)))
  {
    useCache = true;
  }
  if (useCache)
  {
    leaf = this->GetCachedDataObject(uid);
  }
  else
  {
    // populate leaf with data using existing methods
    this->SetCachedData(uid, leaf, serialNum);
  }
  // add leaf to output multiblock in proper spot

```

---

<div class="post-metadata">

**Author:** ![tj.corona](https://discourse.kitware.com/user_avatar/discourse.kitware.com/tj.corona/32/35_2.png) [@tj.corona](https://discourse.kitware.com/u/tj.corona)\
**Post date:** [October 25, 2019, 8:14pm UTC](https://discourse.kitware.com/t/improvements-to-multi-block-sources-needed/317/2 "2019-10-25T20:14:07Z")

</div>

Has this suggestion been implemented?

---

<div class="post-metadata">

**Author:** ![dcthomp](https://discourse.kitware.com/user_avatar/discourse.kitware.com/dcthomp/32/10_2.png) [@dcthomp](https://discourse.kitware.com/u/dcthomp)\
**Post date:** [October 25, 2019, 8:26pm UTC](https://discourse.kitware.com/t/improvements-to-multi-block-sources-needed/317/3 "2019-10-25T20:26:35Z")

</div>

Nope, I’ve been booked since I wrote it.

---

<div class="post-metadata">

**Author:** ![dcthomp](https://discourse.kitware.com/user_avatar/discourse.kitware.com/dcthomp/32/10_2.png) [@dcthomp](https://discourse.kitware.com/u/dcthomp)\
**Post date:** [October 26, 2019, 4:36pm UTC](https://discourse.kitware.com/t/improvements-to-multi-block-sources-needed/317/4 "2019-10-26T16:36:15Z")

</div>

Working on it now to hopefully speed up a demo.

---

<div class="post-metadata">

**Author:** ![dcthomp](https://discourse.kitware.com/user_avatar/discourse.kitware.com/dcthomp/32/10_2.png) [@dcthomp](https://discourse.kitware.com/u/dcthomp)\
**Post date:** [October 26, 2019, 8:03pm UTC](https://discourse.kitware.com/t/improvements-to-multi-block-sources-needed/317/5 "2019-10-26T20:03:10Z")

</div>

See [SMTK MR 1785](https://gitlab.kitware.com/cmb/smtk/merge_requests/1785).
