Make wxTextFile::Write() much faster

Write output text in reasonably-sized chunks instead of line by line.
This is significantly faster because of lack of any caching in wxTempFile.
This commit is contained in:
Václav Slavík 2017-05-22 16:49:02 +02:00 committed by Václav Slavík
parent b6f2973c41
commit 2f7adacb9d

View File

@ -274,14 +274,28 @@ bool wxTextFile::OnWrite(wxTextFileType typeNew, const wxMBConv& conv)
return false; return false;
} }
// Writing to wxTempFile in reasonably-sized chunks is much faster than
// doing it line by line.
const size_t chunk_size = 16384;
wxString chunk;
chunk.reserve(chunk_size);
size_t nCount = GetLineCount(); size_t nCount = GetLineCount();
for ( size_t n = 0; n < nCount; n++ ) { for ( size_t n = 0; n < nCount; n++ )
fileTmp.Write(GetLine(n) + {
GetEOL(typeNew == wxTextFileType_None ? GetLineType(n) chunk += GetLine(n) +
: typeNew), GetEOL(typeNew == wxTextFileType_None ? GetLineType(n)
conv); : typeNew);
if ( chunk.size() >= chunk_size )
{
fileTmp.Write(chunk, conv);
chunk.clear();
}
} }
if ( !chunk.empty() )
fileTmp.Write(chunk, conv);
// replace the old file with this one // replace the old file with this one
return fileTmp.Commit(); return fileTmp.Commit();
} }