Re: Mixing XS with Gtk2::TreeStore




On Dec 27, 2008, at 6:41 AM, Emmanuel Rodriguez wrote:

I have a perl program that displays a tree with a lot of nodes
(18_000) using Gtk2::TreeView and Gtk2::TreeStore. Populating the tree
store takes close to 2 seconds, which I find slow. I want to improve
the speed of the application by rewriting the slow part in XS.

Stop. Custom XS should be a last resort, as it ratchets up the difficulty of maintaining and deploying your application, and is often unnecessary. Let's do some sanity checks first:

a. Are you doing all this adding to the TreeStore while it is hooked up to the TreeView? If so, don't. Every time you insert, remove, or modify a TreeModel's row, it fires a notification so that any Views may update themselves. If you're adding thousands of rows in one go, this will slow you waaay down for no reason. You want to unhook the model from the view, do mass modifications, then rehook the model, like this:

       $model = $view->get_model ();
       $view->set_model (undef);

       fill_model ($model);

       $view->set_model ($model);


b. Are you mirroring a data structure? That is, do you have a TreeStore that you keep in sync with some other perl data structure? If so, you are wasting a lot of memory and time. Either use the TreeStore as your native data store, or write a custom TreeModel that adapts the TreeModel interface to your native data store.


You'll probably find that a. takes care of 90% of your speed problems, or you have different problems that will be solved by b.


If a. doesn't solve your speed problems, and b. is infeasible or you're already doing it and it doesn't help, then it makes sense to dive into the dirty world of custom XS for your application.



By the way, it would really help to see a minimal example that represents what your code is doing.


So I rewrote the code used to fill the tree in a sample C file and it
works fine. But if I wrap that same C code through XS I get
segmentation fault each time that I try to store data into the
Gtk2::TreeStore. In my case both the tree view and the tree store are
created from Perl and manipulated through XS.

If I run the perl interpreter through a debugger I can see that the
segmentation fault occurs when I call
gtk_tree_store_insert_with_values(). What's more strange is that the
stack trace shows me that GTK ends up calling Perl and Gtk2 (the perl
library) functions even though the tree store is empty. Is it possible
to populate a Perl Gtk2::TreeStore from XS?

Yes, we'll call back into perl to handle marshaling of data types while inserting values into the model.


Perhaps I should be adding Perl data types to the tree store?

Your stack trace indicates that you *are* adding Perl data types to your tree. Did you create your store with a column type of 'Glib::Scalar'?


Here's the stack trace produced by gdb, frame #7 is my XS function:

#0  0x080b8f53 in Perl_mg_get ()
#1  0x080da43f in Perl_sv_setsv_flags ()
#2  0x080dadd4 in Perl_newSVsv ()
#3 0xb7c69df7 in gperl_sv_copy () from /usr/lib/perl5/auto/Glib/ Glib.so

perl-Glib sets up a boxed wrapper for SVs which uses gperl_sv_copy() as the copy method.


#4  0xb7c09c12 in boxed_proxy_collect_value (value=0xbf82e9f8,
n_collect_values=1, collect_values=0xbf82e9b8, collect_flags=0)
   at /build/buildd/glib2.0-2.16.4/gobject/gboxed.c:317
#5  0xb72aad53 in gtk_tree_store_set_valist_internal
(tree_store=0x84214d8, iter=0xbf82eaa4, emit_signal=0xbf82ea54,
maybe_need_sort=0xbf82ea50,
   var_args=0xbf82ea88 "\001") at
/build/buildd/gtk+2.0-2.12.9/gtk/gtktreestore.c:948
#6  0xb72acee7 in IA__gtk_tree_store_insert_with_values
(tree_store=0x84214d8, iter=0xbf82eaa4, parent=0x0, position=0)
   at /build/buildd/gtk+2.0-2.12.9/gtk/gtktreestore.c:1441
#7  0xb7c46758 in my_populate_treeview (xargs=0xbf82eafc,
node=0x8612128, parent=0x0, pos=0) at xs/code.c:215
#8  0xb7c46575 in populate_treeview (treeview=0x859d120,
node=0x8603788, namespaces=0x85f8c6c) at xs/code.c:165
#9  0xb7c4382f in XS_Xacobeo__XS_populate_treeview (my_perl=0x8153008,
cv=0x8527418) at lib/Xacobeo/XS.c:92
#10 0x080c22d3 in Perl_pp_entersub ()
#11 0x080c0cab in Perl_runops_standard ()
#12 0x0806727b in perl_run ()
#13 0x08063792 in main ()

All help is welcome.





--
Well done, Android. Aperture Science reminds you that Android Hell is a real place, and you will be sent there at the first sign of defiance.
   -- GlaDOS




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]