optimisation question

Ian Lance Taylor iant@google.com
Thu May 17 18:40:00 GMT 2007


Riccardo Lucchese <riccardo.lucchese@gmail.com> writes:

> I'm working on GTK code; let's say we have some code like:
> 
> widget->privData->color = ...;
> widget->privData->l = ...;
> widget->privData->w = ...;
> widget->privData->foo = ...;
> 
> does gcc already optimise widget->privData saving its value one time and
> than using it? or generated binary just does what is written(literally -
> "reference per reference")?

It depends on the data types.  If it is possible that the assignment
to, e.g., widget->privData->color could change widget->privData itself
(perhaps because widget->privData == widget), then gcc can not
optimize the following load of widget->privData.

> Would it be better to do:
> WidgetPrivData* priv = widget->privData;
> priv->color = ...;
> priv->l = ...;
> priv->w = ...;
> priv->foo = ...;

In some cases, yes.

Ian



More information about the Gcc-help mailing list