* Store script timers in a dictionary rather than a list to make unset much more efficient
* Thanks dslake
0.6.5-rc1
Justin Clarke Casey 2009-03-18 20:24:53 +00:00
parent a7d610ffcc
commit c0c929665f
2 changed files with 23 additions and 10 deletions

View File

@ -52,6 +52,7 @@ Patches
* daTwitch * daTwitch
* devalnor-#708 * devalnor-#708
* dmiles (Daxtron Labs) * dmiles (Daxtron Labs)
* dslake (Intel)
* Gerhard * Gerhard
* Godfrey * Godfrey
* Grumly57 * Grumly57

View File

@ -45,6 +45,12 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api.Plugins
// //
// TIMER // TIMER
// //
static private string MakeTimerKey(uint localID, UUID itemID)
{
return localID.ToString() + itemID.ToString();
}
private class TimerClass private class TimerClass
{ {
public uint localID; public uint localID;
@ -55,15 +61,16 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api.Plugins
public long next; public long next;
} }
private List<TimerClass> Timers = new List<TimerClass>(); private Dictionary<string,TimerClass> Timers = new Dictionary<string,TimerClass>();
private object TimerListLock = new object(); private object TimerListLock = new object();
public void SetTimerEvent(uint m_localID, UUID m_itemID, double sec) public void SetTimerEvent(uint m_localID, UUID m_itemID, double sec)
{ {
// Always remove first, in case this is a re-set
UnSetTimerEvents(m_localID, m_itemID);
if (sec == 0) // Disabling timer if (sec == 0) // Disabling timer
{
UnSetTimerEvents(m_localID, m_itemID);
return; return;
}
// Add to timer // Add to timer
TimerClass ts = new TimerClass(); TimerClass ts = new TimerClass();
@ -75,21 +82,24 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api.Plugins
//ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval); //ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval);
ts.next = DateTime.Now.Ticks + ts.interval; ts.next = DateTime.Now.Ticks + ts.interval;
string key = MakeTimerKey(m_localID, m_itemID);
lock (TimerListLock) lock (TimerListLock)
{ {
Timers.Add(ts); // Adds if timer doesn't exist, otherwise replaces with new timer
Timers[key] = ts;
} }
} }
public void UnSetTimerEvents(uint m_localID, UUID m_itemID) public void UnSetTimerEvents(uint m_localID, UUID m_itemID)
{ {
// Remove from timer // Remove from timer
string key = MakeTimerKey(m_localID, m_itemID);
lock (TimerListLock) lock (TimerListLock)
{ {
foreach (TimerClass ts in new ArrayList(Timers)) if (Timers.ContainsKey(key))
{ {
if (ts.localID == m_localID && ts.itemID == m_itemID) Timers.Remove(key);
Timers.Remove(ts);
} }
} }
} }
@ -103,7 +113,8 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api.Plugins
lock (TimerListLock) lock (TimerListLock)
{ {
// Go through all timers // Go through all timers
foreach (TimerClass ts in Timers) Dictionary<string, TimerClass>.ValueCollection tvals = Timers.Values;
foreach (TimerClass ts in tvals)
{ {
// Time has passed? // Time has passed?
if (ts.next < DateTime.Now.Ticks) if (ts.next < DateTime.Now.Ticks)
@ -128,7 +139,8 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api.Plugins
lock (TimerListLock) lock (TimerListLock)
{ {
foreach (TimerClass ts in Timers) Dictionary<string, TimerClass>.ValueCollection tvals = Timers.Values;
foreach (TimerClass ts in tvals)
{ {
if (ts.itemID == itemID) if (ts.itemID == itemID)
{ {
@ -155,7 +167,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api.Plugins
ts.next = DateTime.Now.Ticks + (long)data[idx+1]; ts.next = DateTime.Now.Ticks + (long)data[idx+1];
idx += 2; idx += 2;
Timers.Add(ts); Timers.Add(MakeTimerKey(localID,itemID), ts);
} }
} }
} }