summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/html/parser/background_html_scanner.cc
blob: e03a3582a67e11720e721f21c8070fa5f5b62f84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/html/parser/background_html_scanner.h"

#include "base/task/sequenced_task_runner.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/renderer/core/dom/scriptable_document_parser.h"
#include "third_party/blink/renderer/core/html/parser/html_preload_scanner.h"
#include "third_party/blink/renderer/core/html/parser/html_tokenizer.h"
#include "third_party/blink/renderer/core/html_names.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/scheduler/public/worker_pool.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier_base.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier_std.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"

namespace blink {
namespace {

using CompileOptions = v8::ScriptCompiler::CompileOptions;

// Eager compilation takes more time and uses more memory than lazy compilation,
// but the resulting code executes faster. These options let us trade off
// between the pros/cons of eager and lazy compilation.
enum class CompileStrategy {
  // All scripts are compiled lazily.
  kLazy,
  // The first script in the chunk being scanned is compiled lazily, while the
  // rest are compiled eagerly. The first script usually needs to be parsed and
  // run soon after the body chunk is received, so using lazy compilation for
  // that script allows it to run sooner since lazy compilation will complete
  // faster.
  kFirstScriptLazy,
  // All scripts are compiled eagerly.
  kEager,
};

CompileOptions GetCompileOptions(bool first_script_in_scan) {
  static const base::FeatureParam<CompileStrategy>::Option
      kCompileStrategyOptions[] = {
          {CompileStrategy::kLazy, "lazy"},
          {CompileStrategy::kFirstScriptLazy, "first-script-lazy"},
          {CompileStrategy::kEager, "eager"},
      };

  static const base::FeatureParam<CompileStrategy> kCompileStrategyParam{
      &features::kPrecompileInlineScripts, "compile-strategy",
      CompileStrategy::kLazy, &kCompileStrategyOptions};

  switch (kCompileStrategyParam.Get()) {
    case CompileStrategy::kLazy:
      return CompileOptions::kNoCompileOptions;
    case CompileStrategy::kFirstScriptLazy:
      return first_script_in_scan ? CompileOptions::kNoCompileOptions
                                  : CompileOptions::kEagerCompile;
    case CompileStrategy::kEager:
      return CompileOptions::kEagerCompile;
  }
}

scoped_refptr<base::SequencedTaskRunner> GetCompileTaskRunner() {
  static const base::FeatureParam<bool> kCompileInParallelParam{
      &features::kPrecompileInlineScripts, "compile-in-parallel", true};
  // Cache the value to avoid parsing the param string more than once.
  static const bool kCompileInParallelValue = kCompileInParallelParam.Get();
  // Returning a null task runner will result in posting to the worker pool for
  // each task.
  if (kCompileInParallelValue)
    return nullptr;
  return worker_pool::CreateSequencedTaskRunner(
      {base::TaskPriority::USER_BLOCKING});
}

scoped_refptr<base::SequencedTaskRunner> GetTokenizeTaskRunner() {
  static const base::FeatureParam<bool> kTokenizeInParallelParam{
      &features::kPretokenizeCSS, "tokenize-in-parallel", true};
  // Cache the value to avoid parsing the param string more than once.
  static const bool kTokenizeInParallelValue = kTokenizeInParallelParam.Get();
  // Returning a null task runner will result in posting to the worker pool for
  // each task.
  if (kTokenizeInParallelValue)
    return nullptr;
  return worker_pool::CreateSequencedTaskRunner(
      {base::TaskPriority::USER_BLOCKING});
}

wtf_size_t GetMinimumScriptSize() {
  static const base::FeatureParam<int> kMinimumScriptSizeParam{
      &features::kPrecompileInlineScripts, "minimum-script-size", 0};
  // Cache the value to avoid parsing the param string more than once.
  static const wtf_size_t kMinimumScriptSizeValue =
      static_cast<wtf_size_t>(kMinimumScriptSizeParam.Get());
  return kMinimumScriptSizeValue;
}

wtf_size_t GetMinimumCSSSize() {
  static const base::FeatureParam<int> kMinimumCSSSizeParam{
      &features::kPretokenizeCSS, "minimum-css-size", 0};
  // Cache the value to avoid parsing the param string more than once.
  static const wtf_size_t kMinimumCSSSizeValue =
      static_cast<wtf_size_t>(kMinimumCSSSizeParam.Get());
  return kMinimumCSSSizeValue;
}

void TokenizeInlineCSS(const String& style_text,
                       ScriptableDocumentParser* parser) {
  if (!parser)
    return;

  TRACE_EVENT0("blink", "TokenizeInlineCSS");
  parser->AddCSSTokenizer(style_text,
                          CSSTokenizer::CreateCachedTokenizer(style_text));
}

}  // namespace

// static
WTF::SequenceBound<BackgroundHTMLScanner> BackgroundHTMLScanner::Create(
    const HTMLParserOptions& options,
    ScriptableDocumentParser* parser) {
  TRACE_EVENT0("blink", "BackgroundHTMLScanner::Create");
  // The background scanner lives on one sequence, while the script streamers
  // work on a second sequence. This allows us to continue scanning the HTML
  // while scripts are compiling.
  return WTF::SequenceBound<BackgroundHTMLScanner>(
      worker_pool::CreateSequencedTaskRunner(
          {base::TaskPriority::USER_BLOCKING}),
      std::make_unique<HTMLTokenizer>(options),
      ScriptTokenScanner::Create(parser));
}

BackgroundHTMLScanner::BackgroundHTMLScanner(
    std::unique_ptr<HTMLTokenizer> tokenizer,
    std::unique_ptr<ScriptTokenScanner> token_scanner)
    : tokenizer_(std::move(tokenizer)),
      token_scanner_(std::move(token_scanner)) {
  DCHECK(token_scanner_);
}

BackgroundHTMLScanner::~BackgroundHTMLScanner() = default;

void BackgroundHTMLScanner::Scan(const String& source) {
  TRACE_EVENT0("blink", "BackgroundHTMLScanner::Scan");
  token_scanner_->set_first_script_in_scan(true);
  source_.Append(source);
  while (tokenizer_->NextToken(source_, token_)) {
    if (token_.GetType() == HTMLToken::kStartTag)
      tokenizer_->UpdateStateFor(token_);
    token_scanner_->ScanToken(token_);
    token_.Clear();
  }
}

std::unique_ptr<BackgroundHTMLScanner::ScriptTokenScanner>
BackgroundHTMLScanner::ScriptTokenScanner::Create(
    ScriptableDocumentParser* parser) {
  bool precompile_scripts =
      base::FeatureList::IsEnabled(features::kPrecompileInlineScripts);
  bool pretokenize_css =
      base::FeatureList::IsEnabled(features::kPretokenizeCSS) &&
      features::kPretokenizeInlineSheets.Get();
  if (!precompile_scripts && !pretokenize_css)
    return nullptr;

  return std::make_unique<ScriptTokenScanner>(
      parser,
      OptimizationParams{.task_runner = GetCompileTaskRunner(),
                         .min_size = GetMinimumScriptSize(),
                         .enabled = precompile_scripts},
      OptimizationParams{.task_runner = GetTokenizeTaskRunner(),
                         .min_size = GetMinimumCSSSize(),
                         .enabled = pretokenize_css});
}

BackgroundHTMLScanner::ScriptTokenScanner::ScriptTokenScanner(
    ScriptableDocumentParser* parser,
    OptimizationParams precompile_scripts_params,
    OptimizationParams pretokenize_css_params)
    : parser_(parser),
      precompile_scripts_params_(std::move(precompile_scripts_params)),
      pretokenize_css_params_(std::move(pretokenize_css_params)) {
  DCHECK(precompile_scripts_params_.enabled || pretokenize_css_params_.enabled);
}

void BackgroundHTMLScanner::ScriptTokenScanner::ScanToken(
    const HTMLToken& token) {
  switch (token.GetType()) {
    case HTMLToken::kCharacter: {
      if (in_tag_ != InsideTag::kNone) {
        if (token.IsAll8BitData())
          builder_.Append(token.Data().AsString8());
        else
          builder_.Append(token.Data().AsString());
      }
      return;
    }
    case HTMLToken::kStartTag: {
      if (precompile_scripts_params_.enabled &&
          Match(TagImplFor(token.Data()), html_names::kScriptTag)) {
        DCHECK_EQ(in_tag_, InsideTag::kNone);
        in_tag_ = InsideTag::kScript;
      } else if (pretokenize_css_params_.enabled &&
                 Match(TagImplFor(token.Data()), html_names::kStyleTag)) {
        DCHECK_EQ(in_tag_, InsideTag::kNone);
        in_tag_ = InsideTag::kStyle;
      } else {
        in_tag_ = InsideTag::kNone;
      }
      builder_.Clear();
      return;
    }
    case HTMLToken::kEndTag: {
      if (precompile_scripts_params_.enabled &&
          Match(TagImplFor(token.Data()), html_names::kScriptTag) &&
          in_tag_ == InsideTag::kScript) {
        in_tag_ = InsideTag::kNone;
        // The script was empty, do nothing.
        if (builder_.IsEmpty())
          return;

        String script_text = builder_.ReleaseString();
        builder_.Clear();

        if (script_text.length() < precompile_scripts_params_.min_size)
          return;

        auto streamer = base::MakeRefCounted<BackgroundInlineScriptStreamer>(
            script_text, GetCompileOptions(first_script_in_scan_));
        first_script_in_scan_ = false;
        auto parser_lock = parser_.Lock();
        if (!parser_lock || !streamer->CanStream())
          return;

        parser_lock->AddInlineScriptStreamer(script_text, streamer);
        if (precompile_scripts_params_.task_runner) {
          PostCrossThreadTask(
              *precompile_scripts_params_.task_runner, FROM_HERE,
              CrossThreadBindOnce(&BackgroundInlineScriptStreamer::Run,
                                  std::move(streamer)));
        } else {
          worker_pool::PostTask(
              FROM_HERE, {base::TaskPriority::USER_BLOCKING},
              CrossThreadBindOnce(&BackgroundInlineScriptStreamer::Run,
                                  std::move(streamer)));
        }
      } else if (pretokenize_css_params_.enabled &&
                 Match(TagImplFor(token.Data()), html_names::kStyleTag) &&
                 in_tag_ == InsideTag::kStyle) {
        in_tag_ = InsideTag::kNone;
        // The style was empty, do nothing.
        if (builder_.IsEmpty())
          return;

        String style_text = builder_.ReleaseString();
        builder_.Clear();

        if (style_text.length() < pretokenize_css_params_.min_size)
          return;

        // We don't need to tokenize duplicate stylesheets, as these will
        // already be cached. The set stores just the hash of the string to
        // optimize memory usage, and it's fine to do extra work in the rare
        // case of a hash collision.
        if (!css_text_hashes_.insert(style_text.Impl()->GetHash()).is_new_entry)
          return;

        if (pretokenize_css_params_.task_runner) {
          PostCrossThreadTask(
              *pretokenize_css_params_.task_runner, FROM_HERE,
              CrossThreadBindOnce(&TokenizeInlineCSS, style_text, parser_));
        } else {
          worker_pool::PostTask(
              FROM_HERE, {base::TaskPriority::USER_BLOCKING},
              CrossThreadBindOnce(&TokenizeInlineCSS, style_text, parser_));
        }
      }
      return;
    }
    default: {
      return;
    }
  }
}

}  // namespace blink