enthought / enthought/traitsui
Why is TabularEditor slow?
- Dominant language
- Python
- Stars
- 306
- Forks
- 99
- PR merge metrics
- No merged PRs in 30d
Description
Creating a tabular editor which displays 5 items takes 0.3 seconds, of which 70% is spent in the `_TableView.setModel` method.
https://github.com/enthought/traitsui/blob/8c21ab1fe9f526cf4ffcc1901739ca4661a7a450/traitsui/qt4/tabular_editor.py#L719
script to profile tabular editor
```
import cProfile
import os
from importlib_resources import files
from traits.api import HasTraits, Instance, List, Property
from traitsui.api import ModelView, TabularAdapter, TabularEditor, UItem, View
IMAGES_DIR = files('traitsui') / 'images'
class DataContainer(HasTraits):
_data = List()
def add_data(self, data_item):
self._data.append(data_item)
class DataContainerTabularAdapter(TabularAdapter):
columns = [('name', 'name')]
name_image = Property()
def _get_name_image(self):
return os.fspath(IMAGES_DIR / 'frame.png')
name_text = Property()
def _get_name_text(self):
return "test text"
model_view = Instance(ModelView)
class DataContainerModelView(ModelView):
model = Instance(DataContainer)
def default_traits_view(self):
body = UItem(
'model._data',
editor=TabularEditor(
adapter=DataContainerTabularAdapter(model_view=self),
editable=True,
operations=['move'],
show_titles=False,
vertical_lines=False,
horizontal_lines=False,
multi_select=True,
),
springy=True,
)
return View(body, resizable=True)
def main():
profile = cProfile.Profile()
profile.enable()
class Foo(HasTraits):
pass
model = DataContainer()
for _ in range(5):
model.add_data(Foo())
model_view = DataContainerModelView(model=model)
model_view.edit_traits()
profile.disable()
profile.dump_stats(
"C:\\Users\\rporuri\\work\\github\\ets\\traitsui\\temp\\test.prof"
)
if __name__ == '__main__':
main()
```
Ths zip file - [test.zip](https://github.com/enthought/traitsui/files/6310472/test.zip) - contains a single file - the profile data.
```
>>> from pstats import Stats
>>> s = Stats("C:\\Users\\rporuri\\work\\github\\ets\\traitsui\\temp\\test.prof")
>>> s.strip_dirs().sort_stats('cumtime').print_stats(0.025)
Wed Apr 14 16:50:07 2021 C:\Users\rporuri\work\github\ets\traitsui\temp\test.prof
35863 function calls (34827 primitive calls) in 0.312 seconds
Ordered by: cumulative time
List reduced from 739 to 18 due to restriction <0.025>
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.312 0.312 handler.py:417(edit_traits)
1 0.000 0.000 0.309 0.309 view.py:374(ui)
1 0.000 0.000 0.308 0.308 ui.py:228(ui)
1 0.000 0.000 0.308 0.308 toolkit.py:167(ui_live)
1 0.000 0.000 0.297 0.297 ui_live.py:50(ui_live)
1 0.000 0.000 0.297 0.297 ui_live.py:69(_ui_dialog)
1 0.000 0.000 0.297 0.297 ui_base.py:294(display_ui)
1 0.000 0.000 0.248 0.248 ui_live.py:82(init)
1 0.000 0.000 0.240 0.240 ui_panel.py:258(panel)
1 0.000 0.000 0.231 0.231 ui_panel.py:497(__init__)
1 0.000 0.000 0.231 0.231 ui_panel.py:723(_add_items)
1 0.000 0.000 0.225 0.225 editor.py:234(prepare)
1 0.000 0.000 0.225 0.225 tabular_editor.py:144(init)
1 0.000 0.000 0.224 0.224 tabular_editor.py:138()
1 0.005 0.005 0.224 0.224 tabular_editor.py:711(__init__)
1 0.218 0.218 0.218 0.218 {built-in method setModel}
1 0.047 0.047 0.048 0.048 {built-in method show}
13/2 0.000 0.000 0.016 0.008 :966(_find_and_load)
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.