OSDN Git Service

* Reduce memory usage while comparing folders(3)
authorsdottaka <none@none>
Sat, 4 Jan 2014 07:50:01 +0000 (16:50 +0900)
committersdottaka <none@none>
Sat, 4 Jan 2014 07:50:01 +0000 (16:50 +0900)
 (Use boost::flyweight instead of boost::shared_ptr)

--HG--
branch : stable

Src/DiffThread.cpp
Src/DirItem.cpp
Src/DirItem.h
Src/DirScan.cpp
Src/DirScan.h
Src/DirTravel.cpp

index f1ca729..fa1c41d 100644 (file)
@@ -160,9 +160,7 @@ static void DiffThreadCollect(void *pParam)
 
        paths = myStruct->context->GetNormalizedPaths();
 
-       shared_ptr<String> subdir[3]; // blank to start at roots specified in diff context
-       subdir[0].reset(new String(_T("")));
-       subdir[1] = subdir[2] = subdir[0];
+       String subdir[3] = {_T(""), _T(""), _T("")}; // blank to start at roots specified in diff context
 
        // Build results list (except delaying file comparisons until below)
        DirScan_GetItems(paths, subdir, myStruct,
index ad1c669..60cc331 100644 (file)
@@ -74,7 +74,7 @@ void DirItem::SetFile(const String &fullPath)
  */
 String DirItem::GetFile() const
 {
-       return paths_ConcatPath(*path, *filename);
+       return paths_ConcatPath(path.get(), filename.get());
 }
 
 /**
index cb833da..c45486f 100644 (file)
@@ -32,6 +32,7 @@
 #include <Poco/File.h>
 #include <Poco/Timestamp.h>
 #include <boost/shared_ptr.hpp>
+#include <boost/flyweight.hpp>
 
 /**
  * @brief Class for fileflags.
@@ -58,18 +59,18 @@ struct DirItem
        Poco::Timestamp ctime; /**< time of creation */
        Poco::Timestamp mtime; /**< time of last modify */
        Poco::File::FileSize size; /**< file size in bytes, -1 means file does not exist*/
-       boost::shared_ptr<const String> filename; /**< filename for this item */
-       boost::shared_ptr<const String> path; /**< full path (excluding filename) for the item */
+       boost::flyweight<String> filename; /**< filename for this item */
+       boost::flyweight<String> path; /**< full path (excluding filename) for the item */
        FileVersion version; /**< string of fixed file version, eg, 1.2.3.4 */
        FileFlags flags; /**< file attributes */
 
        DirItem() : ctime(0), mtime(0), size(-1) { }
        void SetFile(const String &fullPath);
        String GetFile() const;
-       const String& GetPath() const { return *path; };
-       void SetPath(const String& newpath) { path.reset(new String(newpath)); }
-       const String& GetFileName() const { return *filename; };
-       void SetFileName(const String& newfilename) { filename.reset(new String(newfilename)); }
+       const String& GetPath() const { return path.get(); };
+       void SetPath(const String& newpath) { path = newpath; }
+       const String& GetFileName() const { return filename.get(); };
+       void SetFileName(const String& newfilename) { filename = newfilename; }
        bool Update(const String &sFilePath);
        void ClearPartial();
 };
index 4ef2d53..09778ae 100644 (file)
@@ -51,9 +51,9 @@ using boost::shared_ptr;
 void CompareDiffItem(DIFFITEM &di, CDiffContext * pCtxt);
 static void StoreDiffData(DIFFITEM &di, CDiffContext * pCtxt,
                const FolderCmp * pCmpData);
-static DIFFITEM *AddToList(const shared_ptr<String> sLeftDir, const shared_ptr<String> sRightDir, const DirItem * lent, const DirItem * rent,
+static DIFFITEM *AddToList(const String& sLeftDir, const String& sRightDir, const DirItem * lent, const DirItem * rent,
        unsigned code, DiffFuncStruct *myStruct, DIFFITEM *parent);
-static DIFFITEM *AddToList(const shared_ptr<String> sLeftDir, const shared_ptr<String> sMiddleDir, const shared_ptr<String> sRightDir, const DirItem * lent, const DirItem * ment, const DirItem * rent,
+static DIFFITEM *AddToList(const String& sLeftDir, const String& sMiddleDir, const String& sRightDir, const DirItem * lent, const DirItem * ment, const DirItem * rent,
        unsigned code, DiffFuncStruct *myStruct, DIFFITEM *parent);
 static void UpdateDiffItem(DIFFITEM & di, bool & bExists, CDiffContext *pCtxt);
 static int CompareItems(NotificationQueue& queue, DiffFuncStruct *myStruct, UIntPtr parentdiffpos);
@@ -133,7 +133,7 @@ typedef boost::shared_ptr<DiffWorker> DiffWorkerPtr;
  * @param [in] bUniques If true, walk into unique folders.
  * @return 1 normally, -1 if compare was aborted
  */
-int DirScan_GetItems(const PathContext &paths, const shared_ptr<String> subdir[],
+int DirScan_GetItems(const PathContext &paths, const String subdir[],
                DiffFuncStruct *myStruct,
                bool casesensitive, int depth, DIFFITEM *parent,
                bool bUniques)
@@ -148,12 +148,12 @@ int DirScan_GetItems(const PathContext &paths, const shared_ptr<String> subdir[]
        for (nIndex = 0; nIndex < nDirs; nIndex++)
                sDir[nIndex] = paths.GetPath(nIndex);
 
-       if (!subdir[0]->empty())
+       if (!subdir[0].empty())
        {
                for (nIndex = 0; nIndex < paths.GetSize(); nIndex++)
                {
-                       sDir[nIndex] = paths_ConcatPath(sDir[nIndex], *subdir[nIndex]);
-                       subprefix[nIndex] = *subdir[nIndex] + backslash;
+                       sDir[nIndex] = paths_ConcatPath(sDir[nIndex], subdir[nIndex]);
+                       subprefix[nIndex] = subdir[nIndex] + backslash;
                }
        }
 
@@ -351,12 +351,7 @@ OutputDebugString(buf);
                                                nDiffCode & DIFFCODE::SECOND ? &dirs[1][j] : NULL,
                                                nDiffCode, myStruct, parent);
                                        // Scan recursively all subdirectories too, we are not adding folders
-                                       shared_ptr<String> newsubdir[3];
-                                       newsubdir[0].reset(new String(leftnewsub));
-                                       if (leftnewsub == rightnewsub)
-                                               newsubdir[1] = newsubdir[0];
-                                       else
-                                               newsubdir[1].reset(new String(rightnewsub));
+                                       String newsubdir[3] = {leftnewsub, rightnewsub};
                                        int result = DirScan_GetItems(paths, newsubdir, myStruct, casesensitive,
                                                        depth - 1, me, bUniques);
                                        if (result == -1)
@@ -402,16 +397,7 @@ OutputDebugString(buf);
                                                nDiffCode & DIFFCODE::THIRD  ? &dirs[2][k] : NULL,
                                                nDiffCode, myStruct, parent);
                                        // Scan recursively all subdirectories too, we are not adding folders
-                                       shared_ptr<String> newsubdir[3];
-                                       newsubdir[0].reset(new String(leftnewsub));
-                                       if (leftnewsub == middlenewsub)
-                                               newsubdir[1] = newsubdir[0];
-                                       else
-                                               newsubdir[1].reset(new String(middlenewsub));
-                                       if (leftnewsub == rightnewsub)
-                                               newsubdir[2] = newsubdir[0];
-                                       else
-                                               newsubdir[2].reset(new String(rightnewsub));
+                                       String newsubdir[3] = {leftnewsub, middlenewsub, rightnewsub};
                                        int result = DirScan_GetItems(paths, newsubdir, myStruct, casesensitive,
                                                        depth - 1, me, bUniques);
                                        if (result == -1)
@@ -882,7 +868,7 @@ static void StoreDiffData(DIFFITEM &di, CDiffContext * pCtxt,
  * @param [in] pCtxt Compare context.
  * @param [in] parent Parent of item to be added
  */
-static DIFFITEM *AddToList(const shared_ptr<String> sLeftDir, const shared_ptr<String> sRightDir,
+static DIFFITEM *AddToList(const String& sLeftDir, const String& sRightDir,
        const DirItem * lent, const DirItem * rent,
        unsigned code, DiffFuncStruct *myStruct, DIFFITEM *parent)
 {
@@ -892,7 +878,7 @@ static DIFFITEM *AddToList(const shared_ptr<String> sLeftDir, const shared_ptr<S
 /**
  * @brief Add one compare item to list.
  */
-static DIFFITEM *AddToList(const shared_ptr<String> sLeftDir, const shared_ptr<String> sMiddleDir, const shared_ptr<String> sRightDir,
+static DIFFITEM *AddToList(const String& sLeftDir, const String& sMiddleDir, const String& sRightDir,
        const DirItem * lent, const DirItem * ment, const DirItem * rent,
        unsigned code, DiffFuncStruct *myStruct, DIFFITEM *parent)
 {
@@ -925,8 +911,7 @@ static DIFFITEM *AddToList(const shared_ptr<String> sLeftDir, const shared_ptr<S
 
        if (ment)
        {
-               di->diffFileInfo[1].filename = 
-                       (*di->diffFileInfo[0].filename == *ment->filename) ? di->diffFileInfo[0].filename : ment->filename;
+               di->diffFileInfo[1].filename = ment->filename;
                di->diffFileInfo[1].mtime = ment->mtime;
                di->diffFileInfo[1].ctime = ment->ctime;
                di->diffFileInfo[1].size = ment->size;
@@ -943,8 +928,7 @@ static DIFFITEM *AddToList(const shared_ptr<String> sLeftDir, const shared_ptr<S
 
        if (rent)
        {
-               di->diffFileInfo[2].filename = 
-                       (*di->diffFileInfo[0].filename == *rent->filename) ? di->diffFileInfo[0].filename : rent->filename;
+               di->diffFileInfo[2].filename = rent->filename;
                di->diffFileInfo[2].mtime = rent->mtime;
                di->diffFileInfo[2].ctime = rent->ctime;
                di->diffFileInfo[2].size = rent->size;
index cbea8e0..f7b3825 100644 (file)
@@ -12,7 +12,6 @@
 #include "UnicodeString.h"
 #define POCO_NO_UNWINDOWS 1
 #include <Poco/Types.h>
-#include <boost/shared_ptr.hpp>
 
 class CDiffContext;
 class DiffItemList;
@@ -21,7 +20,7 @@ class IAbortable;
 struct DIFFITEM;
 struct DiffFuncStruct;
 
-int DirScan_GetItems(const PathContext &paths, const boost::shared_ptr<String> subdir[], DiffFuncStruct *myStruct,
+int DirScan_GetItems(const PathContext &paths, const String subdir[], DiffFuncStruct *myStruct,
                bool casesensitive, int depth, DIFFITEM *parent, bool bUniques);
 
 int DirScan_CompareItems(DiffFuncStruct *, Poco::UIntPtr parentdiffpos);
index e415f37..2db1f0e 100644 (file)
@@ -50,7 +50,7 @@ void LoadAndSortFiles(const String& sDir, DirItemArray * dirs, DirItemArray * fi
  */
 static void LoadFiles(const String& sDir, DirItemArray * dirs, DirItemArray * files)
 {
-       shared_ptr<const String> pdir(new String(sDir));
+       boost::flyweight<String> dir(sDir);
 #if 0
        DirectoryIterator it(ucr::toUTF8(sDir));
        DirectoryIterator end;
@@ -69,8 +69,8 @@ static void LoadFiles(const String& sDir, DirItemArray * dirs, DirItemArray * fi
                if (ent.mtime < 0)
                        ent.mtime = 0;
                ent.size = it->getSize();
-               ent.path = pdir;
-               ent.filename.reset(new String(ucr::toTString(it.name()));
+               ent.path = dir;
+               ent.filename = ucr::toTString(it.name());
 #ifdef _WIN32
                ent.flags.attributes = GetFileAttributes(ucr::toTString(it.name()).c_str());;
 #else
@@ -117,8 +117,8 @@ static void LoadFiles(const String& sDir, DirItemArray * dirs, DirItemArray * fi
                                ent.size = ((Int64)ff.nFileSizeHigh << 32) + ff.nFileSizeLow;
                        }
 
-                       ent.path = pdir;
-                       ent.filename.reset(new String(ff.cFileName));
+                       ent.path = dir;
+                       ent.filename = ff.cFileName;
                        ent.flags.attributes = ff.dwFileAttributes;
                        
                        (bIsDirectory ? dirs : files)->push_back(ent);