Skip to content

Commit

Permalink
[compile hints] Produce compile hints
Browse files Browse the repository at this point in the history
Query compile hints from V8 and send them via UKM.

UKM collection request: https://docs.google.com/document/d/1mxse4AYNdPGwk9UKYaEpdKYG66TfyGxoLNjZWotI2OI/edit?usp=sharing&resourcekey=0-wbj656WuW3ZhnNlCkx4AIA

Bug: chromium:1406506
Change-Id: Ic1f944e92e976243b0873aff7cf6e16157418d17
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4161550
Reviewed-by: Sun Yueru <yrsun@chromium.org>
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1108819}
  • Loading branch information
marjakh authored and Chromium LUCI CQ committed Feb 23, 2023
1 parent be9d050 commit 99760b2
Show file tree
Hide file tree
Showing 17 changed files with 927 additions and 16 deletions.
1 change: 1 addition & 0 deletions build/config/fuchsia/size_optimized_cast_receiver_args.gn
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ enable_printing = false
enable_cast_receiver = true
cast_streaming_enable_remoting = true
enable_dav1d_decoder = false
enable_v8_compile_hints = false

# //chrome makes many assumptions that Extensions are enabled.
# TODO(crbug.com/1363742): Fix theses assumptions or avoid building it.
Expand Down
8 changes: 8 additions & 0 deletions third_party/blink/common/features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,14 @@ const base::FeatureParam<int> kCacheCodeOnIdleDelayParam{&kCacheCodeOnIdle,
const base::FeatureParam<bool> kCacheCodeOnIdleDelayServiceWorkerOnlyParam{
&kCacheCodeOnIdle, "service-worker-only", false};

BASE_FEATURE(kProduceCompileHints,
"ProduceCompileHints",
base::FEATURE_DISABLED_BY_DEFAULT);
const base::FeatureParam<int> kProduceCompileHintsOnIdleDelayParam{
&kProduceCompileHints, "delay-in-ms", 10000};
const base::FeatureParam<double> kProduceCompileHintsNoiseLevel{
&kProduceCompileHints, "noise probability", 0.9};

// Make all pending 'display: auto' web fonts enter the swap or failure period
// immediately before reaching the LCP time limit (~2500ms), so that web fonts
// do not become a source of bad LCP.
Expand Down
6 changes: 6 additions & 0 deletions third_party/blink/public/common/features.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@ BLINK_COMMON_EXPORT extern const base::FeatureParam<int>
BLINK_COMMON_EXPORT extern const base::FeatureParam<bool>
kCacheCodeOnIdleDelayServiceWorkerOnlyParam;

BLINK_COMMON_EXPORT BASE_DECLARE_FEATURE(kProduceCompileHints);
BLINK_COMMON_EXPORT extern const base::FeatureParam<int>
kProduceCompileHintsOnIdleDelayParam;
BLINK_COMMON_EXPORT extern const base::FeatureParam<double>
kProduceCompileHintsNoiseLevel;

BLINK_COMMON_EXPORT BASE_DECLARE_FEATURE(
kAlignFontDisplayAutoTimeoutWithLCPGoal);
BLINK_COMMON_EXPORT extern const base::FeatureParam<int>
Expand Down
8 changes: 8 additions & 0 deletions third_party/blink/renderer/bindings/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/buildflag_header.gni")
import("//build/config/python.gni")
import("//third_party/blink/renderer/bindings/bindings.gni")
import("//third_party/blink/renderer/bindings/generated_in_core.gni")
Expand Down Expand Up @@ -412,3 +413,10 @@ group("v8_context_snapshot_influential_libs") {
"//third_party/blink/renderer/platform",
]
}

buildflag_header("buildflags") {
header = "buildflags.h"
header_dir = "third_party/blink/renderer/bindings"

flags = [ "ENABLE_V8_COMPILE_HINTS=$enable_v8_compile_hints" ]
}
14 changes: 14 additions & 0 deletions third_party/blink/renderer/bindings/bindings.gni
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,20 @@ blink_core_sources_bindings =
],
"abspath")

declare_args() {
# Enable V8 to produce & consume hints regarding which functions to compile
# eagerly.
enable_v8_compile_hints = true
}

if (enable_v8_compile_hints) {
blink_core_sources_bindings += get_path_info([
"core/v8/v8_compile_hints.cc",
"core/v8/v8_compile_hints.h",
],
"abspath")
}

# Source files to be part of
# "//third_party/blink/renderer/modules:modules".
blink_modules_sources_bindings = get_path_info(
Expand Down
3 changes: 2 additions & 1 deletion third_party/blink/renderer/bindings/core/v8/module_record.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ v8::Local<v8::Module> ModuleRecord::Compile(
std::tie(compile_options, produce_cache_options, no_cache_reason) =
V8CodeCache::GetCompileOptions(v8_cache_options, params.CacheHandler(),
params.GetSourceText().length(),
params.SourceLocationType());
params.SourceLocationType(),
params.BaseURL());

if (!V8ScriptRunner::CompileModule(
isolate, params, text_position, compile_options, no_cache_reason,
Expand Down
10 changes: 9 additions & 1 deletion third_party/blink/renderer/bindings/core/v8/script_streamer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -748,11 +748,19 @@ bool ResourceScriptStreamer::TryStartStreamingTask() {
source_ = std::make_unique<v8::ScriptCompiler::StreamedSource>(
std::move(stream_ptr), encoding_);

// Call FeatureList::IsEnabled only once.
static bool compile_hints_enabled =
base::FeatureList::IsEnabled(features::kProduceCompileHints);

v8::ScriptCompiler::CompileOptions compile_options =
compile_hints_enabled ? v8::ScriptCompiler::kProduceCompileHints
: v8::ScriptCompiler::kNoCompileOptions;

std::unique_ptr<v8::ScriptCompiler::ScriptStreamingTask>
script_streaming_task =
base::WrapUnique(v8::ScriptCompiler::StartStreaming(
V8PerIsolateData::MainThreadIsolate(), source_.get(),
script_type_));
script_type_, compile_options));

if (!script_streaming_task) {
// V8 cannot stream the script.
Expand Down
42 changes: 31 additions & 11 deletions third_party/blink/renderer/bindings/core/v8/v8_code_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "base/metrics/histogram_functions.h"
#include "build/build_config.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/v8_cache_options.mojom-blink.h"
#include "third_party/blink/public/web/web_settings.h"
#include "third_party/blink/renderer/bindings/core/v8/module_record.h"
Expand Down Expand Up @@ -108,7 +109,8 @@ V8CodeCache::GetCompileOptions(mojom::blink::V8CacheOptions cache_options,
const ClassicScript& classic_script) {
return GetCompileOptions(cache_options, classic_script.CacheHandler(),
classic_script.SourceText().length(),
classic_script.SourceLocationType());
classic_script.SourceLocationType(),
classic_script.SourceUrl());
}

std::tuple<v8::ScriptCompiler::CompileOptions,
Expand All @@ -117,10 +119,30 @@ std::tuple<v8::ScriptCompiler::CompileOptions,
V8CodeCache::GetCompileOptions(mojom::blink::V8CacheOptions cache_options,
const CachedMetadataHandler* cache_handler,
size_t source_text_length,
ScriptSourceLocationType source_location_type) {
ScriptSourceLocationType source_location_type,
const KURL& url) {
static const int kMinimalCodeLength = 1024;
v8::ScriptCompiler::NoCacheReason no_cache_reason;

auto no_code_cache_compile_options = v8::ScriptCompiler::kNoCompileOptions;

// Call FeatureList::IsEnabled only once.
static bool compile_hints_enabled =
base::FeatureList::IsEnabled(features::kProduceCompileHints);

if (compile_hints_enabled && url.ProtocolIsInHTTPFamily()) {
// If we end up compiling the script without forced eager compilation, we'll
// also produce compile hints. This is orthogonal to various cache
// behaviors: if we don't want to create a code cache for some reason
// (e.g., script too small, or not hot enough) we still want to produce
// compile hints.

// When we're forcing eager compilation, we cannot produce compile hints
// (we won't gather data about which eagerly compiled functions are
// actually used).
no_code_cache_compile_options = v8::ScriptCompiler::kProduceCompileHints;
}

switch (source_location_type) {
case ScriptSourceLocationType::kInline:
no_cache_reason = v8::ScriptCompiler::kNoCacheBecauseInlineScript;
Expand All @@ -140,21 +162,21 @@ V8CodeCache::GetCompileOptions(mojom::blink::V8CacheOptions cache_options,
}

if (!cache_handler) {
return std::make_tuple(v8::ScriptCompiler::kNoCompileOptions,
return std::make_tuple(no_code_cache_compile_options,
ProduceCacheOptions::kNoProduceCache,
no_cache_reason);
}

if (cache_options == mojom::blink::V8CacheOptions::kNone) {
no_cache_reason = v8::ScriptCompiler::kNoCacheBecauseCachingDisabled;
return std::make_tuple(v8::ScriptCompiler::kNoCompileOptions,
return std::make_tuple(no_code_cache_compile_options,
ProduceCacheOptions::kNoProduceCache,
no_cache_reason);
}

if (source_text_length < kMinimalCodeLength) {
no_cache_reason = v8::ScriptCompiler::kNoCacheBecauseScriptTooSmall;
return std::make_tuple(v8::ScriptCompiler::kNoCompileOptions,
return std::make_tuple(no_code_cache_compile_options,
ProduceCacheOptions::kNoProduceCache,
no_cache_reason);
}
Expand All @@ -174,18 +196,16 @@ V8CodeCache::GetCompileOptions(mojom::blink::V8CacheOptions cache_options,
case mojom::blink::V8CacheOptions::kDefault:
case mojom::blink::V8CacheOptions::kCode:
if (!IsResourceHotForCaching(cache_handler)) {
return std::make_tuple(v8::ScriptCompiler::kNoCompileOptions,
return std::make_tuple(no_code_cache_compile_options,
ProduceCacheOptions::kSetTimeStamp,
v8::ScriptCompiler::kNoCacheBecauseCacheTooCold);
}
return std::make_tuple(
v8::ScriptCompiler::kNoCompileOptions,
ProduceCacheOptions::kProduceCodeCache,
no_code_cache_compile_options, ProduceCacheOptions::kProduceCodeCache,
v8::ScriptCompiler::kNoCacheBecauseDeferredProduceCodeCache);
case mojom::blink::V8CacheOptions::kCodeWithoutHeatCheck:
return std::make_tuple(
v8::ScriptCompiler::kNoCompileOptions,
ProduceCacheOptions::kProduceCodeCache,
no_code_cache_compile_options, ProduceCacheOptions::kProduceCodeCache,
v8::ScriptCompiler::kNoCacheBecauseDeferredProduceCodeCache);
case mojom::blink::V8CacheOptions::kFullCodeWithoutHeatCheck:
return std::make_tuple(
Expand All @@ -202,7 +222,7 @@ V8CodeCache::GetCompileOptions(mojom::blink::V8CacheOptions cache_options,
// All switch branches should return and we should never get here.
// But some compilers aren't sure, hence this default.
NOTREACHED();
return std::make_tuple(v8::ScriptCompiler::kNoCompileOptions,
return std::make_tuple(no_code_cache_compile_options,
ProduceCacheOptions::kNoProduceCache,
v8::ScriptCompiler::kNoCacheNoReason);
}
Expand Down
3 changes: 2 additions & 1 deletion third_party/blink/renderer/bindings/core/v8/v8_code_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ class CORE_EXPORT V8CodeCache final {
GetCompileOptions(mojom::blink::V8CacheOptions,
const CachedMetadataHandler*,
size_t source_text_length,
ScriptSourceLocationType);
ScriptSourceLocationType,
const KURL& url);

static scoped_refptr<CachedMetadata> GetCachedMetadata(
const CachedMetadataHandler* cache_handler,
Expand Down

0 comments on commit 99760b2

Please sign in to comment.