Skip to content

Commit 7d727b4

Browse files
atanasovgjasssonpet
authored andcommitted
Add support for the Runtime to consume precompiled snapshot BLOB
1 parent faff6cc commit 7d727b4

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

src/jni/Constants.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ bool Constants::V8_CACHE_COMPILED_CODE = false;
1212
bool Constants::V8_HEAP_SNAPSHOT = false;
1313
std::string Constants::V8_STARTUP_FLAGS = "";
1414
std::string Constants::V8_HEAP_SNAPSHOT_SCRIPT = "";
15+
std::string Constants::V8_HEAP_SNAPSHOT_BLOB = "";

src/jni/Constants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Constants
1313
static std::string APP_ROOT_FOLDER_PATH;
1414
static std::string V8_STARTUP_FLAGS;
1515
static std::string V8_HEAP_SNAPSHOT_SCRIPT;
16+
static std::string V8_HEAP_SNAPSHOT_BLOB;
1617
static bool V8_CACHE_COMPILED_CODE;
1718
static bool V8_HEAP_SNAPSHOT;
1819

src/jni/Runtime.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ void Runtime::Init(jstring filesPath, bool verboseLoggingEnabled, jstring packag
135135
Constants::V8_HEAP_SNAPSHOT = (bool)snapshot;
136136
JniLocalRef snapshotScript(m_env.GetObjectArrayElement(args, 3));
137137
Constants::V8_HEAP_SNAPSHOT_SCRIPT = ArgConverter::jstringToString(snapshotScript);
138-
JniLocalRef profilerOutputDir(m_env.GetObjectArrayElement(args, 4));
138+
JniLocalRef snapshotBlob(m_env.GetObjectArrayElement(args, 4));
139+
Constants::V8_HEAP_SNAPSHOT_BLOB = ArgConverter::jstringToString(snapshotBlob);
140+
JniLocalRef profilerOutputDir(m_env.GetObjectArrayElement(args, 5));
139141

140142
DEBUG_WRITE("Initializing Telerik NativeScript");
141143

@@ -360,16 +362,28 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring packageName,
360362

361363
m_startupData = new StartupData();
362364

363-
auto snapshotPath = filesPath + "/internal/snapshot.blob";
364-
if( File::Exists(snapshotPath))
365+
string snapshotPath;
366+
bool saveSnapshot = true;
367+
// we have a precompiled snapshot blob provided - try to load it directly
368+
if (Constants::V8_HEAP_SNAPSHOT_BLOB.size() > 0)
369+
{
370+
snapshotPath = Constants::V8_HEAP_SNAPSHOT_BLOB;
371+
saveSnapshot = false;
372+
}
373+
else
374+
{
375+
snapshotPath = filesPath + "/internal/snapshot.blob";
376+
}
377+
378+
if (File::Exists(snapshotPath))
365379
{
366380
int length;
367381
m_startupData->data = reinterpret_cast<char*>(File::ReadBinary(snapshotPath, length));
368382
m_startupData->raw_size = length;
369383

370384
DEBUG_WRITE_FORCE("Snapshot read %s (%dB).", snapshotPath.c_str(), length);
371385
}
372-
else
386+
else if(saveSnapshot)
373387
{
374388
// check for custom script to include in the snapshot
375389
if(Constants::V8_HEAP_SNAPSHOT_SCRIPT.size() > 0 && File::Exists(Constants::V8_HEAP_SNAPSHOT_SCRIPT))

src/src/com/tns/V8Config.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package com.tns;
22

33
import java.io.File;
4+
45
import org.json.JSONObject;
56

7+
import android.os.Build;
8+
69
class V8Config
710
{
811
private static final String AndroidKey = "android";
912
private static final String V8FlagsKey = "v8Flags";
1013
private static final String CodeCacheKey = "codeCache";
1114
private static final String HeapSnapshotKey = "heapSnapshot";
1215
private static final String HeapSnapshotScriptKey = "heapSnapshotScript";
16+
private static final String HeapSnapshotBlobKey = "heapSnapshotBlob";
17+
private static final String SnapshotFile = "snapshot.blob";
1318
private static final String ProfilerOutputDirKey = "profilerOutputDir";
1419

1520
public static Object[] fromPackageJSON(File appDir)
@@ -45,9 +50,21 @@ public static Object[] fromPackageJSON(File appDir)
4550
String value = androidObject.getString(HeapSnapshotScriptKey);
4651
result[3] = FileSystem.resolveRelativePath(appDir.getPath(), value, appDir + "/app/");
4752
}
53+
if(androidObject.has(HeapSnapshotBlobKey))
54+
{
55+
String value = androidObject.getString(HeapSnapshotBlobKey);
56+
String path = FileSystem.resolveRelativePath(appDir.getPath(), value, appDir + "/app/");
57+
File dir = new File(path);
58+
if(dir.exists() && dir.isDirectory())
59+
{
60+
// this path is expected to be a directory, containing three sub-directories: armeabi-v7a, x86 and arm64-v8a
61+
path = path + "/" + Build.CPU_ABI + "/" + SnapshotFile;
62+
result[4] = path;
63+
}
64+
}
4865
if(androidObject.has(ProfilerOutputDirKey))
4966
{
50-
result[4] = androidObject.getString(ProfilerOutputDirKey);
67+
result[5] = androidObject.getString(ProfilerOutputDirKey);
5168
}
5269
}
5370
}
@@ -70,6 +87,8 @@ private static Object[] makeDefaultOptions()
7087
false,
7188
// arbitrary script to be included in the snapshot
7289
"",
90+
// a binary file containing an already saved snapshot
91+
"",
7392
// V8 profiler output directory
7493
""
7594
};

0 commit comments

Comments
 (0)