Skip to content

Commit

Permalink
improve laggy subset finalization (#3175)
Browse files Browse the repository at this point in the history
* only call subset_selected_changed_callback for matching subset updates
* optimize handling subset_state changes
* prevent unnecessary state-update
* avoid (some cases of) startswith since can be expensive on some machines
  • Loading branch information
kecnry authored Sep 5, 2024
1 parent 3224c08 commit 8b3f3a1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
14 changes: 8 additions & 6 deletions jdaviz/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ def _on_layers_changed(self, msg):
self.state.layer_icons = {
**self.state.layer_icons,
layer_name: alpha_index(len([ln for ln, ic in self.state.layer_icons.items()
if not ic.startswith('mdi-') and
if not ic[:4] == 'mdi-' and
self._get_assoc_data_parent(ln) is None]))
}

Expand All @@ -621,12 +621,14 @@ def _on_layers_changed(self, msg):
if children_layers is not None:
parent_icon = self.state.layer_icons[layer_name]
for i, child_layer in enumerate(children_layers, start=1):
child_layer_icons[child_layer] = f'{parent_icon}{i}'
if child_layer not in self.state.layer_icons:
child_layer_icons[child_layer] = f'{parent_icon}{i}'

self.state.layer_icons = {
**self.state.layer_icons,
**child_layer_icons
}
if child_layer_icons:
self.state.layer_icons = {
**self.state.layer_icons,
**child_layer_icons
}

def _change_reference_data(self, new_refdata_label, viewer_id=None):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,9 @@ def _aperture_items_changed(self, msg):
return
if not hasattr(self, 'aperture'):
return
orig_labels = [item['label'] for item in msg['old']]
for item in msg['new']:
if item not in msg['old']:
if item['label'] not in orig_labels:
if item.get('type') != 'spatial':
continue
subset_lbl = item.get('label')
Expand Down
12 changes: 6 additions & 6 deletions jdaviz/core/template_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1936,14 +1936,14 @@ def _update_subset(self, subset, attribute=None):
or (subset.label == self.selected))):
# updated the currently selected subset, clear all cache
self._clear_cache()
update_has_subregions = True
selected_has_changed = True
else:
update_has_subregions = False
selected_has_changed = False

if subset.label not in self.labels:
# NOTE: this logic will need to be revisited if generic renaming of subsets is added
# see https://github.com/spacetelescope/jdaviz/pull/1175#discussion_r829372470
if subset.label.startswith('Subset') and self._is_valid_item(subset):
if subset.label[:6] == 'Subset' and self._is_valid_item(subset):
# NOTE: += will not trigger traitlet update
self.items = self.items + [self._subset_to_dict(subset)] # noqa
else:
Expand All @@ -1958,11 +1958,11 @@ def _update_subset(self, subset, attribute=None):
else self._subset_to_dict(subset)
for s in self.items]

if update_has_subregions:
if selected_has_changed:
self._update_has_subregions()

if self._subset_selected_changed_callback is not None:
self._subset_selected_changed_callback()
if self._subset_selected_changed_callback is not None:
self._subset_selected_changed_callback()

def _update_has_subregions(self):
if "selected_has_subregions" in self._plugin_traitlets.keys():
Expand Down

0 comments on commit 8b3f3a1

Please sign in to comment.