From 639e4194327ce840a903b24b371c0d57560fb4f7 Mon Sep 17 00:00:00 2001 From: Keunhyun Oh Date: Mon, 3 Jan 2022 10:47:36 +0900 Subject: [PATCH] creating temp dir lazily --- .../synapse/ml/core/env/NativeLoader.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/core/src/main/scala/com/microsoft/azure/synapse/ml/core/env/NativeLoader.java b/core/src/main/scala/com/microsoft/azure/synapse/ml/core/env/NativeLoader.java index f7d3d7ccce..f4a6aec65b 100644 --- a/core/src/main/scala/com/microsoft/azure/synapse/ml/core/env/NativeLoader.java +++ b/core/src/main/scala/com/microsoft/azure/synapse/ml/core/env/NativeLoader.java @@ -27,14 +27,21 @@ * */ public class NativeLoader implements Serializable { - private String resourcesPath; + private final String resourcesPath; private Boolean extractionDone = false; - private File tempDir; + private File tempDir = null; - public NativeLoader(String topLevelResourcesPath) throws IOException { + private File getOrCreateTempDir() throws IOException { + if (tempDir == null) { + tempDir = Files.createTempDirectory("mml-natives").toFile(); + tempDir.deleteOnExit(); + } + + return tempDir; + } + + public NativeLoader(String topLevelResourcesPath) { this.resourcesPath = getResourcesPath(topLevelResourcesPath); - tempDir = Files.createTempDirectory("mml-natives").toFile(); - tempDir.deleteOnExit(); } /** @@ -56,7 +63,7 @@ public void loadLibraryByName(String libName) { libName = System.mapLibraryName(libName); extractNativeLibraries(libName); // Try to load library from extracted native resources - System.load(tempDir.getAbsolutePath() + File.separator + libName); + System.load(getOrCreateTempDir().getAbsolutePath() + File.separator + libName); } catch (Exception ee) { throw new UnsatisfiedLinkError(String.format( @@ -106,7 +113,7 @@ private static String getResourcesPath(String topLevelResourcesPath) { private void extractResourceFromPath(String libName, String prefix) throws IOException { - File temp = new File(tempDir.getPath() + File.separator + libName); + File temp = new File(getOrCreateTempDir().getPath() + File.separator + libName); temp.createNewFile(); temp.deleteOnExit();