Re: [Vala] wrong struct code generated



Hello,

On 18 August 2009, 10:14, Spencer, Matthew wrote:
Firstly, there was a bug in my code, it should have been
  public unowned char SourceName[256];

Do you really want a char[], not a string?

I am not sure what this will generate (and don't have valac around
now (at $work) to check), but if it generates

    typedef struct {
        char SourceName[256];
    } TestInitParams

than you have a pretty pathological case (besides unowned would not
make sense for that).

so sorry about that, but the problem still exists.  I also forgot to
mention that I am using vala 0.7.5.

I initially tried without the [SimpleType], but this generates code:
void test_run_main (char** args, int args_length1) {
      TestInitParams _tmp0_ = {0};
      TestInitParams params;
      params = (memset (&_tmp0_, 0, sizeof (TestInitParams)), _tmp0_);
      params = params;
}

Which works, but seems hugely inefficient as it is creating two versions
of the structure on the stack when only one is needed.

For few bytes, which is the normal use-case for structs, it would not
matter that much.

Since the _tmp0_ is not used anymore, the compiler might actually realize
it does not need two variables in the optimization phase. Though because
pointer is taken of both, it probably won't unless it somehow knows
the memset does not store the value anywhere.

The problem is, that when the code generator sees

    TestInitParams params = TestInitParams();

it really sees three things:
 1. declare variable params of type TestInitParams,
 2. create a default value of type TestInitParams,
 3. and assign that value to params.

When doing the second step, it does not know it will be assigning it to
params, so it creates the temporary. And when it generates the third one,
it just takes the string produced by the second step without knowing
anything about it's structure (that's why I mentioned the initializer --
that would be more efficient, because it would simply create a zeroed
piece of static data and copy it over).

To optimize it, it would need to have a special case for assignment of
default value when no constructor is present. You can't really expect
such optimization for rare case from a beta compiler.

This is why I
started investigating use of SimpleType.

[SimpleType] is for cases where the structure is not a structure at all.
Like int or bool and various typedefs of those like GLib.Pid.
That's not your case.

What I have is a simple structure that just needs this style of code:
      TestInitParams params;
      memset (&params, 0, sizeof (TestInitParams);

Is there any way I can get valac to produce code like this?

If you need to produce particular C code, you will have to produce
it yourself. But I don't think you need it. Before you actually finish
your program, profile it and measure, that this extra copying is taking
up several percent of runtime for a typical run, thinking about it is
a premature optimization, also called the root of all evil.

I will also review the bug you submitted and comment there if necessary.

I think it's irrelevant as it's really specific to [SimpleType]s and
you don't have one.

-- 
                                        - Jan Hudec <bulb ucw cz>




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