[PATCH v3 2/3] comdlg32: Avoid generating filenames twice in filedlg.

Lauri Kenttä lauri.kentta at gmail.com
Wed Jul 6 05:27:43 CDT 2016


(Was: comdlg32: Optimize FILEDLG95_FILENAME_FillFromSelection.)

Related to bug 26803.

Previously the function would loop the files twice, first to count,
then to create a string from the names. This patch removes the
first pass and instead changes the buffer size as we go.

This gives 20% speed-up for selecting 253 files with shift-click.

Try 1:
- Allocate a big enough buffer in the beginning.

Try 2:
- Allocate first a small buffer, double as needed.
- Unify formatting within the function. (The file is inconsistent,
  and even this function was inconsistent, so I couldn't exactly copy
  the "original" style.)

(Try 3: resend)

Signed-off-by: Lauri Kenttä <lauri.kentta at gmail.com>
---
 dlls/comdlg32/filedlg.c | 122 +++++++++++++++++++++---------------------------
 1 file changed, 52 insertions(+), 70 deletions(-)

diff --git a/dlls/comdlg32/filedlg.c b/dlls/comdlg32/filedlg.c
index 276dfb0..32752af 100644
--- a/dlls/comdlg32/filedlg.c
+++ b/dlls/comdlg32/filedlg.c
@@ -3626,86 +3626,68 @@ static void FILEDLG95_LOOKIN_Clean(HWND hwnd)
  */
 void FILEDLG95_FILENAME_FillFromSelection (HWND hwnd)
 {
-    FileOpenDlgInfos *fodInfos;
-    LPITEMIDLIST      pidl;
-    UINT              nFiles = 0, nFileToOpen, nFileSelected, nLength = 0;
-    WCHAR             lpstrTemp[MAX_PATH];
-    LPWSTR            lpstrAllFile, lpstrCurrFile;
+  FileOpenDlgInfos *fodInfos;
+  LPITEMIDLIST pidl;
+  UINT nFiles = 0, nFileToOpen, nFileSelected, nAllFilesLength = 0, nThisFileLength, nAllFilesMaxLength;
+  LPWSTR lpstrAllFiles, lpstrTmp;
 
-    TRACE("\n");
-    fodInfos = GetPropA(hwnd,FileOpenDlgInfosStr);
+  TRACE("\n");
+  fodInfos = GetPropA(hwnd, FileOpenDlgInfosStr);
 
-    /* Count how many files we have */
-    nFileSelected = GetNumSelected( fodInfos->Shell.FOIDataObject );
+  /* Count how many files we have */
+  nFileSelected = GetNumSelected(fodInfos->Shell.FOIDataObject);
 
-    /* calculate the string length, count files */
-    if (nFileSelected >= 1)
+  /* Allocate a buffer */
+  nAllFilesMaxLength = MAX_PATH + 3;
+  lpstrAllFiles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nAllFilesMaxLength * sizeof(WCHAR));
+  if (!lpstrAllFiles)
+    goto ret;
+
+  /* Loop through the selection, handle only files (not folders) */
+  for (nFileToOpen = 0; nFileToOpen < nFileSelected; nFileToOpen++)
+  {
+    pidl = GetPidlFromDataObject(fodInfos->Shell.FOIDataObject, nFileToOpen + 1);
+    if (pidl)
     {
-      nLength += 3;	/* first and last quotes, trailing \0 */
-      for ( nFileToOpen = 0; nFileToOpen < nFileSelected; nFileToOpen++ )
+      if (!IsPidlFolder(fodInfos->Shell.FOIShellFolder, pidl))
       {
-        pidl = GetPidlFromDataObject( fodInfos->Shell.FOIDataObject, nFileToOpen+1 );
-
-        if (pidl)
-	{
-          /* get the total length of the selected file names */
-          lpstrTemp[0] = '\0';
-          GetName( fodInfos->Shell.FOIShellFolder, pidl, SHGDN_INFOLDER|SHGDN_FORPARSING, lpstrTemp );
-
-          if ( ! IsPidlFolder(fodInfos->Shell.FOIShellFolder, pidl) ) /* Ignore folders */
-	  {
-            nLength += lstrlenW( lpstrTemp ) + 3;
-            nFiles++;
-	  }
-          COMDLG32_SHFree( pidl );
-	}
+        if (nAllFilesLength + MAX_PATH + 3 > nAllFilesMaxLength)
+        {
+          nAllFilesMaxLength *= 2;
+          lpstrTmp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpstrAllFiles, nAllFilesMaxLength * sizeof(WCHAR));
+          if (!lpstrTmp)
+            goto ret;
+          lpstrAllFiles = lpstrTmp;
+        }
+        nFiles += 1;
+        lpstrAllFiles[nAllFilesLength++] = '"';
+        GetName(fodInfos->Shell.FOIShellFolder, pidl, SHGDN_INFOLDER | SHGDN_FORPARSING, lpstrAllFiles + nAllFilesLength);
+        nThisFileLength = lstrlenW(lpstrAllFiles + nAllFilesLength);
+        nAllFilesLength += nThisFileLength;
+        lpstrAllFiles[nAllFilesLength++] = '"';
+        lpstrAllFiles[nAllFilesLength++] = ' ';
       }
+      COMDLG32_SHFree(pidl);
     }
+  }
 
-    /* allocate the buffer */
-    if (nFiles <= 1) nLength = MAX_PATH;
-    lpstrAllFile = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nLength * sizeof(WCHAR));
-
-    /* Generate the string for the edit control */
-    if(nFiles >= 1)
+  if (nFiles != 0)
+  {
+    /* If there's only one file, use the name as-is without quotes */
+    lpstrTmp = lpstrAllFiles;
+    if (nFiles == 1)
     {
-      lpstrCurrFile = lpstrAllFile;
-      for ( nFileToOpen = 0; nFileToOpen < nFileSelected; nFileToOpen++ )
-      {
-        pidl = GetPidlFromDataObject( fodInfos->Shell.FOIDataObject, nFileToOpen+1 );
-
-        if (pidl)
-	{
-	  /* get the file name */
-          lpstrTemp[0] = '\0';
-          GetName( fodInfos->Shell.FOIShellFolder, pidl, SHGDN_INFOLDER|SHGDN_FORPARSING, lpstrTemp );
-
-          if (! IsPidlFolder(fodInfos->Shell.FOIShellFolder, pidl)) /* Ignore folders */
-	  {
-            if ( nFiles > 1)
-	    {
-              *lpstrCurrFile++ =  '\"';
-              lstrcpyW( lpstrCurrFile, lpstrTemp );
-              lpstrCurrFile += lstrlenW( lpstrTemp );
-              *lpstrCurrFile++ = '\"';
-              *lpstrCurrFile++ = ' ';
-              *lpstrCurrFile = 0;
-	    }
-	    else
-	    {
-              lstrcpyW( lpstrAllFile, lpstrTemp );
-	    }
-          }
-          COMDLG32_SHFree( pidl );
-	}
-      }
-      SetWindowTextW( fodInfos->DlgInfos.hwndFileName, lpstrAllFile );
-       
-      /* Select the file name like Windows does */
-      if (filename_is_edit( fodInfos ))
-          SendMessageW(fodInfos->DlgInfos.hwndFileName, EM_SETSEL, 0, -1);
+      lpstrTmp += 1;
+      lpstrTmp[nThisFileLength] = 0;
     }
-    HeapFree(GetProcessHeap(),0, lpstrAllFile );
+    SetWindowTextW(fodInfos->DlgInfos.hwndFileName, lpstrTmp);
+    /* Select the file name like Windows does */
+    if (filename_is_edit(fodInfos))
+      SendMessageW(fodInfos->DlgInfos.hwndFileName, EM_SETSEL, 0, -1);
+  }
+
+ret:
+  HeapFree(GetProcessHeap(), 0, lpstrAllFiles);
 }
 
 
-- 
2.9.0




More information about the wine-patches mailing list