From 52fd370127fa5bec044a924a5c54e99fd32429ea Mon Sep 17 00:00:00 2001 From: Florian Angerer <florian.angerer@oracle.com> Date: Thu, 4 May 2017 15:36:14 +0200 Subject: [PATCH] Further tried to reduce temporarily generated files from deparsed source by using a hash on the source. --- .../truffle/r/runtime/FastROptions.java | 1 + .../oracle/truffle/r/runtime/RDeparse.java | 40 ++++++++++++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/FastROptions.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/FastROptions.java index 9258059341..ad7e272e91 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/FastROptions.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/FastROptions.java @@ -59,6 +59,7 @@ public enum FastROptions { LoadPackagesNativeCode("Load native code of packages, including builtin packages.", !FastRConfig.ManagedMode), 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), + EmitTmpHashed("Use an SHA-256 hash as file name to reduce temporary file creation.", true), // Promises optimizations EagerEval("If enabled, overrides all other EagerEval switches (see EagerEvalHelper)", false), diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java index 147efcf77d..05e90e6a62 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java @@ -11,16 +11,22 @@ */ 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.IOException; import java.nio.file.Files; 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.Arrays; import java.util.HashSet; import java.util.Iterator; +import javax.xml.bind.DatatypeConverter; + import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; import com.oracle.truffle.api.interop.TruffleObject; import com.oracle.truffle.api.object.DynamicObject; @@ -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() { if (FastROptions.EmitTmpSource.getBooleanValue()) { try { - Path path = Files.createTempFile("deparse-", ".r"); - try (BufferedWriter bw = Files.newBufferedWriter(path, StandardOpenOption.WRITE)) { - bw.write(sb.toString()); - } + Path path = emitToFile(); Source source = RSource.fromFile(path.toFile()); for (SourceSectionElement s : sources) { s.element.setSourceSection(source.createSection(s.start, s.length)); } } catch (IOException e) { RInternalError.reportError(e); + } catch (NoSuchAlgorithmException e) { + throw RInternalError.shouldNotReachHere("SHA-256 is an unknown algorithm"); } } else { Source source = RSource.fromTextInternal(sb.toString(), RSource.Internal.DEPARSE); -- GitLab