Merge pull request #488 from calewis/grow_faster

Avoid O(n^2) work on gzipped liberty data
This commit is contained in:
alanminko 2026-03-07 06:49:33 -08:00 committed by GitHub
commit d10a0d41a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 3 deletions

View File

@ -559,14 +559,17 @@ static char * Io_LibLoadFileGz( char * pFileName, long * pnFileSize )
const int READ_BLOCK_SIZE = 100000;
gzFile pFile;
char * pContents;
long amtRead, readBlock, nFileSize = READ_BLOCK_SIZE;
long amtRead, readBlock, nFileSize = READ_BLOCK_SIZE, nCapacity = READ_BLOCK_SIZE;
pFile = gzopen( pFileName, "rb" ); // if pFileName doesn't end in ".gz" then this acts as a passthrough to fopen
pContents = ABC_ALLOC( char, nFileSize );
pContents = ABC_ALLOC( char, nCapacity );
readBlock = 0;
while ((amtRead = gzread(pFile, pContents + readBlock * READ_BLOCK_SIZE, READ_BLOCK_SIZE)) == READ_BLOCK_SIZE) {
//Abc_Print( 1,"%d: read %d bytes\n", readBlock, amtRead);
nFileSize += READ_BLOCK_SIZE;
pContents = ABC_REALLOC(char, pContents, nFileSize);
if ( nFileSize > nCapacity ) {
nCapacity *= 2;
pContents = ABC_REALLOC(char, pContents, nCapacity);
}
++readBlock;
}
//Abc_Print( 1,"%d: read %d bytes\n", readBlock, amtRead);