Skip to content
Snippets Groups Projects
Commit 52fd3701 authored by Florian Angerer's avatar Florian Angerer
Browse files

Further tried to reduce temporarily generated files from deparsed source by...

Further tried to reduce temporarily generated files from deparsed source by using a hash on the source.
parent dd1de0d1
Branches
No related tags found
No related merge requests found
...@@ -59,6 +59,7 @@ public enum FastROptions { ...@@ -59,6 +59,7 @@ public enum FastROptions {
LoadPackagesNativeCode("Load native code of packages, including builtin packages.", !FastRConfig.ManagedMode), LoadPackagesNativeCode("Load native code of packages, including builtin packages.", !FastRConfig.ManagedMode),
EmitTmpSource("Write deparsed source code to temporary files for better debugging.", true), EmitTmpSource("Write deparsed source code to temporary files for better debugging.", true),
EmitTmpDir("The directory where to allocate temporary files with deparsed source code.", null, true), EmitTmpDir("The directory where to allocate temporary files with deparsed source code.", null, true),
EmitTmpHashed("Use an SHA-256 hash as file name to reduce temporary file creation.", true),
// Promises optimizations // Promises optimizations
EagerEval("If enabled, overrides all other EagerEval switches (see EagerEvalHelper)", false), EagerEval("If enabled, overrides all other EagerEval switches (see EagerEvalHelper)", false),
......
...@@ -11,16 +11,22 @@ ...@@ -11,16 +11,22 @@
*/ */
package com.oracle.truffle.r.runtime; package com.oracle.truffle.r.runtime;
import static java.nio.file.StandardOpenOption.CREATE_NEW;
import static java.nio.file.StandardOpenOption.WRITE;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardOpenOption; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import javax.xml.bind.DatatypeConverter;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.interop.TruffleObject; import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.object.DynamicObject; import com.oracle.truffle.api.object.DynamicObject;
...@@ -321,19 +327,43 @@ public class RDeparse { ...@@ -321,19 +327,43 @@ public class RDeparse {
} }
} }
private static Path tmpDir = null;
private static MessageDigest digest = null;
private Path emitToFile() throws IOException, NoSuchAlgorithmException {
Path path;
if (FastROptions.EmitTmpHashed.getBooleanValue()) {
if (tmpDir == null) {
tmpDir = Files.createTempDirectory("deparse");
}
if (digest == null) {
digest = MessageDigest.getInstance("SHA-256");
}
String printHexBinary = DatatypeConverter.printHexBinary(digest.digest(sb.toString().getBytes()));
path = tmpDir.resolve(printHexBinary + ".r");
} else {
path = Files.createTempFile("deparse-", ".r");
}
if (!Files.exists(path)) {
try (BufferedWriter bw = Files.newBufferedWriter(path, CREATE_NEW, WRITE)) {
bw.write(sb.toString());
}
}
return path;
}
public void fixupSources() { public void fixupSources() {
if (FastROptions.EmitTmpSource.getBooleanValue()) { if (FastROptions.EmitTmpSource.getBooleanValue()) {
try { try {
Path path = Files.createTempFile("deparse-", ".r"); Path path = emitToFile();
try (BufferedWriter bw = Files.newBufferedWriter(path, StandardOpenOption.WRITE)) {
bw.write(sb.toString());
}
Source source = RSource.fromFile(path.toFile()); Source source = RSource.fromFile(path.toFile());
for (SourceSectionElement s : sources) { for (SourceSectionElement s : sources) {
s.element.setSourceSection(source.createSection(s.start, s.length)); s.element.setSourceSection(source.createSection(s.start, s.length));
} }
} catch (IOException e) { } catch (IOException e) {
RInternalError.reportError(e); RInternalError.reportError(e);
} catch (NoSuchAlgorithmException e) {
throw RInternalError.shouldNotReachHere("SHA-256 is an unknown algorithm");
} }
} else { } else {
Source source = RSource.fromTextInternal(sb.toString(), RSource.Internal.DEPARSE); Source source = RSource.fromTextInternal(sb.toString(), RSource.Internal.DEPARSE);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment