Skip to content

Commit

Permalink
[memory] SoftReference for ResourceCompilationUnit.contents #1743
Browse files Browse the repository at this point in the history
Ability to reduce memory during searches that find many files

#1743
  • Loading branch information
EcljpseB0T authored and jukzi committed Dec 14, 2023
1 parent a9f1280 commit dcafabb
Showing 1 changed file with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*******************************************************************************/
package org.eclipse.jdt.internal.core.util;

import java.lang.ref.SoftReference;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.compiler.CharOperation;
Expand All @@ -24,7 +26,7 @@
public class ResourceCompilationUnit implements ICompilationUnit {

private final IFile file;
private char[] contents;
private volatile SoftReference<char[]> contentRef;
private final char[] fileName;
private final char[] mainTypeName;
private final char[] module;
Expand All @@ -46,15 +48,22 @@ public ResourceCompilationUnit(IFile file, char[] mod) {

@Override
public char[] getContents() {
if (this.contents != null)
return this.contents; // answer the cached source

// otherwise retrieve it
SoftReference<char[]> cr = this.contentRef;
if (cr != null) {
char[] cachedContents = cr.get();
if (cachedContents != null) {
return cachedContents;
}
}
char[] contents;
try {
return (this.contents = Util.getResourceContentsAsCharArray(this.file));
contents = Util.getResourceContentsAsCharArray(this.file);
} catch (CoreException e) {
return CharOperation.NO_CHAR;
contents = CharOperation.NO_CHAR;
}
// softly cache the result:
this.contentRef = new SoftReference<>(contents);
return contents;
}

@Override
Expand Down

0 comments on commit dcafabb

Please sign in to comment.