Commit ffdc50a9 authored by Leszek Swirski's avatar Leszek Swirski Committed by V8 LUCI CQ

[compiler] Use SmallVector for Parser->BG thread use counts

Change the off-thread parse to fill a SmallVector<UseCounterFeature, 8>
on the BG compile task, rather than an int[kUseCounterFeatureCount]
array. This allows us to keep the loop over use counts in the compile
task finalization short by avoiding looping over unused counters.

The value 8 was chosen as a "reasonable small number"; experimenting on
our benchmarks shows a max of 3 use counts collected per compile (and
at a vanishingly low percentage of all compiles).

Passing around an explicit SmallVector<UseCounterFeature, 8> pointer,
complete with size, is a bit ugly, but since it's used only in this one
place (Parser -> BackgroundCompileTask) I can live with it to avoid
further indirections. Typedeffing it is possible, but it's not clear
where, since it's needed in both src/codegen/compiler.h and
src/parsing/parser.h, and neither includes the other.

Change-Id: Idb73e2f56fa9e8911ea29fb810d7562246f19d46
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3318662Reviewed-by: 's avatarIgor Sheludko <ishell@chromium.org>
Commit-Queue: Leszek Swirski <leszeks@chromium.org>
Cr-Commit-Position: refs/heads/main@{#78305}
parent ef14030b
...@@ -1547,7 +1547,7 @@ void BackgroundCompileTask::Run( ...@@ -1547,7 +1547,7 @@ void BackgroundCompileTask::Run(
parser.ParseOnBackground(isolate, &info, start_position_, end_position_, parser.ParseOnBackground(isolate, &info, start_position_, end_position_,
function_literal_id_); function_literal_id_);
parser.UpdateStatistics(script_, use_counts_, &total_preparse_skipped_); parser.UpdateStatistics(script_, &use_counts_, &total_preparse_skipped_);
// Save the language mode. // Save the language mode.
language_mode_ = info.language_mode(); language_mode_ = info.language_mode();
...@@ -1691,14 +1691,13 @@ void BackgroundCompileTask::AbortFunction() { ...@@ -1691,14 +1691,13 @@ void BackgroundCompileTask::AbortFunction() {
void BackgroundCompileTask::ReportStatistics(Isolate* isolate) { void BackgroundCompileTask::ReportStatistics(Isolate* isolate) {
// Update use-counts. // Update use-counts.
for (int i = 0; i < static_cast<int>(v8::Isolate::kUseCounterFeatureCount); for (auto feature : use_counts_) {
++i) { isolate->CountUsage(feature);
v8::Isolate::UseCounterFeature feature =
static_cast<v8::Isolate::UseCounterFeature>(i);
isolate->CountUsage(feature, use_counts_[i]);
} }
if (total_preparse_skipped_ > 0) {
isolate->counters()->total_preparse_skipped()->Increment( isolate->counters()->total_preparse_skipped()->Increment(
total_preparse_skipped_); total_preparse_skipped_);
}
} }
BackgroundDeserializeTask::BackgroundDeserializeTask( BackgroundDeserializeTask::BackgroundDeserializeTask(
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "src/ast/ast-value-factory.h" #include "src/ast/ast-value-factory.h"
#include "src/base/platform/elapsed-timer.h" #include "src/base/platform/elapsed-timer.h"
#include "src/base/small-vector.h"
#include "src/codegen/bailout-reason.h" #include "src/codegen/bailout-reason.h"
#include "src/common/globals.h" #include "src/common/globals.h"
#include "src/execution/isolate.h" #include "src/execution/isolate.h"
...@@ -552,7 +553,7 @@ class V8_EXPORT_PRIVATE BackgroundCompileTask { ...@@ -552,7 +553,7 @@ class V8_EXPORT_PRIVATE BackgroundCompileTask {
IsCompiledScope is_compiled_scope_; IsCompiledScope is_compiled_scope_;
FinalizeUnoptimizedCompilationDataList finalize_unoptimized_compilation_data_; FinalizeUnoptimizedCompilationDataList finalize_unoptimized_compilation_data_;
DeferredFinalizationJobDataList jobs_to_retry_finalization_on_main_thread_; DeferredFinalizationJobDataList jobs_to_retry_finalization_on_main_thread_;
int use_counts_[v8::Isolate::kUseCounterFeatureCount] = {0}; base::SmallVector<v8::Isolate::UseCounterFeature, 8> use_counts_;
int total_preparse_skipped_ = 0; int total_preparse_skipped_ = 0;
// Single function data for top-level function compilation. // Single function data for top-level function compilation.
......
...@@ -3370,19 +3370,21 @@ void Parser::UpdateStatistics(Isolate* isolate, Handle<Script> script) { ...@@ -3370,19 +3370,21 @@ void Parser::UpdateStatistics(Isolate* isolate, Handle<Script> script) {
total_preparse_skipped_); total_preparse_skipped_);
} }
void Parser::UpdateStatistics(Handle<Script> script, int* use_counts, void Parser::UpdateStatistics(
Handle<Script> script,
base::SmallVector<v8::Isolate::UseCounterFeature, 8>* use_counts,
int* preparse_skipped) { int* preparse_skipped) {
// Move statistics to Isolate. // Move statistics to Isolate.
for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount; for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
++feature) { ++feature) {
if (use_counts_[feature] > 0) { if (use_counts_[feature] > 0) {
use_counts[feature]++; use_counts->emplace_back(v8::Isolate::UseCounterFeature(feature));
} }
} }
if (scanner_.FoundHtmlComment()) { if (scanner_.FoundHtmlComment()) {
use_counts[v8::Isolate::kHtmlComment]++; use_counts->emplace_back(v8::Isolate::kHtmlComment);
if (script->line_offset() == 0 && script->column_offset() == 0) { if (script->line_offset() == 0 && script->column_offset() == 0) {
use_counts[v8::Isolate::kHtmlCommentInExternalScript]++; use_counts->emplace_back(v8::Isolate::kHtmlCommentInExternalScript);
} }
} }
*preparse_skipped = total_preparse_skipped_; *preparse_skipped = total_preparse_skipped_;
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "src/ast/ast.h" #include "src/ast/ast.h"
#include "src/ast/scopes.h" #include "src/ast/scopes.h"
#include "src/base/compiler-specific.h" #include "src/base/compiler-specific.h"
#include "src/base/small-vector.h"
#include "src/base/threaded-list.h" #include "src/base/threaded-list.h"
#include "src/common/globals.h" #include "src/common/globals.h"
#include "src/parsing/import-assertions.h" #include "src/parsing/import-assertions.h"
...@@ -163,7 +164,9 @@ class V8_EXPORT_PRIVATE Parser : public NON_EXPORTED_BASE(ParserBase<Parser>) { ...@@ -163,7 +164,9 @@ class V8_EXPORT_PRIVATE Parser : public NON_EXPORTED_BASE(ParserBase<Parser>) {
// Move statistics to Isolate // Move statistics to Isolate
void UpdateStatistics(Isolate* isolate, Handle<Script> script); void UpdateStatistics(Isolate* isolate, Handle<Script> script);
void UpdateStatistics(Handle<Script> script, int* use_counters, void UpdateStatistics(
Handle<Script> script,
base::SmallVector<v8::Isolate::UseCounterFeature, 8>* use_counters,
int* preparse_skipped); int* preparse_skipped);
template <typename IsolateT> template <typename IsolateT>
void HandleSourceURLComments(IsolateT* isolate, Handle<Script> script); void HandleSourceURLComments(IsolateT* isolate, Handle<Script> script);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment