Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable bytecode caching by default #165

Merged
merged 4 commits into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,19 @@ if (v8AndroidVersionMajor < 11) {
// These constant values should align to V8RuntimeConfig.java
ext.CODECACHE_MODE_NONE = 0
ext.CODECACHE_MODE_NORMAL = 1
ext.CODECACHE_MODE_PREBUILT = 2
ext.CODECACHE_MODE_NORMAL_WITH_STUB_BUNDLE = 3
ext.CODECACHE_MODE_STUB_BUNDLE = 2
def parseCacheMode(cacheMode) {
switch (cacheMode) {
case null:
// Use normal mode by default
return ext.CODECACHE_MODE_NORMAL
case "none":
return ext.CODECACHE_MODE_NONE
case "normal":
return ext.CODECACHE_MODE_NORMAL
case "prebuilt":
return ext.CODECACHE_MODE_PREBUILT
case "stubBundle":
case "normalWithStubBundle":
return ext.CODECACHE_MODE_NORMAL_WITH_STUB_BUNDLE
return ext.CODECACHE_MODE_STUB_BUNDLE
default:
throw new GradleException("Unsupported cache mode - ${cacheMode}")
}
Expand All @@ -114,9 +115,6 @@ def reactNativeArchitectures() {
}

apply plugin: "com.android.library"
if (v8CacheMode == CODECACHE_MODE_PREBUILT) {
apply from: "./mkcodecache.gradle"
}

android {
compileSdkVersion safeExtGet("compileSdkVersion", 33)
Expand Down Expand Up @@ -188,7 +186,7 @@ android {
assets.srcDirs += [ "${v8AndroidDir}/dist/snapshot_blob" ]
}

if (v8CacheMode == CODECACHE_MODE_NORMAL_WITH_STUB_BUNDLE) {
if (v8CacheMode == CODECACHE_MODE_STUB_BUNDLE) {
assets.srcDirs += [ "src/stub_bundle" ]
}
}
Expand Down
133 changes: 0 additions & 133 deletions android/mkcodecache.gradle

This file was deleted.

9 changes: 2 additions & 7 deletions android/src/main/cpp/OnLoad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class V8ExecutorHolder
const std::string &deviceName,
const std::string &snapshotBlobPath,
int codecacheMode,
const std::string &codecachePath) {
const std::string &codecacheDir) {
react::JReactMarker::setLogPerfMarkerIfNeeded();

auto config = std::make_unique<V8RuntimeConfig>();
Expand All @@ -84,12 +84,7 @@ class V8ExecutorHolder
}
config->codecacheMode =
static_cast<V8RuntimeConfig::CodecacheMode>(codecacheMode);
config->codecachePath = codecachePath;
if (config->codecacheMode == V8RuntimeConfig::CodecacheMode::kPrebuilt &&
!codecachePath.empty()) {
config->prebuiltCodecacheBlob =
std::move(loadBlob(assetManager, codecachePath));
}
config->codecacheDir = codecacheDir;

return makeCxxInstance(folly::make_unique<V8ExecutorFactory>(
installBindings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ public class V8Executor extends JavaScriptExecutor {
config.snapshotBlobPath != null ? config.snapshotBlobPath
: loadDefaultSnapshotBlobPath(),
config.codecacheMode,
config.codecachePath != null
? config.codecachePath
: loadDefaultCodecachePath(context, config.codecacheMode)));
config.codecacheDir != null ? config.codecacheDir
: context.getCodeCacheDir().toString()));
}

@Override
Expand All @@ -49,15 +48,6 @@ private static String loadDefaultSnapshotBlobPath() {
return "";
}

private static String loadDefaultCodecachePath(
final Context context,
int codecacheMode) {
if (codecacheMode == V8RuntimeConfig.CODECACHE_MODE_PREBUILT) {
return "assets://" + Build.SUPPORTED_ABIS[0] + "/v8codecache.bin";
}
return new File(context.getCodeCacheDir(), "v8codecache.bin").toString();
}

private static native HybridData initHybrid(
AssetManager assetManager,
String timezoneId,
Expand All @@ -66,7 +56,7 @@ private static native HybridData initHybrid(
String deviceName,
String snapshotBlobPath,
int codecacheMode,
String codecachePath);
String codecacheDir);

/* package */ static native void onMainLoopIdle(
RuntimeExecutor runtimeExecutor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,17 @@ public String toString() {
}

@Nullable
public static String getBundleAssetName(
public static String getV8BundleAssetName(
final Context context,
final String assetName,
final boolean useDeveloperSupport) {
if (useDeveloperSupport) {
return null;
}
final boolean hasCache =
new File(context.getCodeCacheDir(), "v8codecache.bin").exists();
new File(context.getCodeCacheDir(), assetName).exists();
if (BuildConfig.V8_CACHE_MODE ==
V8RuntimeConfig.CODECACHE_MODE_NORMAL_WITH_STUB_BUNDLE &&
V8RuntimeConfig.CODECACHE_MODE_STUB_BUNDLE &&
hasCache) {
return "stub.bundle";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,31 @@ public final class V8RuntimeConfig {
@Nullable public String snapshotBlobPath;

@IntDef(
{CODECACHE_MODE_NONE,
CODECACHE_MODE_NORMAL,
CODECACHE_MODE_PREBUILT,
CODECACHE_MODE_NORMAL_WITH_STUB_BUNDLE})
{CODECACHE_MODE_NONE, CODECACHE_MODE_NORMAL, CODECACHE_MODE_STUB_BUNDLE})
@Retention(RetentionPolicy.SOURCE)
private @interface CodecacheMode {}

// Disable bytecode caching
public static final int CODECACHE_MODE_NONE = 0;
// Classic v8 bytecode caching
public static final int CODECACHE_MODE_NORMAL = 1;
// **EXPERIMENTAL** Prebuilt bytecode caching
public static final int CODECACHE_MODE_PREBUILT = 2;
// **EXPERIMENTAL** Classic v8 bytecode caching + loading stub JS bundle when
// cache existed
public static final int CODECACHE_MODE_NORMAL_WITH_STUB_BUNDLE = 3;
public static final int CODECACHE_MODE_STUB_BUNDLE = 2;

// Bytecode caching mode
public @CodecacheMode int codecacheMode;

// Codecache blob
@Nullable public String codecachePath;
// The directory to store codecache files
@Nullable public String codecacheDir;

public static V8RuntimeConfig createDefault() {
final V8RuntimeConfig config = new V8RuntimeConfig();
config.timezoneId = getTimezoneId();
config.enableInspector = false;
config.snapshotBlobPath = null;
config.codecacheMode = CODECACHE_MODE_NONE;
config.codecachePath = null;
config.codecacheDir = null;
return config;
}

Expand Down
Loading