[vm] Clean up New() methods of Dart classes.

Goals of this CL, a followup to 2f63acea22:

* Ensure that X::New() depends on the initialization guarantees of
  Object::Allocate<X>(...):

  * Ptr fields are guaranteed to be initialized to Object::null().
  * Non-Ptr fields are guaranteed to be zero-initialized.

  In cases where the only uses of the allocated object before return
  was to perform unnecessary field assignments, X::New() just simply
  returns the result of Object::Allocate<X>(...).

  Otherwise, the old now-unnecessary assignments have been changed
  into ASSERTs so that they will be checked in DEBUG mode.

* Ensure that NoSafepointScopes are entered in X::New() only when
  necessary (e.g., to ensure fields used to calculate to(...)
  are properly set before being seen by pointer visitors as the GC
  may run when outside a NoSafepointScope).

  In particular, the often occurring pattern:

  auto& result = X::Handle();
  {
    auto raw = Object::Allocate<X>(...);
    NoSafepointScope no_safepoint;
    result = raw;
  }
  ...

  has been replaced with:

  const auto& result = X::Handle(Object::Allocate<X>(...));

* If a handle was allocated, the only uses of that handle before
  returning must be performed under a NoSafepointScope, and the same
  operations can be done directly on the object pointer, then do so
  and remove the unnecessary handle allocation.

Notable changes outside the above:

* Swapped ObjectPool::EntryType::{kImmediate,kTaggedObject} so that
  kImmediate has value 0, since Object::Allocate<ObjectPool>(len)
  zero-initializes the payload and without this change,
  ObjectPool::New() must set the entry types manually.

* Removed the old static ArrayPtr cached_array_ field on
  SubtypeTestCache as well as SubtypeTestCache::{Init,Cleanup} and
  instead added Object::empty_subtype_test_cache_array().

* Removed the no-arg Closure::New() method, which is never used.

* Inlined the no-arg Script::New() method into its only caller,
  one of the other Script::New() overloads.

TEST=ci

Issue: https://github.com/dart-lang/sdk/issues/52876
Change-Id: I079b4c9f73c7d2c0146c30cf2cd570b91a1ecf36
Cq-Include-Trybots: luci.dart.try:vm-linux-debug-x64-try,vm-linux-debug-x64c-try,vm-linux-release-x64-try,vm-aot-linux-product-x64-try,vm-aot-linux-release-x64-try,vm-aot-linux-debug-x64-try,vm-ffi-qemu-linux-release-riscv64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/313120
Reviewed-by: Ryan Macnak <rmacnak@google.com>
Commit-Queue: Tess Strickland <sstrickl@google.com>
This commit is contained in:
Tess Strickland 2023-07-12 07:37:16 +00:00 committed by Commit Queue
parent 23f4145206
commit 86d1261dff
5 changed files with 262 additions and 514 deletions

View file

@ -6051,6 +6051,8 @@ class VMSerializationRoots : public SerializationRoots {
s->AddBaseObject(Object::empty_array().ptr(), "Array", "<empty_array>");
s->AddBaseObject(Object::empty_instantiations_cache_array().ptr(), "Array",
"<empty_instantiations_cache_array>");
s->AddBaseObject(Object::empty_subtype_test_cache_array().ptr(), "Array",
"<empty_subtype_test_cache_array>");
s->AddBaseObject(Object::dynamic_type().ptr(), "Type", "<dynamic type>");
s->AddBaseObject(Object::void_type().ptr(), "Type", "<void type>");
s->AddBaseObject(Object::empty_type_arguments().ptr(), "TypeArguments",
@ -6086,8 +6088,6 @@ class VMSerializationRoots : public SerializationRoots {
s->AddBaseObject(ICData::cached_icdata_arrays_[i], "Array",
"<empty icdata entries>");
}
s->AddBaseObject(SubtypeTestCache::cached_array_, "Array",
"<empty subtype entries>");
ClassTable* table = s->isolate_group()->class_table();
for (intptr_t cid = kFirstInternalOnlyCid; cid <= kLastInternalOnlyCid;
@ -6174,6 +6174,7 @@ class VMDeserializationRoots : public DeserializationRoots {
d->AddBaseObject(Object::optimized_out().ptr());
d->AddBaseObject(Object::empty_array().ptr());
d->AddBaseObject(Object::empty_instantiations_cache_array().ptr());
d->AddBaseObject(Object::empty_subtype_test_cache_array().ptr());
d->AddBaseObject(Object::dynamic_type().ptr());
d->AddBaseObject(Object::void_type().ptr());
d->AddBaseObject(Object::empty_type_arguments().ptr());
@ -6197,7 +6198,6 @@ class VMDeserializationRoots : public DeserializationRoots {
for (intptr_t i = 0; i < ICData::kCachedICDataArrayCount; i++) {
d->AddBaseObject(ICData::cached_icdata_arrays_[i]);
}
d->AddBaseObject(SubtypeTestCache::cached_array_);
ClassTable* table = d->isolate_group()->class_table();
for (intptr_t cid = kFirstInternalOnlyCid; cid <= kLastInternalOnlyCid;

View file

@ -21,13 +21,13 @@ bool IsSameObject(const Object& a, const Object& b);
struct ObjectPoolBuilderEntry {
enum Patchability {
kPatchable,
kPatchable = 0,
kNotPatchable,
};
enum EntryType {
kImmediate = 0,
kTaggedObject,
kImmediate,
kNativeFunction,
// Used only during AOT snapshot serialization/deserialization.

View file

@ -400,7 +400,6 @@ char* Dart::DartInit(const Dart_InitializeParams* params) {
OffsetsTable::Init();
ArgumentsDescriptor::Init();
ICData::Init();
SubtypeTestCache::Init();
if (params->vm_snapshot_data != nullptr) {
#if defined(SUPPORT_TIMELINE)
TimelineBeginEndScope tbes(Timeline::GetVMStream(), "ReadVMSnapshot");
@ -747,7 +746,6 @@ char* Dart::Cleanup() {
UserTags::Cleanup();
IsolateGroup::Cleanup();
ICData::Cleanup();
SubtypeTestCache::Cleanup();
ArgumentsDescriptor::Cleanup();
OffsetsTable::Cleanup();
FfiCallbackMetadata::Cleanup();

File diff suppressed because it is too large Load diff

View file

@ -467,6 +467,7 @@ class Object {
V(TypeArguments, empty_type_arguments) \
V(Array, empty_array) \
V(Array, empty_instantiations_cache_array) \
V(Array, empty_subtype_test_cache_array) \
V(ContextScope, empty_context_scope) \
V(ObjectPool, empty_object_pool) \
V(CompressedStackMaps, empty_compressed_stackmaps) \
@ -2175,7 +2176,7 @@ class Class : public Object {
intptr_t ComputeNumTypeArguments() const;
// Assigns empty array to all raw class array fields.
void InitEmptyFields();
void InitEmptyFields() const;
static FunctionPtr CheckFunctionType(const Function& func, MemberKind kind);
FunctionPtr LookupFunctionReadLocked(const String& name,
@ -4957,8 +4958,6 @@ class Script : public Object {
void set_load_timestamp(int64_t value) const;
ArrayPtr debug_positions() const;
static ScriptPtr New();
FINAL_HEAP_OBJECT_IMPLEMENTATION(Script, Object);
friend class Class;
friend class Precompiler;
@ -7708,9 +7707,6 @@ class SubtypeTestCache : public Object {
return RoundedAllocationSize(sizeof(UntaggedSubtypeTestCache));
}
static void Init();
static void Cleanup();
static intptr_t cache_offset() {
return OFFSET_OF(UntaggedSubtypeTestCache, cache_);
}
@ -7858,10 +7854,6 @@ class SubtypeTestCache : public Object {
BaseTextBuffer* buffer,
const char* line_prefix = nullptr) const;
// An array where each entry is an array that is a VM heap allocated
// preinitialized empty subtype entry array.
static ArrayPtr cached_array_;
FINAL_HEAP_OBJECT_IMPLEMENTATION(SubtypeTestCache, Object);
friend class Class;
friend class FieldInvalidator;
@ -7994,7 +7986,7 @@ class LanguageError : public Error {
void set_token_pos(TokenPosition value) const;
bool report_after_token() const { return untag()->report_after_token_; }
void set_report_after_token(bool value);
void set_report_after_token(bool value) const;
void set_kind(uint8_t value) const;
@ -12456,8 +12448,6 @@ class Closure : public Instance {
FunctionTypePtr GetInstantiatedSignature(Zone* zone) const;
private:
static ClosurePtr New();
FINAL_HEAP_OBJECT_IMPLEMENTATION(Closure, Instance);
friend class Class;
};