@@ -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
704702ObjectManager::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
712718void 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);
0 commit comments