Skip to content

Commit

Permalink
[lang] Refactor the DefaultActionPrototype type in order to limit mem…
Browse files Browse the repository at this point in the history
…ory leaks.

Several tests indicated that DefaultActionPrototype takes 250Mb of heap
space over the 350Mb allocated by the SARL IDE.
This issue is caused by a buffer that is not clear in an appropriate
way. A context, which contains the buffer is added in order to control
the life-time of this buffer.

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Oct 1, 2019
1 parent bcfca13 commit 82edde9
Show file tree
Hide file tree
Showing 9 changed files with 817 additions and 437 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
import org.eclipse.xtext.util.Strings;
import org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference;

import io.sarl.lang.sarl.actionprototype.IActionPrototypeContext;
import io.sarl.lang.sarl.actionprototype.IActionPrototypeProvider;

/** The generator from SARL to the Python language.
*
* @author $Author: sgalland$
Expand Down Expand Up @@ -67,6 +70,8 @@ public class ExtraLanguageGeneratorContext implements IExtraLanguageGeneratorCon

private LightweightTypeReference expectedExpressionType;

private IActionPrototypeContext actionPrototypeContext;

/** Create the context for the given delegate.
*
* @param delegate the delegate.
Expand Down Expand Up @@ -171,6 +176,7 @@ public void setData(String id, Object value) {
@Override
public void clearData() {
this.temporaryData = null;
this.actionPrototypeContext = null;
}

@Override
Expand Down Expand Up @@ -264,4 +270,12 @@ public LightweightTypeReference setExpectedExpressionType(LightweightTypeReferen
return old;
}

@Override
public IActionPrototypeContext getActionPrototypeContext(IActionPrototypeProvider provider) {
if (this.actionPrototypeContext == null) {
this.actionPrototypeContext = provider.createContext();
}
return this.actionPrototypeContext;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
import org.eclipse.xtext.generator.IGeneratorContext;
import org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference;

import io.sarl.lang.sarl.actionprototype.IActionPrototypeContext;
import io.sarl.lang.sarl.actionprototype.IActionPrototypeProvider;

/** The generator from SARL to the Python language.
*
* @author $Author: sgalland$
Expand Down Expand Up @@ -105,6 +108,14 @@ public interface IExtraLanguageGeneratorContext extends IGeneratorContext {
*/
IFileSystemAccess2 getFileSystemAccess();

/** Replies the context for the action prototype provider.
*
* @param provider the provider for creating the context if it was not created.
* @return the context.
* @since 0.10
*/
IActionPrototypeContext getActionPrototypeContext(IActionPrototypeProvider provider);

/** Replies the stored data with the given identifier.
* If the data was not found, the default value is replied.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import io.sarl.lang.sarl.SarlBehaviorUnit;
import io.sarl.lang.sarl.actionprototype.ActionParameterTypes;
import io.sarl.lang.sarl.actionprototype.ActionPrototype;
import io.sarl.lang.sarl.actionprototype.IActionPrototypeContext;
import io.sarl.lang.sarl.actionprototype.IActionPrototypeProvider;

/** Describe generation context.
*
Expand Down Expand Up @@ -135,6 +137,8 @@ abstract class GenerationContext {
*/
private GenerationContext parent;

private IActionPrototypeContext actionPrototypeContext;

/** Construct a information about the generation.
*
* @param owner the object for which the context is created.
Expand Down Expand Up @@ -415,10 +419,25 @@ public boolean isAtLeastJava8() {
return getGeneratorConfig().getJavaSourceVersion().isAtLeast(JavaVersion.JAVA8);
}

/** Get the context for the action prototype provider.
*
* @param provider the provider for creating the context if it was not created.
* @return the context
*/
public IActionPrototypeContext getActionPrototypeContext(IActionPrototypeProvider provider) {
if (this.actionPrototypeContext == null) {
this.actionPrototypeContext = provider.createContext();
}
return this.actionPrototypeContext;
}

/** Release any allocated resource.
*/
public void release() {
if (this.actionPrototypeContext != null) {
this.actionPrototypeContext.release();
this.actionPrototypeContext = null;
}
this.target = null;
this.contextObject = null;
this.generatorConfig = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,6 @@ protected final synchronized GenerationContext openContext(EObject sarlObject, J
final Iterable<Class<? extends XtendMember>> supportedMemberTypes) {
assert type != null;
assert supportedMemberTypes != null;
this.sarlSignatureProvider.clear(type);
final GenerationContext context = new GenerationContext(sarlObject, type) {
@Override
public boolean isSupportedMember(XtendMember member) {
Expand Down Expand Up @@ -1466,6 +1465,7 @@ protected void transform(final XtendConstructor source, final JvmGenericType con

// Generate all the constructor signatures related to the constructor to create.
final InferredPrototype constructorSignatures = this.sarlSignatureProvider.createPrototypeFromSarlModel(
context.getActionPrototypeContext(this.sarlSignatureProvider),
actionKey,
Utils.isVarArg(source.getParameters()), source.getParameters());

Expand Down Expand Up @@ -1631,6 +1631,7 @@ protected void transform(final XtendFunction source, final JvmGenericType contai
// Compute the different action prototypes associated to the action to create.
final boolean isVarArgs = Utils.isVarArg(source.getParameters());
final InferredPrototype actionSignatures = this.sarlSignatureProvider.createPrototypeFromSarlModel(
context.getActionPrototypeContext(this.sarlSignatureProvider),
actionKey,
isVarArgs, source.getParameters());

Expand Down Expand Up @@ -2282,10 +2283,12 @@ protected void appendSyntheticDefaultValuedParameterMethods(

// Retreive the inferred prototype (including the prototypes with optional arguments)
InferredPrototype redefinedPrototype = this.sarlSignatureProvider.getPrototypes(
context.getActionPrototypeContext(this.sarlSignatureProvider),
qualifiedActionName, parameterTypes);
if (redefinedPrototype == null) {
// The original operation was not parsed by the SARL compiler in the current run-time context.
redefinedPrototype = this.sarlSignatureProvider.createPrototypeFromJvmModel(
context.getActionPrototypeContext(this.sarlSignatureProvider),
qualifiedActionName, redefinedOperation.isVarArgs(),
redefinedOperation.getParameters());
}
Expand Down

0 comments on commit 82edde9

Please sign in to comment.