GtkTreeView 由于排序而导致插入/更新性能损失

发布于 2024-09-15 23:12:02 字数 2471 浏览 7 评论 0原文

在将许多行插入 GTK 树视图(使用 PyGTK)或修改许多行时,我遇到了性能问题。问题是模型似乎在每次更改(插入/修改)后都会被重新使用。这会导致 GUI 挂起数秒。通过注释 model.set_sort_column_id(SOME_ROW_INDEX, gtk.SORT_ASCENDING) 使模型不排序可以消除这些问题。

因此,我想在插入或修改模型时禁用排序,然后重新启用它。遗憾的是,无法使用 model.set_sort_column_id(-1, gtk.SORT_ASCENDING) 禁用排序。使用冻结/解冻功能也不起作用:

treeview.freeze_child_notify()

try:
    for row in model:
        # ... change something in row ...
finally:
    treeview.thaw_child_notify()

那么,如何禁用排序?或者有更好的批量插入/修改方法吗?


解决方案

感谢bobince在他的回答中提供的信息和链接,我检查了一些替代方案:

1)虚拟排序

 tv.freeze_child_notify()
 sortSettings = model.get_sort_column_id()
 model.set_default_sort_func(lambda *unused: 0) # <-- can also use None but that is slower!
 # model.set_default_sort_func(lambda *unused: 1) <-- slow
 # model.set_default_sort_func(lambda *unused: -1) <-- crash (access violation in gtk_tree_store_move_after?!)
 model.set_sort_column_id(-1, gtk.SORT_ASCENDING)
 # change rows
 model.set_sort_column_id(*sortSettings)
 tv.thaw_child_notify()

这将时间从大约11秒缩短到2秒秒。哇!但还可以更好,这仅适用于 1000 行。

2) 更新时删除模型

tv.set_model(None)
# change rows
tv.set_model(model)

没有明显差异,11 秒。

3) 虚拟排序和来自 PyGTK 常见问题解答

 def gen():
      tv.freeze_child_notify()
      sortSettings = model.get_sort_column_id()
      model.set_default_sort_func(lambda *unused: 0)
      model.set_sort_column_id(-1, gtk.SORT_ASCENDING)

      i = 0
      for row in rowsToChange:
           i += 1
           # change something
           if i % 200 == 0:
                # freeze/thaw not really  necessary here as sorting is wrong because of the
                # default sort function
                yield True

      model.set_sort_column_id(*sortSettings)
      tv.thaw_child_notify()
      yield False

 g = gen()
 if g.next(): # run once now, remaining iterations when idle
     gobject.idle_add(g.next)

结果:与解决方案 1) 中的估计时间相同,为 2 秒,但 GUI 在这段时间内做出反应。我更喜欢这种方法。如果需要,可以调整模 200 以使 GUI 或多或少具有反应性。

也许甚至可以对 gtk.TreeStore 进行子类化以获得更好的结果?还没有尝试过,但选项 3) 目前就足够了。

I'm having performance problems when inserting many rows into a GTK treeview (using PyGTK) - or when modifying many rows. The problem is that the model seems to get resorted after each change (insert/modification). This causes the GUI to hang for multiple seconds. Leaving the model unsorted by commenting out model.set_sort_column_id(SOME_ROW_INDEX, gtk.SORT_ASCENDING) eliminates these problems.

Therefore, I would like to disable the sorting while I'm inserting or modifying the model, and re-enable it afterwards. Unfortunately, sorting can't be disabled with model.set_sort_column_id(-1, gtk.SORT_ASCENDING). Using the freeze/thaw functions doesn't work either:

treeview.freeze_child_notify()

try:
    for row in model:
        # ... change something in row ...
finally:
    treeview.thaw_child_notify()

So, how can I disable the sorting? Or is there a better method for bulk inserts/modifications?


Solution

Thanks to the information and links bobince provided in his answer, I checked out some of the alternatives:

1) Dummy sorting

 tv.freeze_child_notify()
 sortSettings = model.get_sort_column_id()
 model.set_default_sort_func(lambda *unused: 0) # <-- can also use None but that is slower!
 # model.set_default_sort_func(lambda *unused: 1) <-- slow
 # model.set_default_sort_func(lambda *unused: -1) <-- crash (access violation in gtk_tree_store_move_after?!)
 model.set_sort_column_id(-1, gtk.SORT_ASCENDING)
 # change rows
 model.set_sort_column_id(*sortSettings)
 tv.thaw_child_notify()

This brought the time down from about 11 seconds to 2 seconds. Wow! But could be better, this was only for 1000 rows.

2) Removing model while updating

tv.set_model(None)
# change rows
tv.set_model(model)

No noticable difference, 11 seconds.

3) Dummy sorting and the cool generator trick from the PyGTK FAQ

 def gen():
      tv.freeze_child_notify()
      sortSettings = model.get_sort_column_id()
      model.set_default_sort_func(lambda *unused: 0)
      model.set_sort_column_id(-1, gtk.SORT_ASCENDING)

      i = 0
      for row in rowsToChange:
           i += 1
           # change something
           if i % 200 == 0:
                # freeze/thaw not really  necessary here as sorting is wrong because of the
                # default sort function
                yield True

      model.set_sort_column_id(*sortSettings)
      tv.thaw_child_notify()
      yield False

 g = gen()
 if g.next(): # run once now, remaining iterations when idle
     gobject.idle_add(g.next)

The result: The same estimated 2 seconds as in solution 1), but the GUI reacts during this time. I prefer this method. The modulo 200 can be tweaked to make the GUI more or less reactive if needed.

Maybe it's even possible to subclass gtk.TreeStore to get better results? Haven't tried that yet, but option 3) is good enough for now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌无敌 2024-09-22 23:12:02

听起来你已经快到了。有关更多说明,请参阅常见问题解答。特别是,您还应该设置 default_sort_order (您现在可以使用 None 以及该示例中的虚拟比较 lambda,以获得更好的性能)以确保没有排序,并在操作期间从树视图中删除模型。

如果有很多更改,您最好创建并设置一个完整的新模型。

Sounds like you're nearly there. See the FAQ for further notes. In particular, you should also set the default_sort_order (you can now use None as well as the dummy compare lambda in that example, for better performance) to ensure there is no sorting, and remove the model from the treeview for the duration of the operations.

If it's a lot of changes you may be better off creating and setting a complete new model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文