Home » Modeling » TMF (Xtext) » [CLOSED] How to optimize generated code?
[CLOSED] How to optimize generated code? [message #1737558] |
Sat, 09 July 2016 23:12 |
|
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 |
Ed Willink Messages: 7655 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 |
|
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 |
|
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.
|
|
| | |
Goto Forum:
Current Time: Fri Apr 26 03:35:46 GMT 2024
Powered by FUDForum. Page generated in 0.04048 seconds
|