896e535fe9
We support allocating deeply immutable typed data in the Dart C API
already.
=> This CL adds a test for that.
Furthermore we hook into the already O(n) construction of
`List.unmodifiable(<list>)` and set the objects immutable bit
if all elements of `<list>` are also immutable.
=> This allows then sharing of such lists when sent across isolates.
We do this eagerly to
- avoid introducing complexity to the deep immutability check in
transitive copy algorithm
- avoids re-checking for the subgraph on every send (similar to
canonicalized bit for constants, we can simply rely on the
immutable bit)
If we ever optimized `List.unmodifiable()` (which always performs
runtime call atm) we could fold this checking into the place that copies
list elements from `<list>` to the newly allocated list.
Issue https://github.com/dart-lang/sdk/issues/51302
TEST=vm/dart{,_2}/isolates/fast_object_copy2_test
Change-Id: Ie275e65036a4c53d992804972aae741a9f5ed6dd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/281424
Reviewed-by: Ryan Macnak <rmacnak@google.com>
Reviewed-by: Alexander Aprelev <aam@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
108 lines
3.8 KiB
C++
108 lines
3.8 KiB
C++
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
#include "platform/assert.h"
|
|
|
|
#include "vm/bootstrap_natives.h"
|
|
#include "vm/exceptions.h"
|
|
#include "vm/native_entry.h"
|
|
#include "vm/object.h"
|
|
#include "vm/object_graph_copy.h"
|
|
|
|
namespace dart {
|
|
|
|
DEFINE_NATIVE_ENTRY(GrowableList_allocate, 0, 2) {
|
|
const TypeArguments& type_arguments =
|
|
TypeArguments::CheckedHandle(zone, arguments->NativeArgAt(0));
|
|
GET_NON_NULL_NATIVE_ARGUMENT(Array, data, arguments->NativeArgAt(1));
|
|
if (data.Length() < 0) {
|
|
Exceptions::ThrowRangeError("length",
|
|
Integer::Handle(Integer::New(data.Length())),
|
|
0, // This is the limit the user sees.
|
|
Array::kMaxElements);
|
|
}
|
|
const GrowableObjectArray& new_array =
|
|
GrowableObjectArray::Handle(GrowableObjectArray::New(data));
|
|
new_array.SetTypeArguments(type_arguments);
|
|
return new_array.ptr();
|
|
}
|
|
|
|
DEFINE_NATIVE_ENTRY(GrowableList_getIndexed, 0, 2) {
|
|
const GrowableObjectArray& array =
|
|
GrowableObjectArray::CheckedHandle(zone, arguments->NativeArgAt(0));
|
|
GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1));
|
|
if ((index.Value() < 0) || (index.Value() >= array.Length())) {
|
|
Exceptions::ThrowRangeError("index", index, 0, array.Length() - 1);
|
|
}
|
|
const Instance& obj = Instance::CheckedHandle(zone, array.At(index.Value()));
|
|
return obj.ptr();
|
|
}
|
|
|
|
DEFINE_NATIVE_ENTRY(GrowableList_setIndexed, 0, 3) {
|
|
const GrowableObjectArray& array =
|
|
GrowableObjectArray::CheckedHandle(zone, arguments->NativeArgAt(0));
|
|
GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1));
|
|
if ((index.Value() < 0) || (index.Value() >= array.Length())) {
|
|
Exceptions::ThrowRangeError("index", index, 0, array.Length() - 1);
|
|
}
|
|
GET_NON_NULL_NATIVE_ARGUMENT(Instance, value, arguments->NativeArgAt(2));
|
|
array.SetAt(index.Value(), value);
|
|
return Object::null();
|
|
}
|
|
|
|
DEFINE_NATIVE_ENTRY(GrowableList_getLength, 0, 1) {
|
|
const GrowableObjectArray& array =
|
|
GrowableObjectArray::CheckedHandle(zone, arguments->NativeArgAt(0));
|
|
return Smi::New(array.Length());
|
|
}
|
|
|
|
DEFINE_NATIVE_ENTRY(GrowableList_getCapacity, 0, 1) {
|
|
const GrowableObjectArray& array =
|
|
GrowableObjectArray::CheckedHandle(zone, arguments->NativeArgAt(0));
|
|
return Smi::New(array.Capacity());
|
|
}
|
|
|
|
DEFINE_NATIVE_ENTRY(GrowableList_setLength, 0, 2) {
|
|
const GrowableObjectArray& array =
|
|
GrowableObjectArray::CheckedHandle(zone, arguments->NativeArgAt(0));
|
|
GET_NON_NULL_NATIVE_ARGUMENT(Smi, length, arguments->NativeArgAt(1));
|
|
ASSERT((length.Value() >= 0) && (length.Value() <= array.Capacity()));
|
|
array.SetLength(length.Value());
|
|
return Object::null();
|
|
}
|
|
|
|
DEFINE_NATIVE_ENTRY(GrowableList_setData, 0, 2) {
|
|
const GrowableObjectArray& array =
|
|
GrowableObjectArray::CheckedHandle(zone, arguments->NativeArgAt(0));
|
|
GET_NON_NULL_NATIVE_ARGUMENT(Array, data, arguments->NativeArgAt(1));
|
|
ASSERT(data.Length() >= 0);
|
|
array.SetData(data);
|
|
return Object::null();
|
|
}
|
|
|
|
DEFINE_NATIVE_ENTRY(Internal_makeListFixedLength, 0, 1) {
|
|
GET_NON_NULL_NATIVE_ARGUMENT(GrowableObjectArray, array,
|
|
arguments->NativeArgAt(0));
|
|
return Array::MakeFixedLength(array, /* unique = */ true);
|
|
}
|
|
|
|
DEFINE_NATIVE_ENTRY(Internal_makeFixedListUnmodifiable, 0, 1) {
|
|
GET_NON_NULL_NATIVE_ARGUMENT(Array, array, arguments->NativeArgAt(0));
|
|
array.MakeImmutable();
|
|
const intptr_t len = array.Length();
|
|
bool is_deeply_immutable = true;
|
|
for (intptr_t i = 0; i < len; ++i) {
|
|
if (!CanShareObjectAcrossIsolates(array.At(i))) {
|
|
is_deeply_immutable = false;
|
|
break;
|
|
}
|
|
}
|
|
if (is_deeply_immutable) {
|
|
array.SetImmutable();
|
|
}
|
|
return array.ptr();
|
|
}
|
|
|
|
} // namespace dart
|