Re: Overhead of type casting when using GObject system



On 10 February 2013 13:57, Ross Lagerwall <rosslagerwall gmail com> wrote:
> On Sun, Feb 10, 2013 at 11:08:31AM +0000, Emmanuele Bassi wrote:
>> disabling cast checks is usually the result of performance profiling,
>> tho, so it should only be used if you have profiled your application
>> code and verified that type casting is high on the profiles; there are
>> also various ways to avoid that, by using ancillary values and the
>> correct type to minimize casting, before a blanket type cast check
>> disable.
>>
>
> What are ancillary values, and how can you use them to minimize casting?

if you are calling multiple functions of the same type, e.g.

  gtk_widget_set_foo (GTK_WIDGET (w), foo);
  gtk_widget_set_bar (GTK_WIDGET (w), bar);
  gtk_widget_set_baz (GTK_WIDGET (w), baz);

instead of using a checked cast for each call, use an ancillary
variable and cast once:

  GtkWidget *tmp = GTK_WIDGET (w);
  gtk_widget_set_foo (tmp, foo);
  gtk_widget_set_bar (tmp, bar);
  gtk_widget_set_baz (tmp, baz);

there's also the option of using the most common type when calling
functions or inside signal handlers, to minimize type checks; if you
have a function that calls method of the same type, use that type as
part of the arguments, instead of doing a cast (upwards or downwards).

ciao,
 Emmanuele.

-- 
W: http://www.emmanuelebassi.name
B: http://blogs.gnome.org/ebassi/


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