Skip to content

Commit d10a0d4

Browse files
authored
Merge pull request #488 from calewis/grow_faster
Avoid O(n^2) work on gzipped liberty data
2 parents f3a17d3 + ee40e40 commit d10a0d4

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/map/scl/sclLiberty.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -559,14 +559,17 @@ static char * Io_LibLoadFileGz( char * pFileName, long * pnFileSize )
559559
const int READ_BLOCK_SIZE = 100000;
560560
gzFile pFile;
561561
char * pContents;
562-
long amtRead, readBlock, nFileSize = READ_BLOCK_SIZE;
562+
long amtRead, readBlock, nFileSize = READ_BLOCK_SIZE, nCapacity = READ_BLOCK_SIZE;
563563
pFile = gzopen( pFileName, "rb" ); // if pFileName doesn't end in ".gz" then this acts as a passthrough to fopen
564-
pContents = ABC_ALLOC( char, nFileSize );
564+
pContents = ABC_ALLOC( char, nCapacity );
565565
readBlock = 0;
566566
while ((amtRead = gzread(pFile, pContents + readBlock * READ_BLOCK_SIZE, READ_BLOCK_SIZE)) == READ_BLOCK_SIZE) {
567567
//Abc_Print( 1,"%d: read %d bytes\n", readBlock, amtRead);
568568
nFileSize += READ_BLOCK_SIZE;
569-
pContents = ABC_REALLOC(char, pContents, nFileSize);
569+
if ( nFileSize > nCapacity ) {
570+
nCapacity *= 2;
571+
pContents = ABC_REALLOC(char, pContents, nCapacity);
572+
}
570573
++readBlock;
571574
}
572575
//Abc_Print( 1,"%d: read %d bytes\n", readBlock, amtRead);

0 commit comments

Comments
 (0)