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,13 +274,27 @@ bool wxTextFile::OnWrite(wxTextFileType typeNew, const wxMBConv& conv)
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();
for ( size_t n = 0; n < nCount; n++ ) {
fileTmp.Write(GetLine(n) +
for ( size_t n = 0; n < nCount; n++ )
{
chunk += GetLine(n) +
GetEOL(typeNew == wxTextFileType_None ? GetLineType(n)
: typeNew),
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
return fileTmp.Commit();