Re: gvalues
- From: Owen Taylor <otaylor redhat com>
- To: suk <suk artax karlin mff cuni cz>
- Cc: gtk-devel-list gnome org
- Subject: Re: gvalues
- Date: 24 Apr 2003 17:39:26 -0400
On Thu, 2003-04-24 at 12:34, suk wrote:
> I'm writing about minor performance disadvantage when accesing
> style_property values. So it goes:
>
> This code pattern is at several places. Example from GtkButon.c
>
> static const GtkBorder default_default_outside_border = { 0, 0, 0, 0 };
> static const GtkBorder default_default_border = { 1, 1, 1, 1 };
>
> .....
>
> static void gtk_button_get_props (GtkButton *button,
> GtkBorder *default_border,
> GtkBorder *default_outside_border,
> gboolean *interior_focus)
> {
> GtkWidget *widget = GTK_WIDGET (button);
> GtkBorder *tmp_border;
> if (default_border)
> {
> gtk_widget_style_get (widget, "default_border", &tmp_border,NULL);
>
> if (tmp_border)
> {
> *default_border = *tmp_border;
> g_free (tmp_border);
> }
> else
> *default_border = default_default_border;
> }
> ....
>
> This function is called from other code like this:
>
> GtkBorder default_border;
> gtk_button_get_props (button, &default_border, NULL, NULL);
>
>
> This tries to get_style_property and if not found then return some
> fallback value. But it unreasonably allocates/frees memory and copies
> the value twice (branch when property exists) and once otherwise.
> This of course goes against the idea in gtk_style_... to have access
> to values fast.
>
> Variant 1:
>
> So maybe it would be better to have
> boolean gtk_widget_style_get_property (widget, "default_border", &variable );
> instead of
> void gtk_widget_style_get_property (widget, "default_border", &variable );
>
>
> With this we can make it with only one copying (in each branch one).
> if (!gtk_widget_style_get (widget, "default_border", default_border, NULL))
> *default_border = default_default_border;
> So one copying is done inside gtk_widget_style_get
> - but this time, without alocating new struct.
> and if there is no such property, we do copying outside.
Perhaps you are confusing GtkBorder ** and GtkBorder *. There is no
handling of structures _by value_ in GObject, you have to always
deal with pointers to structures. ("Boxed types")
You can think of that call to gtk_widget_get_style() like it
had a signature such as:
gtk_widget_style_get_property (GtkWidget *widget,
const char *property_name
GtkBorder **default_border);
> I don't understand G_VALUES properly, but this would be fairly easy for
> someone who does.
>
> Variant 2:
> Of course we can make no copying at all. Because
>
> void* gtk_widget_style_get_property (widget, "default_border" );
>
> gets the value from style->property_cache, so if we promise to not touch
> and not store for a long time.... This variant will not get through, i'm
> sure.
The reason that we have to return a newly allocated GtkBorder structure
rather than a pointer to something internal is that we always have the
same allocation policy every time we we go through the property
system. You can't say "not making a copy is OK in this case, but in
this case, we need to make a copy you can free".
I don't think this stuff is worth worrying about unless it shows
up on profiles, honestly.
Regards,
Owen
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]