Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit ecd8216

Browse files
mkustermanncommit-bot@chromium.org
authored andcommitted
[vm/concurrency] Avoid allocating objects if not needed (and possibly cache objects)
Two growable arrays needed for possibly re-hashing objects when sending messages using the snappshotter are often unused. Only create them when needed. When looking up message handler and calling it avoid repeated allocations of small arrays (they can be cached savely because right the transition stubs will copy the arguments out of those arrays). This improves performance of high-frequency messages but small message handlers. Issue dart-lang/sdk#36097 Change-Id: I623aba5b29064f3a75ea294557ee153067a63d08 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/148263 Reviewed-by: Ryan Macnak <[email protected]> Commit-Queue: Martin Kustermann <[email protected]>
1 parent 34bf674 commit ecd8216

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

runtime/vm/dart_entry.cc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,12 @@ ObjectPtr DartLibraryCalls::LookupHandler(Dart_Port port_id) {
658658
ASSERT(!function.IsNull());
659659
thread->isolate()->object_store()->set_lookup_port_handler(function);
660660
}
661-
const Array& args = Array::Handle(zone, Array::New(kNumArguments));
661+
Array& args = Array::Handle(
662+
zone, thread->isolate()->isolate_object_store()->dart_args_1());
663+
if (args.IsNull()) {
664+
args = Array::New(kNumArguments);
665+
thread->isolate()->isolate_object_store()->set_dart_args_1(args);
666+
}
662667
args.SetAt(0, Integer::Handle(zone, Integer::New(port_id)));
663668
const Object& result =
664669
Object::Handle(zone, DartEntry::InvokeFunction(function, args));
@@ -687,7 +692,12 @@ ObjectPtr DartLibraryCalls::HandleMessage(const Object& handler,
687692
ASSERT(!function.IsNull());
688693
isolate->object_store()->set_handle_message_function(function);
689694
}
690-
const Array& args = Array::Handle(zone, Array::New(kNumArguments));
695+
Array& args = Array::Handle(
696+
zone, thread->isolate()->isolate_object_store()->dart_args_2());
697+
if (args.IsNull()) {
698+
args = Array::New(kNumArguments);
699+
thread->isolate()->isolate_object_store()->set_dart_args_2(args);
700+
}
691701
args.SetAt(0, handler);
692702
args.SetAt(1, message);
693703
#if !defined(PRODUCT)

runtime/vm/object_store.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ class ObjectPointerVisitor;
254254
#define ISOLATE_OBJECT_STORE_FIELD_LIST(R_, RW) \
255255
RW(UnhandledException, preallocated_unhandled_exception) \
256256
RW(StackTrace, preallocated_stack_trace) \
257+
RW(Array, dart_args_1) \
258+
RW(Array, dart_args_2) \
257259
R_(GrowableObjectArray, resume_capabilities) \
258260
R_(GrowableObjectArray, exit_listeners) \
259261
R_(GrowableObjectArray, error_listeners)

runtime/vm/snapshot.cc

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,6 @@ ObjectPtr SnapshotReader::ReadObject() {
313313
// Setup for long jump in case there is an exception while reading.
314314
LongJumpScope jump;
315315
if (setjmp(*jump.Set()) == 0) {
316-
objects_to_rehash_ = GrowableObjectArray::New();
317-
types_to_postprocess_ = GrowableObjectArray::New();
318-
319316
PassiveObject& obj =
320317
PassiveObject::Handle(zone(), ReadObjectImpl(kAsInlinedObject));
321318
for (intptr_t i = 0; i < backward_references_->length(); i++) {
@@ -344,11 +341,14 @@ ObjectPtr SnapshotReader::ReadObject() {
344341
}
345342

346343
void SnapshotReader::EnqueueTypePostprocessing(const AbstractType& type) {
344+
if (types_to_postprocess_.IsNull()) {
345+
types_to_postprocess_ = GrowableObjectArray::New();
346+
}
347347
types_to_postprocess_.Add(type);
348348
}
349349

350350
void SnapshotReader::RunDelayedTypePostprocessing() {
351-
if (types_to_postprocess_.Length() == 0) {
351+
if (types_to_postprocess_.IsNull()) {
352352
return;
353353
}
354354

@@ -362,11 +362,14 @@ void SnapshotReader::RunDelayedTypePostprocessing() {
362362
}
363363

364364
void SnapshotReader::EnqueueRehashingOfMap(const LinkedHashMap& map) {
365+
if (objects_to_rehash_.IsNull()) {
366+
objects_to_rehash_ = GrowableObjectArray::New();
367+
}
365368
objects_to_rehash_.Add(map);
366369
}
367370

368371
ObjectPtr SnapshotReader::RunDelayedRehashingOfMaps() {
369-
if (objects_to_rehash_.Length() > 0) {
372+
if (!objects_to_rehash_.IsNull()) {
370373
const Library& collections_lib =
371374
Library::Handle(zone_, Library::CollectionLibrary());
372375
const Function& rehashing_function = Function::Handle(
@@ -575,6 +578,9 @@ ObjectPtr SnapshotReader::ReadObjectImpl(intptr_t header_value,
575578
}
576579

577580
void SnapshotReader::EnqueueRehashingOfSet(const Object& set) {
581+
if (objects_to_rehash_.IsNull()) {
582+
objects_to_rehash_ = GrowableObjectArray::New();
583+
}
578584
objects_to_rehash_.Add(set);
579585
}
580586

0 commit comments

Comments
 (0)