per-isolate-compiler-cache.h 1.83 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2018 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_COMPILER_PER_ISOLATE_COMPILER_CACHE_H_
#define V8_COMPILER_PER_ISOLATE_COMPILER_CACHE_H_

#include "src/compiler/refs-map.h"
9
#include "src/execution/isolate.h"
10 11 12 13 14 15 16 17 18 19 20 21
#include "src/zone/zone-containers.h"

namespace v8 {
namespace internal {

class Isolate;
class Zone;

namespace compiler {

class ObjectData;

22 23 24 25 26
// This class serves as a container of data that should persist across all
// (optimizing) compiler runs in an isolate. For now it stores serialized data
// for various common objects such as builtins, so that these objects don't have
// to be serialized in each compilation job. See JSHeapBroker::InitializeRefsMap
// for details.
27 28 29 30 31
class PerIsolateCompilerCache : public ZoneObject {
 public:
  explicit PerIsolateCompilerCache(Zone* zone)
      : zone_(zone), refs_snapshot_(nullptr) {}

32 33 34 35 36
  bool HasSnapshot() const { return refs_snapshot_ != nullptr; }
  RefsMap* GetSnapshot() {
    DCHECK(HasSnapshot());
    return refs_snapshot_;
  }
37
  void SetSnapshot(RefsMap* refs) {
38
    DCHECK(!HasSnapshot());
39 40
    DCHECK(!refs->IsEmpty());
    refs_snapshot_ = new (zone_) RefsMap(refs, zone_);
41
    DCHECK(HasSnapshot());
42 43 44 45 46
  }

  Zone* zone() const { return zone_; }

  static void Setup(Isolate* isolate) {
47 48 49 50 51 52
    if (isolate->compiler_cache() == nullptr) {
      Zone* zone = new Zone(isolate->allocator(), "Compiler zone");
      PerIsolateCompilerCache* cache = new (zone) PerIsolateCompilerCache(zone);
      isolate->set_compiler_utils(cache, zone);
    }
    DCHECK_NOT_NULL(isolate->compiler_cache());
53 54 55 56 57 58 59 60 61 62 63 64
  }

 private:
  Zone* const zone_;
  RefsMap* refs_snapshot_;
};

}  // namespace compiler
}  // namespace internal
}  // namespace v8

#endif  // V8_COMPILER_PER_ISOLATE_COMPILER_CACHE_H_