summaryrefslogtreecommitdiff
path: root/chromium/v8/src/codegen/background-merge-task.h
blob: 643a6a84efadbbd627df42b57ac807486b3763db (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
// Copyright 2022 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_CODEGEN_BACKGROUND_MERGE_TASK_H_
#define V8_CODEGEN_BACKGROUND_MERGE_TASK_H_

#include <vector>

#include "src/handles/maybe-handles.h"

namespace v8 {
namespace internal {

class FeedbackMetadata;
class PersistentHandles;
class Script;
class SharedFunctionInfo;
class String;

struct ScriptDetails;

// Contains data transferred between threads for background merging between a
// newly compiled or deserialized script and an existing script from the Isolate
// compilation cache.
class V8_EXPORT_PRIVATE BackgroundMergeTask {
 public:
  ~BackgroundMergeTask();

  // Step 1: on the main thread, check whether the Isolate compilation cache
  // contains the script.
  void SetUpOnMainThread(Isolate* isolate, Handle<String> source_text,
                         const ScriptDetails& script_details,
                         LanguageMode language_mode);

  // Step 2: on the background thread, update pointers in the new Script's
  // object graph to point to corresponding objects from the cached Script where
  // appropriate. May only be called if HasPendingBackgroundWork returned true.
  void BeginMergeInBackground(LocalIsolate* isolate, Handle<Script> new_script);

  // Step 3: on the main thread again, complete the merge so that all relevant
  // objects are reachable from the cached Script. May only be called if
  // HasPendingForegroundWork returned true. Returns the top-level
  // SharedFunctionInfo that should be used.
  Handle<SharedFunctionInfo> CompleteMergeInForeground(
      Isolate* isolate, Handle<Script> new_script);

  bool HasPendingBackgroundWork() const {
    return state_ == kPendingBackgroundWork;
  }
  bool HasPendingForegroundWork() const {
    return state_ == kPendingForegroundWork;
  }

 private:
  std::unique_ptr<PersistentHandles> persistent_handles_;

  // Data from main thread:

  MaybeHandle<Script> cached_script_;

  // Data from background thread:

  // The top-level SharedFunctionInfo from the cached script, if one existed,
  // just to keep it alive.
  MaybeHandle<SharedFunctionInfo> toplevel_sfi_from_cached_script_;

  // New SharedFunctionInfos which are used because there was no corresponding
  // SharedFunctionInfo in the cached script. The main thread must:
  // 1. Check whether the cached script gained corresponding SharedFunctionInfos
  //    for any of these, and if so, redo the merge.
  // 2. Update the cached script's shared_function_infos list to refer to these.
  std::vector<Handle<SharedFunctionInfo>> used_new_sfis_;

  // SharedFunctionInfos from the cached script which were not compiled, with
  // function_data and feedback_metadata from the corresponding new
  // SharedFunctionInfo. If the SharedFunctionInfo from the cached script is
  // still uncompiled when finishing, the main thread must set the two fields.
  struct NewCompiledDataForCachedSfi {
    Handle<SharedFunctionInfo> cached_sfi;
    Handle<Object> function_data;
    Handle<FeedbackMetadata> feedback_metadata;
  };
  std::vector<NewCompiledDataForCachedSfi> new_compiled_data_for_cached_sfis_;

  enum State {
    kNotStarted,
    kPendingBackgroundWork,
    kPendingForegroundWork,
    kDone,
  };
  State state_ = kNotStarted;
};

}  // namespace internal
}  // namespace v8

#endif  // V8_CODEGEN_BACKGROUND_MERGE_TASK_H_