Skip to content

Commit c5ad279

Browse files
committed
perf(runtime): keep canary records in the object id index
Canary records lived in an unordered_map keyed by Java object id, so every canary registration paid a hashed lookup, and every record a node allocation and a hashed insert and erase. They now live in the same radix index the wrapper registrations use.
1 parent 416d715 commit c5ad279

2 files changed

Lines changed: 31 additions & 21 deletions

File tree

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

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -640,11 +640,9 @@ ObjectManager::CanaryRecord* ObjectManager::AdoptCanary(
640640
object->GetInternalField(static_cast<int>(MetadataNodeKeys::CallSuper)).As<Value>(),
641641
object->GetInternalField(static_cast<int>(MetadataNodeKeys::Metadata)).As<Value>(),
642642
implementationObject);
643-
auto created = std::make_unique<CanaryRecord>();
644-
created->cls = cls;
643+
record = NewCanaryRecord(javaObjectID);
644+
record->cls = cls;
645645
cls->records++;
646-
record = created.get();
647-
m_canaryRecords.emplace(javaObjectID, std::move(created));
648646
return record;
649647
}
650648
if (!EnsureJavaStrong(javaObjectID, record)) {
@@ -702,21 +700,30 @@ ObjectManager::CanaryClass* ObjectManager::CanaryClassFor(
702700
}
703701

704702
ObjectManager::CanaryRecord* ObjectManager::FindCanaryRecord(int javaObjectID) {
705-
if (m_canaryRecords.empty()) {
706-
return nullptr;
703+
return m_canaryRecordCount == 0 ? nullptr : m_canaryRecords.Find(javaObjectID);
704+
}
705+
706+
ObjectManager::CanaryRecord* ObjectManager::NewCanaryRecord(int javaObjectID) {
707+
auto record = new CanaryRecord();
708+
record->id = javaObjectID;
709+
if (!m_canaryRecords.Insert(javaObjectID, record)) {
710+
NS_DCHECK(false);
711+
delete record;
712+
return m_canaryRecords.Find(javaObjectID);
707713
}
708-
auto it = m_canaryRecords.find(javaObjectID);
709-
return it != m_canaryRecords.end() ? it->second.get() : nullptr;
714+
m_canaryRecordCount++;
715+
return record;
710716
}
711717

712718
void ObjectManager::EraseCanaryRecord(int javaObjectID) {
713719
if (auto live = m_idToObject.Find(javaObjectID)) {
714720
live->record = nullptr;
715721
}
716-
auto it = m_canaryRecords.find(javaObjectID);
717-
if (it != m_canaryRecords.end()) {
718-
ForgetStateNames(it->second.get());
719-
m_canaryRecords.erase(it);
722+
if (CanaryRecord* record = FindCanaryRecord(javaObjectID)) {
723+
ForgetStateNames(record);
724+
m_canaryRecords.Erase(javaObjectID);
725+
m_canaryRecordCount--;
726+
delete record;
720727
}
721728
}
722729

@@ -975,9 +982,7 @@ Local<Object> ObjectManager::AttachState(CanaryHandle* canary, const Local<Objec
975982
record = FindCanaryRecord(canary->javaObjectID);
976983
}
977984
if (record == nullptr) {
978-
auto created = std::make_unique<CanaryRecord>();
979-
record = created.get();
980-
m_canaryRecords.emplace(canary->javaObjectID, std::move(created));
985+
record = NewCanaryRecord(canary->javaObjectID);
981986
}
982987
canary->record = record;
983988
record->state.Reset(m_isolate, fresh);
@@ -1171,16 +1176,16 @@ std::map<std::string, ObjectManager::ClassCounts> ObjectManager::GetWrapperClass
11711176
entry.canaries++;
11721177
}
11731178
});
1174-
for (const auto& [id, record] : m_canaryRecords) {
1179+
m_canaryRecords.ForEach([&](CanaryRecord* record) {
11751180
if (record->state.IsEmpty()) {
1176-
continue;
1181+
return;
11771182
}
11781183
jclass clazz = record->clazz;
1179-
if (auto live = m_idToObject.Find(id)) {
1184+
if (auto live = m_idToObject.Find(record->id)) {
11801185
clazz = live->objectClazz;
11811186
}
11821187
counts[nameOf(clazz)].states++;
1183-
}
1188+
});
11841189
return counts;
11851190
}
11861191

@@ -1501,7 +1506,8 @@ void ObjectManager::ReleaseAllRegistered() {
15011506
}
15021507
}
15031508
std::fill(&m_fieldNameTags[0][0], &m_fieldNameTags[0][0] + kFieldNameSets * kFieldNameWays, 0u);
1504-
m_canaryRecords.clear();
1509+
m_canaryRecords.DrainAll([](CanaryRecord* record) { delete record; });
1510+
m_canaryRecordCount = 0;
15051511
m_instanceFieldTableKey.Reset();
15061512

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

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ class ObjectManager {
350350
CanaryRecord(const CanaryRecord&) = delete;
351351
CanaryRecord& operator=(const CanaryRecord&) = delete;
352352
~CanaryRecord();
353+
int id = -1;
353354
// The object's JS properties; empty until the first write.
354355
v8::Global<v8::Object> state;
355356
// JS-extended instances only.
@@ -533,7 +534,10 @@ class ObjectManager {
533534
const v8::Local<v8::Value>& callSuper,
534535
const v8::Local<v8::Value>& metadata,
535536
const v8::Local<v8::Value>& implementationObject);
536-
std::unordered_map<int, std::unique_ptr<CanaryRecord>> m_canaryRecords;
537+
// Owns the records.
538+
ObjectIdIndex<CanaryRecord> m_canaryRecords;
539+
size_t m_canaryRecordCount = 0;
540+
CanaryRecord* NewCanaryRecord(int javaObjectID);
537541
std::vector<jint> m_pendingCanaryDeaths;
538542
bool m_copyingCanaryProperties = false;
539543
bool m_deathInboxOpen = false;

0 commit comments

Comments
 (0)