diff --git a/src/jni/Constants.cpp b/src/jni/Constants.cpp index 12ccc435b..f51cc86b8 100644 --- a/src/jni/Constants.cpp +++ b/src/jni/Constants.cpp @@ -12,3 +12,4 @@ bool Constants::V8_CACHE_COMPILED_CODE = false; bool Constants::V8_HEAP_SNAPSHOT = false; std::string Constants::V8_STARTUP_FLAGS = ""; std::string Constants::V8_HEAP_SNAPSHOT_SCRIPT = ""; +std::string Constants::V8_HEAP_SNAPSHOT_BLOB = ""; diff --git a/src/jni/Constants.h b/src/jni/Constants.h index 5df755e8b..af5dcc68e 100644 --- a/src/jni/Constants.h +++ b/src/jni/Constants.h @@ -13,6 +13,7 @@ class Constants static std::string APP_ROOT_FOLDER_PATH; static std::string V8_STARTUP_FLAGS; static std::string V8_HEAP_SNAPSHOT_SCRIPT; + static std::string V8_HEAP_SNAPSHOT_BLOB; static bool V8_CACHE_COMPILED_CODE; static bool V8_HEAP_SNAPSHOT; diff --git a/src/jni/Runtime.cpp b/src/jni/Runtime.cpp index b0bb2443b..b5add93b3 100644 --- a/src/jni/Runtime.cpp +++ b/src/jni/Runtime.cpp @@ -135,7 +135,9 @@ void Runtime::Init(jstring filesPath, bool verboseLoggingEnabled, jstring packag Constants::V8_HEAP_SNAPSHOT = (bool)snapshot; JniLocalRef snapshotScript(m_env.GetObjectArrayElement(args, 3)); Constants::V8_HEAP_SNAPSHOT_SCRIPT = ArgConverter::jstringToString(snapshotScript); - JniLocalRef profilerOutputDir(m_env.GetObjectArrayElement(args, 4)); + JniLocalRef snapshotBlob(m_env.GetObjectArrayElement(args, 4)); + Constants::V8_HEAP_SNAPSHOT_BLOB = ArgConverter::jstringToString(snapshotBlob); + JniLocalRef profilerOutputDir(m_env.GetObjectArrayElement(args, 5)); DEBUG_WRITE("Initializing Telerik NativeScript"); @@ -360,8 +362,20 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring packageName, m_startupData = new StartupData(); - auto snapshotPath = filesPath + "/internal/snapshot.blob"; - if( File::Exists(snapshotPath)) + string snapshotPath; + bool saveSnapshot = true; + // we have a precompiled snapshot blob provided - try to load it directly + if (Constants::V8_HEAP_SNAPSHOT_BLOB.size() > 0) + { + snapshotPath = Constants::V8_HEAP_SNAPSHOT_BLOB; + saveSnapshot = false; + } + else + { + snapshotPath = filesPath + "/internal/snapshot.blob"; + } + + if (File::Exists(snapshotPath)) { int length; m_startupData->data = reinterpret_cast(File::ReadBinary(snapshotPath, length)); @@ -369,7 +383,7 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring packageName, DEBUG_WRITE_FORCE("Snapshot read %s (%dB).", snapshotPath.c_str(), length); } - else + else if(saveSnapshot) { // check for custom script to include in the snapshot if(Constants::V8_HEAP_SNAPSHOT_SCRIPT.size() > 0 && File::Exists(Constants::V8_HEAP_SNAPSHOT_SCRIPT)) diff --git a/src/src/com/tns/V8Config.java b/src/src/com/tns/V8Config.java index 5f7de4e93..38ccb7f79 100644 --- a/src/src/com/tns/V8Config.java +++ b/src/src/com/tns/V8Config.java @@ -1,8 +1,11 @@ package com.tns; import java.io.File; + import org.json.JSONObject; +import android.os.Build; + class V8Config { private static final String AndroidKey = "android"; @@ -10,6 +13,8 @@ class V8Config private static final String CodeCacheKey = "codeCache"; private static final String HeapSnapshotKey = "heapSnapshot"; private static final String HeapSnapshotScriptKey = "heapSnapshotScript"; + private static final String HeapSnapshotBlobKey = "heapSnapshotBlob"; + private static final String SnapshotFile = "snapshot.blob"; private static final String ProfilerOutputDirKey = "profilerOutputDir"; public static Object[] fromPackageJSON(File appDir) @@ -45,9 +50,21 @@ public static Object[] fromPackageJSON(File appDir) String value = androidObject.getString(HeapSnapshotScriptKey); result[3] = FileSystem.resolveRelativePath(appDir.getPath(), value, appDir + "/app/"); } + if(androidObject.has(HeapSnapshotBlobKey)) + { + String value = androidObject.getString(HeapSnapshotBlobKey); + String path = FileSystem.resolveRelativePath(appDir.getPath(), value, appDir + "/app/"); + File dir = new File(path); + if(dir.exists() && dir.isDirectory()) + { + // this path is expected to be a directory, containing three sub-directories: armeabi-v7a, x86 and arm64-v8a + path = path + "/" + Build.CPU_ABI + "/" + SnapshotFile; + result[4] = path; + } + } if(androidObject.has(ProfilerOutputDirKey)) { - result[4] = androidObject.getString(ProfilerOutputDirKey); + result[5] = androidObject.getString(ProfilerOutputDirKey); } } } @@ -70,6 +87,8 @@ private static Object[] makeDefaultOptions() false, // arbitrary script to be included in the snapshot "", + // a binary file containing an already saved snapshot + "", // V8 profiler output directory "" };