Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[performance] Avoid reading SourceFile twice #2910

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,15 @@ protected void reportWorked(int workIncrement, int currentUnitIndex) {
}

public void compile(ICompilationUnit[] sourceUnits) {
compile(sourceUnits, false);
try {
compile(sourceUnits, false);
} finally {
for (ICompilationUnit cu:sourceUnits) {
if (cu != null) {
cu.releaseContent();
}
}
}
}
/**
* General API
Expand Down Expand Up @@ -851,30 +859,31 @@ protected void internalBeginToCompile(ICompilationUnit[] sourceUnits, int maxUni
for (int i = 0; i < maxUnits; i++) {
CompilationResult unitResult = null;
try {
ICompilationUnit sourceUnit = sourceUnits[i];
if (this.options.verbose) {
this.out.println(
Messages.bind(Messages.compilation_request,
new String[] {
String.valueOf(i + 1),
String.valueOf(maxUnits),
new String(sourceUnits[i].getFileName())
new String(sourceUnit.getFileName())
}));
}
// diet parsing for large collection of units
CompilationUnitDeclaration parsedUnit;
unitResult = new CompilationResult(sourceUnits[i], i, maxUnits, this.options.maxProblemsPerUnit);
unitResult = new CompilationResult(sourceUnit, i, maxUnits, this.options.maxProblemsPerUnit);
long parseStart = System.currentTimeMillis();
if (this.totalUnits < this.parseThreshold) {
parsedUnit = this.parser.parse(sourceUnits[i], unitResult);
parsedUnit = this.parser.parse(sourceUnit, unitResult);
} else {
parsedUnit = this.parser.dietParse(sourceUnits[i], unitResult);
parsedUnit = this.parser.dietParse(sourceUnit, unitResult);
}
long resolveStart = System.currentTimeMillis();
this.stats.parseTime += resolveStart - parseStart;
// initial type binding creation
this.lookupEnvironment.buildTypeBindings(parsedUnit, null /*no access restriction*/);
this.stats.resolveTime += System.currentTimeMillis() - resolveStart;
addCompilationUnit(sourceUnits[i], parsedUnit);
addCompilationUnit(sourceUnit, parsedUnit);
Comment on lines +862 to +886
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this method changed?

ImportReference currentPackage = parsedUnit.currentPackage;
if (currentPackage != null) {
unitResult.recordPackageName(currentPackage.tokens);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public interface ICompilationUnit extends IDependent {
* CharOperation.NO_CHAR being the candidate of choice.
*/
char[] getContents();

/** if {@link #getContents()} keeps a cached copy that cache can be released with this method **/
default void releaseContent() {
// nothing
}
/**
* Answer the name of the top level public type.
* For example, {Hashtable}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11693,6 +11693,7 @@ public void getMethodBodies(CompilationUnitDeclaration unit) {
char[] contents = this.readManager != null
? this.readManager.getContents(compilationResult.compilationUnit)
: compilationResult.compilationUnit.getContents();
compilationResult.compilationUnit.releaseContent();
this.scanner.setSource(contents, compilationResult);

if (this.javadocParser != null && this.javadocParser.checkDocComment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*******************************************************************************/
package org.eclipse.jdt.internal.core.builder;

import java.lang.ref.SoftReference;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.CoreException;
Expand All @@ -29,6 +31,7 @@ public class SourceFile implements ICompilationUnit {
ClasspathMultiDirectory sourceLocation;
String initialTypeName;
boolean updateClassFile;
private volatile SoftReference<char[]> contentRef;

public SourceFile(IFile resource, ClasspathMultiDirectory sourceLocation) {
this.resource = resource;
Expand Down Expand Up @@ -80,12 +83,27 @@ String extractTypeName() {

@Override
public char[] getContents() {

SoftReference<char[]> cr = this.contentRef;
if (cr != null) {
char[] cachedContents = cr.get();
if (cachedContents != null) {
return cachedContents;
}
}
char[] contents;
try {
return Util.getResourceContentsAsCharArray(this.resource);
contents = Util.getResourceContentsAsCharArray(this.resource);
} catch (CoreException e) {
throw new AbortCompilation(true, new MissingSourceFileException(this.resource.getFullPath().toString()));
}
// softly cache the result:
this.contentRef = new SoftReference<>(contents);
return contents;
}

@Override
public void releaseContent() {
this.contentRef = null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public char[] getContents() {
return contents;
}

@Override
public void releaseContent() {
this.contentRef = null;
}

@Override
public char[] getFileName() {
return this.fileName;
Expand Down
Loading