optimize InputFilter::Update (.5ms -> .05ms on k7/1000)

This commit is contained in:
Glenn Maynard
2005-10-24 06:32:30 +00:00
parent 2791f8f90b
commit e8f7db11f3
+27 -11
View File
@@ -5,7 +5,23 @@
#include "RageUtil.h" #include "RageUtil.h"
#include "RageThreads.h" #include "RageThreads.h"
#include "Preference.h" #include "Preference.h"
#include "Foreach.h"
#include <set>
namespace
{
/* Maintain a set of all interesting buttons: buttons which are being held
* down, or which were held down and need a RELEASE event. We use this to
* optimize InputFilter::Update, so we don't have to process every button
* we know about when most of them aren't in use. This set is protected
* by queuemutex. */
typedef pair<InputDevice,DeviceButton> Button;
set<Button> g_ButtonsToProcess;
void ActivateButton( const DeviceInput &di )
{
g_ButtonsToProcess.insert( make_pair(di.device, di.button) );
}
}
/* /*
* Some input devices require debouncing. Do this on both press and release. After * Some input devices require debouncing. Do this on both press and release. After
* reporting a change in state, don't report another for the debounce period. If a * reporting a change in state, don't report another for the debounce period. If a
@@ -101,6 +117,8 @@ void InputFilter::ButtonPressed( const DeviceInput &di, bool Down )
bs.m_BeingHeld = Down; bs.m_BeingHeld = Down;
bs.m_BeingHeldTime = di.ts; bs.m_BeingHeldTime = di.ts;
} }
ActivateButton( di );
} }
void InputFilter::SetButtonComment( const DeviceInput &di, const CString &sComment ) void InputFilter::SetButtonComment( const DeviceInput &di, const CString &sComment )
@@ -120,6 +138,7 @@ void InputFilter::ResetDevice( InputDevice device )
void InputFilter::Update(float fDeltaTime) void InputFilter::Update(float fDeltaTime)
{ {
RageTimer foo;
RageTimer now; RageTimer now;
// Constructing the DeviceInput inside the nested loops caues terrible // Constructing the DeviceInput inside the nested loops caues terrible
@@ -138,18 +157,14 @@ void InputFilter::Update(float fDeltaTime)
* things like "key pressed, key release, key repeat". */ * things like "key pressed, key release, key repeat". */
LockMut(*queuemutex); LockMut(*queuemutex);
// Don't reconstruct "di" inside the loop. This line alone is
// taking 4% of the CPU on a P3-666.
DeviceInput di( (InputDevice)0,0,1.0f,now); DeviceInput di( (InputDevice)0,0,1.0f,now);
FOREACH_InputDevice( d ) set<Button> Buttons( g_ButtonsToProcess );
FOREACHS( Button, Buttons, b )
{ {
di.device = d; di.device = b->first;
di.button = b->second;
for( int b=0; b < GetNumDeviceButtons(d); b++ ) // foreach button ButtonState &bs = m_ButtonState[di.device][di.button];
{
ButtonState &bs = m_ButtonState[d][b];
di.button = b;
di.level = bs.m_Level; di.level = bs.m_Level;
/* Generate IET_FIRST_PRESS and IET_RELEASE events. */ /* Generate IET_FIRST_PRESS and IET_RELEASE events. */
@@ -172,7 +187,10 @@ void InputFilter::Update(float fDeltaTime)
/* Generate IET_FAST_REPEAT and IET_SLOW_REPEAT events. */ /* Generate IET_FAST_REPEAT and IET_SLOW_REPEAT events. */
if( !bs.m_bLastReportedHeld ) if( !bs.m_bLastReportedHeld )
{
g_ButtonsToProcess.erase( make_pair(di.device, di.button) );
continue; continue;
}
const float fOldHoldTime = bs.m_fSecsHeld; const float fOldHoldTime = bs.m_fSecsHeld;
bs.m_fSecsHeld += fDeltaTime; bs.m_fSecsHeld += fDeltaTime;
@@ -217,8 +235,6 @@ void InputFilter::Update(float fDeltaTime)
} }
} }
}
bool InputFilter::IsBeingPressed( const DeviceInput &di ) bool InputFilter::IsBeingPressed( const DeviceInput &di )
{ {
return m_ButtonState[di.device][di.button].m_bLastReportedHeld; return m_ButtonState[di.device][di.button].m_bLastReportedHeld;