Have the buffer grow with a 2x factor to avoid O(n^2) work when reading big files.

Signed-off-by: Drew Lewis <cannada@google.com>
This commit is contained in:
Drew Lewis 2026-03-02 22:22:27 +00:00
parent f3a17d343a
commit ee40e40d09
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);