From 2f7adacb9dc9b0c294fd063a9440849f7bd695ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Va=CC=81clav=20Slavi=CC=81k?= Date: Mon, 22 May 2017 16:49:02 +0200 Subject: [PATCH] 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. --- src/common/textfile.cpp | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/common/textfile.cpp b/src/common/textfile.cpp index 0b15083a7f..279013c28c 100644 --- a/src/common/textfile.cpp +++ b/src/common/textfile.cpp @@ -274,14 +274,28 @@ 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) + - GetEOL(typeNew == wxTextFileType_None ? GetLineType(n) - : typeNew), - conv); + for ( size_t n = 0; n < nCount; n++ ) + { + chunk += GetLine(n) + + GetEOL(typeNew == wxTextFileType_None ? GetLineType(n) + : 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(); }