Changeset 13601

Show
Ignore:
Timestamp:
01/26/08 11:25:51 (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
  • branches/1.5/gdal/port/cpl_vsil_unix_stdio_64.cpp

    r11304 r13601  
    287287    if ( (hDir = opendir(pszPath)) != NULL ) 
    288288    { 
     289        /* In case of really big number of files in the directory, CSLAddString */ 
     290        /* can be slow (see #2158). We then directly build the list. */ 
     291        int nItems=0; 
     292        int nAllocatedItems=0; 
    289293        while( (psDirEntry = readdir(hDir)) != NULL ) 
    290294        { 
    291             papszDir = CSLAddString(papszDir, psDirEntry->d_name); 
     295            if (nItems == 0) 
     296            { 
     297                papszDir = (char**) CPLCalloc(2,sizeof(char*)); 
     298                nAllocatedItems = 1; 
     299            } 
     300            else if (nItems >= nAllocatedItems) 
     301            { 
     302                nAllocatedItems = nAllocatedItems * 2; 
     303                papszDir = (char**)CPLRealloc(papszDir,  
     304                                              (nAllocatedItems+2)*sizeof(char*)); 
     305            } 
     306 
     307            papszDir[nItems] = CPLStrdup(psDirEntry->d_name); 
     308            papszDir[nItems+1] = NULL; 
     309 
     310            nItems++; 
    292311        } 
    293312