From 8c19a03806d0a06596036625adaacced47c44431 Mon Sep 17 00:00:00 2001 From: spog Date: Mon, 20 Feb 2006 23:14:51 +0000 Subject: [PATCH] added buffering to minimise GtkTextBuffer insert calls git-svn-id: svn://svn.icculus.org/gtkradiant/GtkRadiant/trunk@17 8a3a26a2-13c4-0310-b231-cf6edde360e5 --- libs/archivelib.h | 9 ++-- libs/debugging/debugging.h | 1 + libs/scenelib.h | 12 ++--- libs/stream/textstream.h | 83 +++++++++++++++++++++++++++++++++ libs/xml/xmlparser.h | 2 +- libs/xml/xmlwriter.h | 50 +------------------- plugins/archivezip/zlibstream.h | 2 +- radiant/console.cpp | 17 ++++--- radiant/undo.cpp | 6 +-- radiant/xmlstuff.h | 2 +- 10 files changed, 111 insertions(+), 73 deletions(-) diff --git a/libs/archivelib.h b/libs/archivelib.h index 8071ab9..710d2e8 100644 --- a/libs/archivelib.h +++ b/libs/archivelib.h @@ -36,28 +36,27 @@ template class SingleByteInputStream { typedef typename InputStreamType::byte_type byte_type; - static const int BUFFERSIZE = SIZE; InputStreamType& m_inputStream; - byte_type m_buffer[BUFFERSIZE]; + byte_type m_buffer[SIZE]; byte_type* m_cur; byte_type* m_end; public: - SingleByteInputStream(InputStreamType& inputStream) : m_inputStream(inputStream), m_cur(m_buffer + BUFFERSIZE), m_end(m_cur) + SingleByteInputStream(InputStreamType& inputStream) : m_inputStream(inputStream), m_cur(m_buffer + SIZE), m_end(m_cur) { } bool readByte(byte_type& b) { if(m_cur == m_end) { - if(m_end != m_buffer + BUFFERSIZE) + if(m_end != m_buffer + SIZE) { return false; } - m_end = m_buffer + m_inputStream.read(m_buffer, BUFFERSIZE); + m_end = m_buffer + m_inputStream.read(m_buffer, SIZE); m_cur = m_buffer; if(m_end == m_buffer) diff --git a/libs/debugging/debugging.h b/libs/debugging/debugging.h index 93dd099..2a9cbe7 100644 --- a/libs/debugging/debugging.h +++ b/libs/debugging/debugging.h @@ -128,6 +128,7 @@ if(!globalDebugMessageHandler().handleMessage()) { DEBUGGER_BREAKPOINT(); } else #else #define ASSERT_MESSAGE(condition, message) +#define ERROR_MESSAGE(message) #define ASSERT_NOTNULL(ptr) #endif diff --git a/libs/scenelib.h b/libs/scenelib.h index 71d4963..a5be241 100644 --- a/libs/scenelib.h +++ b/libs/scenelib.h @@ -122,7 +122,7 @@ template class StaticNodeType { public: - static const int SIZE = NODETYPEID_MAX; + enum unnamed0 { SIZE = NODETYPEID_MAX }; static TypeId getTypeId() { return Static< NodeType >::instance().getTypeId(); @@ -161,10 +161,10 @@ namespace scene class Node { public: - static const int eVisible = 0; - static const int eHidden = 1 << 0; - static const int eFiltered = 1 << 1; - static const int eExcluded = 1 << 2; + enum unnamed0 { eVisible = 0 }; + enum unnamed1 { eHidden = 1 << 0 }; + enum unnamed2 { eFiltered = 1 << 1 }; + enum unnamed3 { eExcluded = 1 << 2 }; class Symbiot { @@ -483,7 +483,7 @@ template class StaticInstanceType { public: - static const int SIZE = INSTANCETYPEID_MAX; + enum unnamed0 { SIZE = INSTANCETYPEID_MAX }; static TypeId getTypeId() { return Static< InstanceType >::instance().getTypeId(); diff --git a/libs/stream/textstream.h b/libs/stream/textstream.h index ddc6cf4..083026b 100644 --- a/libs/stream/textstream.h +++ b/libs/stream/textstream.h @@ -389,4 +389,87 @@ public: } }; +/// \brief A wrapper for a TextOutputStream, optimised for writing a single character at a time. +class SingleCharacterOutputStream : public TextOutputStream +{ + enum unnamed0 { m_bufsize = 1024 }; + TextOutputStream& m_ostream; + char m_buffer[m_bufsize]; + char* m_pos; + const char* m_end; + + const char* end() const + { + return m_end; + } + void reset() + { + m_pos = m_buffer; + } + void flush() + { + m_ostream.write(m_buffer, m_pos - m_buffer); + reset(); + } +public: + SingleCharacterOutputStream(TextOutputStream& ostream) : m_ostream(ostream), m_pos(m_buffer), m_end(m_buffer+m_bufsize) + { + } + ~SingleCharacterOutputStream() + { + flush(); + } + void write(const char c) + { + if(m_pos == end()) + { + flush(); + } + *m_pos++ = c; + } + std::size_t write(const char* buffer, std::size_t length) + { + const char*const end = buffer + length; + for(const char* p = buffer; p != end; ++p) + { + write(*p); + } + return length; + } +}; + +/// \brief A wrapper for a TextOutputStream, optimised for writing a few characters at a time. +template +class BufferedTextOutputStream : public TextOutputStream +{ + TextOutputStreamType outputStream; + char m_buffer[SIZE]; + char* m_cur; + +public: + BufferedTextOutputStream(TextOutputStreamType& outputStream) : outputStream(outputStream), m_cur(m_buffer) + { + } + ~BufferedTextOutputStream() + { + outputStream.write(m_buffer, m_cur - m_buffer); + } + std::size_t write(const char* buffer, std::size_t length) + { + std::size_t remaining = length; + for(;;) + { + std::size_t n = std::min(remaining, std::size_t((m_buffer + SIZE) - m_cur)); + m_cur = std::copy(buffer, buffer + n, m_cur); + remaining -= n; + if(remaining == 0) + { + return 0; + } + outputStream.write(m_buffer, SIZE); + m_cur = m_buffer; + } + } +}; + #endif diff --git a/libs/xml/xmlparser.h b/libs/xml/xmlparser.h index 3140888..bea4557 100644 --- a/libs/xml/xmlparser.h +++ b/libs/xml/xmlparser.h @@ -201,7 +201,7 @@ public: class XMLStreamParser : public XMLExporter { - static const int BUFSIZE = 1024; + enum unnamed0 { BUFSIZE = 1024 }; public: XMLStreamParser(TextInputStream& istream) : m_istream(istream) diff --git a/libs/xml/xmlwriter.h b/libs/xml/xmlwriter.h index 8cf40bd..d387f32 100644 --- a/libs/xml/xmlwriter.h +++ b/libs/xml/xmlwriter.h @@ -26,57 +26,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #include "xml/ixml.h" -class BufferedTextOutputStream : public TextOutputStream -{ - static const int m_bufsize = 1024; - TextOutputStream& m_ostream; - char m_buffer[m_bufsize]; - char* m_pos; - const char* m_end; - - const char* end() const - { - return m_end; - } - void reset() - { - m_pos = m_buffer; - } - void flush() - { - m_ostream.write(m_buffer, m_pos - m_buffer); - reset(); - } -public: - BufferedTextOutputStream(TextOutputStream& ostream) : m_ostream(ostream), m_pos(m_buffer), m_end(m_buffer+m_bufsize) - { - } - ~BufferedTextOutputStream() - { - flush(); - } - void write(const char c) - { - if(m_pos == end()) - { - flush(); - } - *m_pos++ = c; - } - std::size_t write(const char* buffer, std::size_t length) - { - const char*const end = buffer + length; - for(const char* p = buffer; p != end; ++p) - { - write(*p); - } - return length; - } -}; - class XMLEntityOutputStream { - BufferedTextOutputStream m_ostream; + SingleCharacterOutputStream m_ostream; public: XMLEntityOutputStream(TextOutputStream& ostream) : m_ostream(ostream) diff --git a/plugins/archivezip/zlibstream.h b/plugins/archivezip/zlibstream.h index 3ce9179..63def54 100644 --- a/plugins/archivezip/zlibstream.h +++ b/plugins/archivezip/zlibstream.h @@ -33,7 +33,7 @@ class DeflatedInputStream : public InputStream { InputStream& m_istream; z_stream m_zipstream; - static const int m_bufsize = 1024; + enum unnamed0 { m_bufsize = 1024 }; unsigned char m_buffer[m_bufsize]; public: diff --git a/radiant/console.cpp b/radiant/console.cpp index 5c46240..f47c66d 100644 --- a/radiant/console.cpp +++ b/radiant/console.cpp @@ -206,14 +206,17 @@ std::size_t Sys_Print(int level, const char* buf, std::size_t length) } - GtkTextBufferOutputStream textBuffer(buffer, &iter, tag); - if(!globalCharacterSet().isUTF8()) { - textBuffer << ConvertLocaleToUTF8(StringRange(buf, buf + length)); - } - else - { - textBuffer << StringRange(buf, buf + length); + GtkTextBufferOutputStream textBuffer(buffer, &iter, tag); + if(!globalCharacterSet().isUTF8()) + { + BufferedTextOutputStream buffered(textBuffer); + buffered << ConvertLocaleToUTF8(StringRange(buf, buf + length)); + } + else + { + textBuffer << StringRange(buf, buf + length); + } } // update console widget immediatly if we're doing something time-consuming diff --git a/radiant/undo.cpp b/radiant/undo.cpp index 5a15d34..001adbf 100644 --- a/radiant/undo.cpp +++ b/radiant/undo.cpp @@ -60,7 +60,7 @@ public: class RadiantUndoSystem : public UndoSystem { - static const int MAX_UNDO_LEVELS = 1024; + INTEGER_CONSTANT(MAX_UNDO_LEVELS, 1024); class Snapshot { @@ -295,9 +295,9 @@ public: } void setLevels(std::size_t levels) { - if(levels > MAX_UNDO_LEVELS) + if(levels > MAX_UNDO_LEVELS()) { - levels = MAX_UNDO_LEVELS; + levels = MAX_UNDO_LEVELS(); } while(m_undo_stack.size() > levels) diff --git a/radiant/xmlstuff.h b/radiant/xmlstuff.h index 34a52bf..0ffb8e2 100644 --- a/radiant/xmlstuff.h +++ b/radiant/xmlstuff.h @@ -58,7 +58,7 @@ struct message_info_t int geometry_depth; // are we parsing some geometry information (i.e. do we forward the SAX calls?) ISAXHandler* pGeometry; // the handler - static const int bufsize = 1024; + enum unnamed0 { bufsize = 1024 }; char m_buffer[bufsize]; std::size_t m_length; };