switch off content cache (low hit rate) to save some memory

This commit is contained in:
Eugene Zhuravlev
2013-11-17 21:39:43 +01:00
parent a673c02979
commit c44230b679
3 changed files with 64 additions and 51 deletions
@@ -44,10 +44,12 @@ class OptimizedFileManager extends DefaultFileManager {
private boolean myUseZipFileIndex;
private final Map<File, Archive> myArchives;
private final Map<File, Boolean> myIsFile = new HashMap<File, Boolean>();
private final Map<InputFileObject, SoftReference<CharBuffer>> myContentCache = new HashMap<InputFileObject, SoftReference<CharBuffer>>();
private final Map<File, File[]> myDirectoryCache = new HashMap<File, File[]>();
public static final File[] NULL_FILE_ARRAY = new File[0];
private static final boolean ourUseContentCache = Boolean.valueOf(System.getProperty("javac.use.content.cache", "false"));
private final Map<InputFileObject, SoftReference<CharBuffer>> myContentCache = ourUseContentCache? new HashMap<InputFileObject, SoftReference<CharBuffer>>() : Collections.<InputFileObject, SoftReference<CharBuffer>>emptyMap();
public OptimizedFileManager() throws Throwable {
super(new Context(), true, null);
final Field archivesField = DefaultFileManager.class.getDeclaredField("archives");
@@ -352,56 +354,36 @@ class OptimizedFileManager extends DefaultFileManager {
}
public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
SoftReference<CharBuffer> r = myContentCache.get(this);
CharBuffer cb = (r == null ? null : r.get());
if (cb == null) {
InputStream in = new FileInputStream(f);
try {
final ByteBuffer bb = makeByteBuffer(in);
JavaFileObject prev = log.useSource(this);
try {
cb = decode(bb, ignoreEncodingErrors);
}
finally {
log.useSource(prev);
}
myByteBufferCache.put(bb); // save for next time
CharBuffer cb;
if (ourUseContentCache) {
SoftReference<CharBuffer> ref = myContentCache.get(this);
cb = (ref != null) ? ref.get() : null;
if (cb == null) {
cb = loadFileContent(ignoreEncodingErrors);
if (!ignoreEncodingErrors) {
myContentCache.put(this, new SoftReference<CharBuffer>(cb));
}
}
finally {
in.close();
}
}
else {
cb = loadFileContent(ignoreEncodingErrors);
}
return cb;
}
//public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
// final String encodingName = getEncodingName();
// SoftReference<CharBuffer> r = myContentCache.get(this);
// CharBuffer cb = (r == null ? null : r.get());
// if (cb == null) {
// InputStream in = new FileInputStream(f);
// try {
// JavaFileObject prev = log.useSource(this);
// try {
// final char[] chars = FileUtil.loadFileText(f, encodingName);
// cb = CharBuffer.wrap(chars);
// }
// finally {
// log.useSource(prev);
// }
// if (!ignoreEncodingErrors) {
// myContentCache.put(this, new SoftReference<CharBuffer>(cb));
// }
// }
// finally {
// in.close();
// }
// }
// return cb;
//}
private CharBuffer loadFileContent(boolean ignoreEncodingErrors) throws IOException {
final InputStream in = new FileInputStream(f);
final ByteBuffer bb = makeByteBuffer(in);
JavaFileObject prev = log.useSource(this);
try {
return decode(bb, ignoreEncodingErrors);
}
finally {
log.useSource(prev);
myByteBufferCache.put(bb); // save for next time
in.close();
}
}
@Override
public boolean equals(Object other) {
@@ -540,7 +522,26 @@ class OptimizedFileManager extends DefaultFileManager {
void put(ByteBuffer x) {
myCached.set(x);
}
void clear() {
myCached.set(null);
}
}
private final ByteBufferCache myByteBufferCache = new ByteBufferCache();
public void close() {
try {
super.close();
}
finally {
// archives are cleared in super.close()
if (ourUseContentCache) {
myContentCache.clear();
}
myDirectoryCache.clear();
myByteBufferCache.clear();
myIsFile.clear();
}
}
}
@@ -52,7 +52,8 @@ class OptimizedFileManager17 extends com.sun.tools.javac.file.JavacFileManager {
private static final boolean isOS2 = _OS_NAME.startsWith("os/2") || _OS_NAME.startsWith("os2");
private static final boolean isMac = _OS_NAME.startsWith("mac");
private static final boolean isFileSystemCaseSensitive = !isWindows && !isOS2 && !isMac;
private static final boolean ourUseContentCache = Boolean.valueOf(System.getProperty("javac.use.content.cache", "false"));
public OptimizedFileManager17() throws Throwable {
super(new Context(), true, null);
final Field archivesField = com.sun.tools.javac.file.JavacFileManager.class.getDeclaredField("archives");
@@ -386,22 +387,24 @@ class OptimizedFileManager17 extends com.sun.tools.javac.file.JavacFileManager {
}
public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
CharBuffer cb = fileManager.getCachedContent(this);
CharBuffer cb = ourUseContentCache? fileManager.getCachedContent(this) : null;
if (cb == null) {
InputStream in = new FileInputStream(file);
try {
ByteBuffer bb = fileManager.makeByteBuffer(in);
JavaFileObject prev = fileManager.log.useSource(this);
final ByteBuffer bb = fileManager.makeByteBuffer(in);
final JavaFileObject prev = fileManager.log.useSource(this);
try {
cb = fileManager.decode(bb, ignoreEncodingErrors);
} finally {
}
finally {
fileManager.log.useSource(prev);
}
fileManager.recycleByteBuffer(bb);
if (!ignoreEncodingErrors) {
if (ourUseContentCache && !ignoreEncodingErrors) {
fileManager.cache(this, cb);
}
} finally {
}
finally {
in.close();
}
}
@@ -409,5 +412,14 @@ class OptimizedFileManager17 extends com.sun.tools.javac.file.JavacFileManager {
}
}
public void close() {
try {
super.close();
}
finally {
// archives are cleared in super.close()
myDirectoryCache.clear();
myIsFile.clear();
}
}
}
Binary file not shown.