Skip to content

Commit 0485f1d

Browse files
committed
优化了
CAICHSyncThread::Run中的O(n^2)部分 Kad中的关键字处理 CSharedFileList::IsFilePtrInList
1 parent 6a8f937 commit 0485f1d

File tree

4 files changed

+82
-75
lines changed

4 files changed

+82
-75
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,6 @@ FakesAssemblies/
186186
# LightSwitch generated files
187187
GeneratedArtifacts/
188188
_Pvt_Extensions/
189-
ModelManifest.xml
189+
ModelManifest.xml
190+
debug100/
191+
release/

AICHSyncThread.cpp

+66-65
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,20 @@ int CAICHSyncThread::Run()
252252
// now we check that all files which are in the sharedfilelist have a corresponding hash in out list
253253
// those who don'T are added to the hashinglist
254254
CList<CAICHHash> liUsedHashs;
255+
CMap<CAICHHash, const CAICHHash&, bool, bool> mapKnown2Hashes;
256+
CMap<CAICHHash, const CAICHHash&, bool, bool> mapKnown2Hashes_un;
255257
CSingleLock sharelock(&theApp.sharedfiles->m_mutWriteList);
256258
sharelock.Lock();
257259

258260
bool bDbgMsgCreatingPartHashs = true;
261+
for (POSITION pos = liKnown2Hashs.GetHeadPosition(); pos != 0;) {
262+
CAICHHash current_hash = liKnown2Hashs.GetNext(pos);
263+
mapKnown2Hashes.SetAt(current_hash, true);
264+
}
265+
for (POSITION pos = liKnown2Hashs_un.GetHeadPosition(); pos != 0;) {
266+
CAICHHash current_hash = liKnown2Hashs_un.GetNext(pos);
267+
mapKnown2Hashes_un.SetAt(current_hash, true);
268+
}
259269
for (int i = 0; i < theApp.sharedfiles->GetCount(); i++){
260270
CKnownFile* pCurFile = theApp.sharedfiles->GetFileByIndex(i);
261271
if (pCurFile != NULL && !pCurFile->IsPartFile() )
@@ -264,85 +274,76 @@ int CAICHSyncThread::Run()
264274
return 0;
265275
if (pCurFile->GetFileIdentifier().HasAICHHash()){
266276
bool bFound = false;
267-
for (POSITION pos = liKnown2Hashs.GetHeadPosition();pos != 0;)
268-
{
269-
CAICHHash current_hash = liKnown2Hashs.GetNext(pos);
270-
if (current_hash == pCurFile->GetFileIdentifier().GetAICHHash()){
271-
bFound = true;
272-
liUsedHashs.AddTail(current_hash);
273-
pCurFile->SetAICHRecoverHashSetAvailable(true);
274-
// Has the file the proper AICH Parthashset? If not probably upgrading, create it
275-
if (!pCurFile->GetFileIdentifier().HasExpectedAICHHashCount())
277+
if (mapKnown2Hashes.Lookup(pCurFile->GetFileIdentifier().GetAICHHash(), bFound)) {
278+
bFound = true;
279+
liUsedHashs.AddTail(pCurFile->GetFileIdentifier().GetAICHHash());
280+
pCurFile->SetAICHRecoverHashSetAvailable(true);
281+
// Has the file the proper AICH Parthashset? If not probably upgrading, create it
282+
if (!pCurFile->GetFileIdentifier().HasExpectedAICHHashCount())
283+
{
284+
if (bDbgMsgCreatingPartHashs)
276285
{
277-
if (bDbgMsgCreatingPartHashs)
278-
{
279-
bDbgMsgCreatingPartHashs = false;
280-
DebugLogWarning(_T("Missing AICH Part Hashsets for known files - maybe upgrading from earlier version. Creating them out of full AICH Recovery Hashsets, shouldn't take too long"));
281-
}
282-
CAICHRecoveryHashSet tempHashSet(pCurFile, pCurFile->GetFileSize());
283-
tempHashSet.SetMasterHash(pCurFile->GetFileIdentifier().GetAICHHash(), AICH_HASHSETCOMPLETE);
284-
if (!tempHashSet.LoadHashSet())
286+
bDbgMsgCreatingPartHashs = false;
287+
DebugLogWarning(_T("Missing AICH Part Hashsets for known files - maybe upgrading from earlier version. Creating them out of full AICH Recovery Hashsets, shouldn't take too long"));
288+
}
289+
CAICHRecoveryHashSet tempHashSet(pCurFile, pCurFile->GetFileSize());
290+
tempHashSet.SetMasterHash(pCurFile->GetFileIdentifier().GetAICHHash(), AICH_HASHSETCOMPLETE);
291+
if (!tempHashSet.LoadHashSet())
292+
{
293+
ASSERT( false );
294+
DebugLogError(_T("Failed to load full AICH Recovery Hashset - known2.met might be corrupt. Unable to create AICH Part Hashset - %s"), pCurFile->GetFileName());
295+
}
296+
else
297+
{
298+
if (!pCurFile->GetFileIdentifier().SetAICHHashSet(tempHashSet))
285299
{
300+
DebugLogError(_T("Failed to create AICH Part Hashset out of full AICH Recovery Hashset - %s"), pCurFile->GetFileName());
286301
ASSERT( false );
287-
DebugLogError(_T("Failed to load full AICH Recovery Hashset - known2.met might be corrupt. Unable to create AICH Part Hashset - %s"), pCurFile->GetFileName());
288-
}
289-
else
290-
{
291-
if (!pCurFile->GetFileIdentifier().SetAICHHashSet(tempHashSet))
292-
{
293-
DebugLogError(_T("Failed to create AICH Part Hashset out of full AICH Recovery Hashset - %s"), pCurFile->GetFileName());
294-
ASSERT( false );
295-
}
296-
ASSERT(pCurFile->GetFileIdentifier().HasExpectedAICHHashCount());
297302
}
303+
ASSERT(pCurFile->GetFileIdentifier().HasExpectedAICHHashCount());
298304
}
299-
//theApp.QueueDebugLogLine(false, _T("%s - %s"), current_hash.GetString(), pCurFile->GetFileName());
300-
/*#ifdef _DEBUG
301-
// in debugmode we load and verify all hashsets
302-
CAICHRecoveryHashSet* pTempHashSet = new CAICHRecoveryHashSet(pCurFile);
303-
pTempHashSet->SetFileSize(pCurFile->GetFileSize());
304-
pTempHashSet->SetMasterHash(pCurFile->GetFileIdentifier().GetAICHHash(), AICH_HASHSETCOMPLETE)
305-
ASSERT( pTempHashSet->LoadHashSet() );
306-
delete pTempHashSet;
307-
#endif*/
308-
break;
309305
}
306+
//theApp.QueueDebugLogLine(false, _T("%s - %s"), current_hash.GetString(), pCurFile->GetFileName());
307+
/*#ifdef _DEBUG
308+
// in debugmode we load and verify all hashsets
309+
CAICHRecoveryHashSet* pTempHashSet = new CAICHRecoveryHashSet(pCurFile);
310+
pTempHashSet->SetFileSize(pCurFile->GetFileSize());
311+
pTempHashSet->SetMasterHash(pCurFile->GetFileIdentifier().GetAICHHash(), AICH_HASHSETCOMPLETE)
312+
ASSERT( pTempHashSet->LoadHashSet() );
313+
delete pTempHashSet;
314+
#endif*/
310315
}
311316
//zz_fly :: known2 split :: start
312317
//the hashset is found in known2_unshared.met
313-
for (POSITION pos = liKnown2Hashs_un.GetHeadPosition();pos != 0;){
314-
CAICHHash current_hash = liKnown2Hashs_un.GetNext(pos);
315-
if (current_hash == pCurFile->GetFileIdentifier().GetAICHHash()){
316-
bFound = true;
317-
liUsedHashs.AddTail(current_hash);
318-
liHashsMoveToKnown2.AddTail(current_hash);
319-
pCurFile->SetAICHRecoverHashSetAvailable(true);
320-
// Has the file the proper AICH Parthashset? If not probably upgrading, create it
321-
if (!pCurFile->GetFileIdentifier().HasExpectedAICHHashCount())
318+
if (mapKnown2Hashes_un.Lookup(pCurFile->GetFileIdentifier().GetAICHHash(), bFound)){
319+
bFound = true;
320+
liUsedHashs.AddTail(pCurFile->GetFileIdentifier().GetAICHHash());
321+
liHashsMoveToKnown2.AddTail(pCurFile->GetFileIdentifier().GetAICHHash());
322+
pCurFile->SetAICHRecoverHashSetAvailable(true);
323+
// Has the file the proper AICH Parthashset? If not probably upgrading, create it
324+
if (!pCurFile->GetFileIdentifier().HasExpectedAICHHashCount())
325+
{
326+
if (bDbgMsgCreatingPartHashs)
322327
{
323-
if (bDbgMsgCreatingPartHashs)
324-
{
325-
bDbgMsgCreatingPartHashs = false;
326-
DebugLogWarning(_T("Missing AICH Part Hashsets for known files - maybe upgrading from earlier version. Creating them out of full AICH Recovery Hashsets, shouldn't take too long"));
327-
}
328-
CAICHRecoveryHashSet tempHashSet(pCurFile, pCurFile->GetFileSize());
329-
tempHashSet.SetMasterHash(pCurFile->GetFileIdentifier().GetAICHHash(), AICH_HASHSETCOMPLETE);
330-
if (!tempHashSet.LoadHashSet())
328+
bDbgMsgCreatingPartHashs = false;
329+
DebugLogWarning(_T("Missing AICH Part Hashsets for known files - maybe upgrading from earlier version. Creating them out of full AICH Recovery Hashsets, shouldn't take too long"));
330+
}
331+
CAICHRecoveryHashSet tempHashSet(pCurFile, pCurFile->GetFileSize());
332+
tempHashSet.SetMasterHash(pCurFile->GetFileIdentifier().GetAICHHash(), AICH_HASHSETCOMPLETE);
333+
if (!tempHashSet.LoadHashSet())
334+
{
335+
ASSERT( false );
336+
DebugLogError(_T("Failed to load full AICH Recovery Hashset - known2.met might be corrupt. Unable to create AICH Part Hashset - %s"), pCurFile->GetFileName());
337+
}
338+
else
339+
{
340+
if (!pCurFile->GetFileIdentifier().SetAICHHashSet(tempHashSet))
331341
{
342+
DebugLogError(_T("Failed to create AICH Part Hashset out of full AICH Recovery Hashset - %s"), pCurFile->GetFileName());
332343
ASSERT( false );
333-
DebugLogError(_T("Failed to load full AICH Recovery Hashset - known2.met might be corrupt. Unable to create AICH Part Hashset - %s"), pCurFile->GetFileName());
334-
}
335-
else
336-
{
337-
if (!pCurFile->GetFileIdentifier().SetAICHHashSet(tempHashSet))
338-
{
339-
DebugLogError(_T("Failed to create AICH Part Hashset out of full AICH Recovery Hashset - %s"), pCurFile->GetFileName());
340-
ASSERT( false );
341-
}
342-
ASSERT(pCurFile->GetFileIdentifier().HasExpectedAICHHashCount());
343344
}
345+
ASSERT(pCurFile->GetFileIdentifier().HasExpectedAICHHashCount());
344346
}
345-
break;
346347
}
347348
}
348349
//zz_fly :: known2 split :: end

SharedFileList.cpp

+12-9
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,27 @@ class CPublishKeyword
9292

9393
BOOL AddRef(CKnownFile* pFile)
9494
{
95-
if (m_aFiles.Find(pFile) != -1)
95+
CKnownFile* pTmp;
96+
if (m_mapFiles.Lookup(pFile, pTmp))
9697
{
9798
ASSERT(0);
9899
return FALSE;
99100
}
101+
m_mapFiles.SetAt(pFile, pFile);
100102
return m_aFiles.Add(pFile);
101103
}
102104

103105
int RemoveRef(CKnownFile* pFile)
104106
{
105107
m_aFiles.Remove(pFile);
108+
m_mapFiles.RemoveKey(pFile);
106109
return m_aFiles.GetSize();
107110
}
108111

109112
void RemoveAllReferences()
110113
{
111114
m_aFiles.RemoveAll();
115+
m_mapFiles.RemoveAll();
112116
}
113117

114118
void RotateReferences(int iRotateSize)
@@ -132,6 +136,7 @@ class CPublishKeyword
132136
UINT m_tNextPublishTime;
133137
UINT m_uPublishedCount;
134138
CSimpleKnownFileArray m_aFiles;
139+
CTypedPtrMap<CMapPtrToPtr, CKnownFile*, CKnownFile*> m_mapFiles;
135140
};
136141

137142

@@ -552,6 +557,7 @@ void CSharedFileList::FindSharedFiles()
552557
m_UnsharedFiles_map.SetAt(CSKey(cur_file->GetFileHash()), true);
553558
listlock.Lock();
554559
m_Files_map.RemoveKey(key);
560+
m_IsFilePtrInList_map.RemoveKey(cur_file);
555561
//reset optimize on remove element from m_Files_map
556562
m_currPositionIndex = 0;
557563
m_currPositon = NULL;
@@ -810,6 +816,7 @@ bool CSharedFileList::AddFile(CKnownFile* pFile)
810816
CSingleLock listlock(&m_mutWriteList);
811817
listlock.Lock();
812818
m_Files_map.SetAt(key, pFile);
819+
m_IsFilePtrInList_map.SetAt(pFile, pFile);
813820
m_dwFile_map_updated = GetTickCount(); // requpfile optimization [SiRoB] - Stulle
814821
listlock.Unlock();
815822
theApp.uploadqueue->SetSuperiorInQueueDirty(); // Keep Sup clients in up if there is no other sup client in queue [Stulle] - Stulle
@@ -900,6 +907,7 @@ bool CSharedFileList::RemoveFile(CKnownFile* pFile, bool bDeleted)
900907
CSingleLock listlock(&m_mutWriteList);
901908
listlock.Lock();
902909
bool bResult = (m_Files_map.RemoveKey(CCKey(pFile->GetFileHash())) != FALSE);
910+
m_IsFilePtrInList_map.RemoveKey(pFile);
903911
//reset optimize on remove element from m_Files_map
904912
m_currPositionIndex = 0;
905913
m_currPositon = NULL;
@@ -1398,14 +1406,9 @@ bool CSharedFileList::IsFilePtrInList(const CKnownFile* file) const
13981406
{
13991407
if (file)
14001408
{
1401-
POSITION pos = m_Files_map.GetStartPosition();
1402-
while (pos)
1403-
{
1404-
CCKey key;
1405-
CKnownFile* cur_file;
1406-
m_Files_map.GetNextAssoc(pos, key, cur_file);
1407-
if (file == cur_file)
1408-
return true;
1409+
CKnownFile* tmpFile;
1410+
if (m_IsFilePtrInList_map.Lookup((CKnownFile*)file, tmpFile)) {
1411+
return true;
14091412
}
14101413
}
14111414
return false;

SharedFileList.h

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class CSharedFileList
120120

121121
private:
122122
CMap<CCKey,const CCKey&,CKnownFile*,CKnownFile*> m_Files_map;
123+
CTypedPtrMap<CMapPtrToPtr, CKnownFile*, CKnownFile*> m_IsFilePtrInList_map;
123124
CMap<CSKey,const CSKey&, bool, bool> m_UnsharedFiles_map;
124125
CMapStringToString m_mapPseudoDirNames;
125126
CPublishKeywordList* m_keywords;

0 commit comments

Comments
 (0)