Changeset 13600

Show
Ignore:
Timestamp:
01/26/08 11:23:05 (4 months ago)
Author:
rouault
Message:

Fix #2158 (gdal becomes painfully slow when used in directories with large number of files)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/gdal/port/cpl_string.cpp

    r13540 r13600  
    6767 * StringList. 
    6868 * If the input StringList is NULL, then a new StringList is created. 
     69 * Note that CSLAddString performance when building a list is in O(n^2) 
     70 * which can cause noticable slow down when n > 10000. 
    6971 **********************************************************************/ 
    7072char **CSLAddString(char **papszStrList, const char *pszNewString) 
  • trunk/gdal/port/cpl_vsil_unix_stdio_64.cpp

    r13594 r13600  
    356356    if ( (hDir = opendir(pszPath)) != NULL ) 
    357357    { 
     358        /* In case of really big number of files in the directory, CSLAddString */ 
     359        /* can be slow (see #2158). We then directly build the list. */ 
     360        int nItems=0; 
     361        int nAllocatedItems=0; 
    358362        while( (psDirEntry = readdir(hDir)) != NULL ) 
    359363        { 
    360             papszDir = CSLAddString(papszDir, psDirEntry->d_name); 
     364            if (nItems == 0) 
     365            { 
     366                papszDir = (char**) CPLCalloc(2,sizeof(char*)); 
     367                nAllocatedItems = 1; 
     368            } 
     369            else if (nItems >= nAllocatedItems) 
     370            { 
     371                nAllocatedItems = nAllocatedItems * 2; 
     372                papszDir = (char**)CPLRealloc(papszDir,  
     373                                              (nAllocatedItems+2)*sizeof(char*)); 
     374            } 
     375 
     376            papszDir[nItems] = CPLStrdup(psDirEntry->d_name); 
     377            papszDir[nItems+1] = NULL; 
     378 
     379            nItems++; 
    361380        } 
    362381