Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [CLOSED] How to optimize generated code?
[CLOSED] How to optimize generated code? [message #1737558] Sat, 09 July 2016 23:12 Go to next message
Stéphane Galland is currently offline Stéphane GallandFriend
Messages: 123
Registered: July 2014
Location: Belfort, France
Senior Member
Hi all.

In my DSL, I have:
protected def caller {
     getSkill(C1).myfunction(Integer)
     getSkill(C1).myfunction(Float, 1)
     getSkill(C1).myfunction(String, 2, 3)
}


The generated Java code is:
protected void caller() {
    C1 _skill = this.getSkill(C1.class);
    _skill.myfunction(Integer.class);
    C1 _skill_1 = this.getSkill(C1.class);
    _skill_1.myfunction(Float.class, 1);
    C1 _skill_2 = this.getSkill(C1.class);
    _skill_2.myfunction(String.class, 2, 3);
  }


In the generated code, the three variables _skill, _skill_1, _skill_2 are pointing to the same C1 instance; because there is no setSkill call.

I would like to know if the Xbase expression generator is able to optimize the generated Java code? Is a documentation that explain optimization of the generated code?

At the end I would like to obtain:
protected void caller() {
    C1 _skill = this.getSkill(C1.class);
    _skill.myfunction(Integer.class);
    _skill.myfunction(Float.class, 1);
    _skill.myfunction(String.class, 2, 3);
  }


Thank you.
Stéphane.

[Updated on: Mon, 11 July 2016 11:09]

Report message to a moderator

Re: How to optimize generated code? [message #1737563 is a reply to message #1737558] Sun, 10 July 2016 05:28 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 6990
Registered: July 2009
Senior Member
Hi

In Java, the optimization you request is unsafe and so invalid for two
reasons:

a) getSkill() might have a side effect, e.g. a static counter of the
number of calls
b) there is no synchronize to inhibit a context switch to another thread
that mutates this (e.g. does setSkill())

If you want this CSE optimization to be reliable, you need to use a
technology such as OCL that is transitively side-effect free.

I think that Xbase has some abilities to exploit no side effects, but
they depend on programmer discipline rather than language principles.

Regards

Ed Willink



On 10/07/2016 00:12, Stephane Galland wrote:
> Hi all.
>
> In my DSL, I have:
>
> protected def caller {
> getSkill(C1).myfunction(Integer)
> getSkill(C1).myfunction(Float, 1)
> getSkill(C1).myfunction(String, 2, 3)
> }
>
>
> The generated Java code is:
>
> protected void caller() {
> C1 _skill = this.getSkill(C1.class);
> _skill.myfunction(Integer.class);
> C1 _skill_1 = this.getSkill(C1.class);
> _skill_1.myfunction(Float.class, 1);
> C1 _skill_2 = this.getSkill(C1.class);
> _skill_2.myfunction(String.class, 2, 3);
> }
>
>
> In the generated code, the three variables _skill, _skill_1, _skill_2
> are pointing to the same C1 instance; because there is no setSkill call.
>
> I would like to know if the Xbase expression generator is able to
> optimize the generated Java code? Is a documentation that explain
> optimization of the generated code?
>
> At the end I would like to obtain:
>
> protected void caller() {
> C1 _skill = this.getSkill(C1.class);
> _skill.myfunction(Integer.class);
> _skill.myfunction(Float.class, 1);
> _skill.myfunction(String.class, 2, 3);
> }
>
>
> Thank you.
> Stéphane.
Re: How to optimize generated code? [message #1737595 is a reply to message #1737558] Mon, 11 July 2016 07:01 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 742
Registered: July 2009
Location: Dortmund, Germany
Senior Member

To avoid creation of multiple variables, assign the result of the repeating expression to a constant:
protected def caller {
     val skill = getSkill(C1)
     skill.myfunction(Integer)
     skill.myfunction(Float, 1)
     skill.myfunction(String, 2, 3)
}

Re: How to optimize generated code? [message #1737601 is a reply to message #1737595] Mon, 11 July 2016 07:42 Go to previous messageGo to next message
Stéphane Galland is currently offline Stéphane GallandFriend
Messages: 123
Registered: July 2014
Location: Belfort, France
Senior Member
Dear all.

Thank you for your replies. Indeed, I'm writting my DSL code with a local constant. But some of the users of my DSL do not. It may be helpful to have an optimizer of generated code, as for standard compilers.

Currently, getSkill() is annotated with @Pure. It may help the compiler for its optimization tasks. I will work with my students on an optimizer.

Stéphane.
Re: How to optimize generated code? [message #1737604 is a reply to message #1737601] Mon, 11 July 2016 08:19 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 742
Registered: July 2009
Location: Dortmund, Germany
Senior Member

This sounds reasonable on first sight. Before starting with the actual work, file an issue for Xbase that describes the proposed approach. This will allow the Xtext team to discuss it.

Please file it to:
https://github.com/eclipse/xtext-extras/issues
Re: How to optimize generated code? [message #1737610 is a reply to message #1737604] Mon, 11 July 2016 08:59 Go to previous message
Stéphane Galland is currently offline Stéphane GallandFriend
Messages: 123
Registered: July 2014
Location: Belfort, France
Senior Member
Done: https://github.com/eclipse/xtext-extras/issues/20
Previous Topic:Error markers in the outline
Next Topic:Two projects for one extension
Goto Forum:
  


Current Time: Fri Jan 22 04:45:55 GMT 2021

Powered by FUDForum. Page generated in 0.02288 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top