# Slicer interactions would benefit from using a BST instead of linear search

**URL:** <https://discourse.slicer.org/t/slicer-interactions-would-benefit-from-using-a-bst-instead-of-linear-search/24245>\
**Category:** Development\
**Created:** [July 9, 2022, 12:07am UTC](https://discourse.slicer.org/t/slicer-interactions-would-benefit-from-using-a-bst-instead-of-linear-search/24245 "2022-07-09T00:07:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [July 9, 2022, 12:07am UTC](https://discourse.slicer.org/t/slicer-interactions-would-benefit-from-using-a-bst-instead-of-linear-search/24245/1 "2022-07-09T00:07:25Z")

</div>

I would like to make the argument that functions like `vtkSlicerMarkupsWidget::ProcessInteractionEvent` could be the reason of bottleneck performance around 100 fiducial points.

Because it has a switch stament with 21 cases:

> <https://github.com/Slicer/Slicer/blob/661e00945f7a1f695e939366bcaf10e9481f2ada/Modules/Loadable/Markups/VTKWidgets/vtkSlicerMarkupsWidget.cxx#L823-L896>

According to math we could get interaction 4 times faster if we used binary search on a Binary Search Tree for the function callbacks instead of switch staments if it was only for this different algorithms complexity.

Also I found this classes to have to more than 6 or 8 case staments:

```auto
vtkMRMLSubjectHierarchyNode::ItemEventCallback
vtkMRMLAbstractThreeDViewDisplayableManager::PassThroughInteractorStyleEvent
vtkMRMLCameraWidget::ProcessInteractionEvent
vtkMRMLSliceIntersectionWidget::ProcessInteractionEvent
vtkMRMLWindowLevelWidget::ProcessInteractionEvent
vtkMRMLAbstractLogic::ProcessMRMLSceneEvents
qMRMLSceneModel::onMRMLSceneEvent
void ITKComputeThresholdFromVTKImage
vtkDiffusionTensorGlyph::RequestData
static void vtkDiffusionTensorMathematicsExecute1Eigen
void vtkDiffusionTensorMathematics::ThreadedRequestData
itk_data_types
vtkAnnotationGlyphSource2D::RequestData
vtkMarkupsGlyphSource2D::RequestData
qMRMLSegmentsModel::onEvent
qMRMLSubjectHierarchyModel::onEvent

```

I would know how to solve this is python with lambdas but I don’t know how to do it in C++.

Comments are welcomed

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [July 9, 2022, 12:14am UTC](https://discourse.slicer.org/t/slicer-interactions-would-benefit-from-using-a-bst-instead-of-linear-search/24245/2 "2022-07-09T00:14:23Z")

</div>

Hi @mau_igna_06 it would definitely be good to make the markups interactions more scalable to handle more points and other entities. I’m skeptical that the switch statement itself is the performance bottleneck since the compiler will make those pretty efficient. But to be sure I suggest you consider using a performance analysis tool. People have had good luck with [VerySleepy on windows](https://github.com/VerySleepy/verysleepy), or similar tools on other platforms. Once we know exactly where the time is being spent we can talk about options for improving the implementations.
