Skip to content

Commit db6f7fb

Browse files
committed
perf(runtime): make canary states fast-mode objects
A state was made with Object::New and a null prototype, which V8 makes a dictionary-mode object. States are now clones of one empty fast-mode object with a null prototype, so states that get the same names in the same order share their maps' transitions. A 3-name state takes 104 bytes instead of 208. On a phone (release, canary/all, 6 rounds), against the commits before it: a canary's first write takes about 0.75 of its time, a new-name write 0.85, constructing a JS-extended instance with 1-5 fields 0.92-0.94, and a full collection with 10,000 live stateful canaries 0.80-0.88. Rewriting a name a state already has takes about 1.11, and 1.07 across 512 objects.
1 parent 76bb3bc commit db6f7fb

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

‎test-app/runtime/src/main/cpp/ObjectManager.cpp‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ bool ObjectManager::CanaryStateForWrite(const Local<Object>& holder, Local<Objec
972972
if (state == nullptr || !state->keepsState) {
973973
return false;
974974
}
975-
*out = AttachState(state, Object::New(m_isolate, Null(m_isolate), nullptr, nullptr, 0));
975+
*out = AttachState(state, NewEmptyState());
976976
return true;
977977
}
978978

@@ -999,7 +999,21 @@ Local<Object> ObjectManager::NewStateWith(CanaryHandle* canary, Local<Name> prop
999999
(void)existing->CreateDataProperty(m_isolate->GetCurrentContext(), property, value);
10001000
return existing;
10011001
}
1002-
return AttachState(canary, Object::New(m_isolate, Null(m_isolate), &property, &value, 1));
1002+
Local<Object> fresh = NewEmptyState();
1003+
(void)fresh->CreateDataProperty(m_isolate->GetCurrentContext(), property, value);
1004+
return AttachState(canary, fresh);
1005+
}
1006+
1007+
// A fast-mode object with a null prototype: states made from one copy share
1008+
// its map, so states that get the same names in the same order share their
1009+
// maps' transitions. Object::New with a null prototype makes a dictionary.
1010+
Local<Object> ObjectManager::NewEmptyState() {
1011+
if (m_emptyState.IsEmpty()) {
1012+
Local<Object> empty = Object::New(m_isolate);
1013+
(void)empty->SetPrototype(m_isolate->GetCurrentContext(), Null(m_isolate));
1014+
m_emptyState.Reset(m_isolate, empty);
1015+
}
1016+
return m_emptyState.Get(m_isolate)->Clone(m_isolate);
10031017
}
10041018

10051019
Local<Object> ObjectManager::StateOf(CanaryHandle* canary) {
@@ -1023,7 +1037,7 @@ Local<Object> ObjectManager::StateForWrite(CanaryHandle* canary) {
10231037
if (!existing.IsEmpty()) {
10241038
return existing;
10251039
}
1026-
return AttachState(canary, Object::New(m_isolate, Null(m_isolate), nullptr, nullptr, 0));
1040+
return AttachState(canary, NewEmptyState());
10271041
}
10281042

10291043
void ObjectManager::CanaryDiedStatic(
@@ -1509,6 +1523,7 @@ void ObjectManager::ReleaseAllRegistered() {
15091523
m_canaryRecords.DrainAll([](CanaryRecord* record) { delete record; });
15101524
m_canaryRecordCount = 0;
15111525
m_instanceFieldTableKey.Reset();
1526+
m_emptyState.Reset();
15121527

15131528
auto jsInfoIdx = static_cast<int>(MetadataNodeKeys::JsInfo);
15141529

‎test-app/runtime/src/main/cpp/ObjectManager.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,9 @@ class ObjectManager {
544544
bool m_canaryEpilogueAdded = false;
545545
int m_deathInboxRuntimeId = -1;
546546
v8::Persistent<v8::Private> m_instanceFieldTableKey;
547+
// Cloned for each new state; never handed to JS.
548+
v8::Global<v8::Object> m_emptyState;
549+
v8::Local<v8::Object> NewEmptyState();
547550

548551
void JSObjectFinalizer(v8::Isolate* isolate,
549552
ObjectWeakCallbackState* callbackState);

0 commit comments

Comments
 (0)