prevent CLogWindow::UpdateLog from constantly burning through timer objects, because it was allocating memory a lot and making it hard to debug some things. I think this should be faster even in release builds, but any speedup is probably too tiny to measure.

This commit is contained in:
nitsuja 2011-12-30 20:51:17 -08:00
parent f0d7b8122f
commit 0d9e87da18
2 changed files with 42 additions and 36 deletions

View File

@ -39,7 +39,7 @@ CLogWindow::CLogWindow(CFrame *parent, wxWindowID id, const wxPoint& pos,
const wxSize& size, long style, const wxString& name) const wxSize& size, long style, const wxString& name)
: wxPanel(parent, id, pos, size, style, name) : wxPanel(parent, id, pos, size, style, name)
, x(0), y(0), winpos(0) , x(0), y(0), winpos(0)
, Parent(parent) , m_LogAccess(true) , Parent(parent), m_ignoreLogTimer(false), m_LogAccess(true)
, m_Log(NULL), m_cmdline(NULL), m_FontChoice(NULL) , m_Log(NULL), m_cmdline(NULL), m_FontChoice(NULL)
, m_SJISConv(*(wxCSConv*)wxConvCurrent) , m_SJISConv(*(wxCSConv*)wxConvCurrent)
{ {
@ -279,6 +279,7 @@ void CLogWindow::OnWrapLineCheck(wxCommandEvent& event)
void CLogWindow::OnLogTimer(wxTimerEvent& WXUNUSED(event)) void CLogWindow::OnLogTimer(wxTimerEvent& WXUNUSED(event))
{ {
if (!m_LogAccess) return; if (!m_LogAccess) return;
if (m_ignoreLogTimer) return;
UpdateLog(); UpdateLog();
// Scroll to the last line // Scroll to the last line
@ -294,52 +295,56 @@ void CLogWindow::UpdateLog()
if (!m_LogAccess) return; if (!m_LogAccess) return;
if (!m_Log) return; if (!m_Log) return;
m_LogTimer->Stop(); // m_LogTimer->Stop();
// instead of stopping the timer, let's simply ignore its calls during UpdateLog,
// because repeatedly stopping and starting a timer churns memory (and potentially leaks it).
m_ignoreLogTimer = true;
if (!msgQueue.empty())
{ {
std::lock_guard<std::mutex> lk(m_LogSection); std::lock_guard<std::mutex> lk(m_LogSection);
int msgQueueSize = (int)msgQueue.size(); int msgQueueSize = (int)msgQueue.size();
for (int i = 0; i < msgQueueSize; i++) for (int i = 0; i < msgQueueSize; i++)
{
switch (msgQueue.front().first)
{ {
switch (msgQueue.front().first)
case ERROR_LEVEL: {
m_Log->SetDefaultStyle(wxTextAttr(*wxRED)); case ERROR_LEVEL:
break; m_Log->SetDefaultStyle(wxTextAttr(*wxRED));
break;
case WARNING_LEVEL: case WARNING_LEVEL:
m_Log->SetDefaultStyle(wxTextAttr(wxColour(255, 255, 0))); // YELLOW m_Log->SetDefaultStyle(wxTextAttr(wxColour(255, 255, 0))); // YELLOW
break; break;
case NOTICE_LEVEL: case NOTICE_LEVEL:
m_Log->SetDefaultStyle(wxTextAttr(*wxGREEN)); m_Log->SetDefaultStyle(wxTextAttr(*wxGREEN));
break; break;
case INFO_LEVEL: case INFO_LEVEL:
m_Log->SetDefaultStyle(wxTextAttr(*wxCYAN)); m_Log->SetDefaultStyle(wxTextAttr(*wxCYAN));
break; break;
case DEBUG_LEVEL: case DEBUG_LEVEL:
m_Log->SetDefaultStyle(wxTextAttr(*wxLIGHT_GREY)); m_Log->SetDefaultStyle(wxTextAttr(*wxLIGHT_GREY));
break; break;
default: default:
m_Log->SetDefaultStyle(wxTextAttr(*wxWHITE)); m_Log->SetDefaultStyle(wxTextAttr(*wxWHITE));
break; break;
}
if (msgQueue.front().second.size())
{
int j = m_Log->GetLastPosition();
m_Log->AppendText(msgQueue.front().second);
// White timestamp
m_Log->SetStyle(j, j + 9, wxTextAttr(*wxWHITE));
}
msgQueue.pop();
} }
if (msgQueue.front().second.size())
{
int j = m_Log->GetLastPosition();
m_Log->AppendText(msgQueue.front().second);
// White timestamp
m_Log->SetStyle(j, j + 9, wxTextAttr(*wxWHITE));
}
msgQueue.pop();
}
} // unlock log } // unlock log
m_LogTimer->Start(UPDATETIME); // m_LogTimer->Start(UPDATETIME);
m_ignoreLogTimer = false;
} }
void CLogWindow::Log(LogTypes::LOG_LEVELS level, const char *text) void CLogWindow::Log(LogTypes::LOG_LEVELS level, const char *text)

View File

@ -62,6 +62,7 @@ private:
wxFont DefaultFont, MonoSpaceFont; wxFont DefaultFont, MonoSpaceFont;
std::vector<wxFont> LogFont; std::vector<wxFont> LogFont;
wxTimer *m_LogTimer; wxTimer *m_LogTimer;
bool m_ignoreLogTimer;
LogManager *m_LogManager; LogManager *m_LogManager;
std::queue<std::pair<u8, wxString> > msgQueue; std::queue<std::pair<u8, wxString> > msgQueue;
bool m_writeFile, m_writeConsole, m_writeWindow, m_LogAccess; bool m_writeFile, m_writeConsole, m_writeWindow, m_LogAccess;