From 29c9f9ae8d9b5ea6f7e72b628e4030636d7c10f6 Mon Sep 17 00:00:00 2001 From: twanvl Date: Fri, 21 Sep 2007 11:55:54 +0000 Subject: [PATCH] Some minor performance tweaks git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@726 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/gfx/color.cpp | 15 ++++++++++----- src/util/io/reader.cpp | 40 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/gfx/color.cpp b/src/gfx/color.cpp index 2513414c..a25b485f 100644 --- a/src/gfx/color.cpp +++ b/src/gfx/color.cpp @@ -62,10 +62,15 @@ void fill_image(Image& image, const Color& color) { Byte* pos = image.GetData(); Byte* end = pos + image.GetWidth() * image.GetHeight() * 3; Byte r = color.Red(), g = color.Green(), b = color.Blue(); - // fill the image - while (pos != end) { - *pos++ = r; - *pos++ = g; - *pos++ = b; + if (r == g && r == b) { + // optimization: use memset + memset(pos, r, end-pos); + } else { + // fill the image + while (pos != end) { + *pos++ = r; + *pos++ = g; + *pos++ = b; + } } } diff --git a/src/util/io/reader.cpp b/src/util/io/reader.cpp index 8b4d9b76..89edfc43 100644 --- a/src/util/io/reader.cpp +++ b/src/util/io/reader.cpp @@ -10,6 +10,7 @@ #include #include #include +#undef small // ----------------------------------------------------------------------------- : Reader @@ -112,12 +113,43 @@ void Reader::moveNext() { } } +/// Faster vector, uses large local storage +/** Usually a line from a file doesn't use very many characters. + * In that case, allocating a vector is a waste of resources. + * 2007-09-21: Profiling => Using this class roughly halves the runtime of read_utf8_line, + * making startup slightly faster. + */ +template class LocalVector { + public: + LocalVector() : size(0), alloced(SMALL_SIZE), buffer(small) {} + ~LocalVector() { if (buffer != small) free(buffer); } + void push_back(T t) { + if (size >= alloced) { + // double buffer size + if (buffer != small) { + buffer = (T*)realloc(buffer, sizeof(T) * alloced * 2); + } else { + buffer = (T*)malloc(sizeof(T) * alloced * 2); + memcpy(buffer, small, alloced * sizeof(T)); + } + alloced *= 2; + } + buffer[size++] = t; + } + const T* get() { return buffer; } + private: + static const int SMALL_SIZE = 1024; + size_t size, alloced; + T* buffer; + T small[SMALL_SIZE]; +}; + /// Read an UTF-8 encoded line from an input stream /** As opposed to wx functions, this one actually reports errors */ String read_utf8_line(wxInputStream& input, bool eat_bom = true, bool until_eof = false); String read_utf8_line(wxInputStream& input, bool eat_bom, bool until_eof) { - vector buffer; + LocalVector buffer; while (!input.Eof()) { Byte c = input.GetC(); if (input.LastRead() <= 0) break; if (!until_eof) { @@ -135,7 +167,7 @@ String read_utf8_line(wxInputStream& input, bool eat_bom, bool until_eof) { } // convert to string buffer.push_back('\0'); - size_t size = wxConvUTF8.MB2WC(nullptr, &buffer[0], 0); + size_t size = wxConvUTF8.MB2WC(nullptr, buffer.get(), 0); if (size == -1) { throw ParseError(_("Invalid UTF-8 sequence")); } else if (size == 0) { @@ -145,13 +177,13 @@ String read_utf8_line(wxInputStream& input, bool eat_bom, bool until_eof) { #ifdef UNICODE // NOTE: wx doc is wrong, parameter to GetWritableChar is numer of characters, not bytes Char* result_buf = result.GetWriteBuf(size + 1); - wxConvUTF8.MB2WC(result_buf, &buffer[0], size + 1); + wxConvUTF8.MB2WC(result_buf, buffer.get(), size + 1); result.UngetWriteBuf(size); return eat_bom ? decodeUTF8BOM(result) : result; #else // first to wchar, then back to local vector buf2; buf2.resize(size+1); - wxConvUTF8.MB2WC(&buf2[0], &buffer[0], size + 1); + wxConvUTF8.MB2WC(&buf2[0], buffer.get(), size + 1); // eat BOM? if (eat_bom && buf2[0]==0xFEFF ) { buf2.erase(buf2.begin()); // remove BOM