a few changes on priority queues and their heap

0.9.1.0-post-fixes
UbitUmarov 2019-01-25 20:46:03 +00:00
parent 27f5248d9f
commit e3d0ec6f40
2 changed files with 45 additions and 52 deletions

View File

@ -65,7 +65,7 @@ namespace OpenSim.Framework
{ {
if (handle != null) if (handle != null)
{ {
handle.Clear(); handle.heap = null;
handle = null; handle = null;
} }
value = default(T); value = default(T);
@ -96,8 +96,8 @@ namespace OpenSim.Framework
public MinHeap(Comparison<T> comparison) : this(DEFAULT_CAPACITY, comparison) { } public MinHeap(Comparison<T> comparison) : this(DEFAULT_CAPACITY, comparison) { }
public MinHeap(int _capacity, Comparison<T> _comparison) public MinHeap(int _capacity, Comparison<T> _comparison)
{ {
minCapacity = _capacity; minCapacity = 16;
items = new HeapItem[minCapacity]; items = new HeapItem[_capacity];
comparison = _comparison; comparison = _comparison;
size = version = 0; size = version = 0;
} }
@ -187,7 +187,8 @@ namespace OpenSim.Framework
private void BubbleDown(int index) private void BubbleDown(int index)
{ {
HeapItem item = items[index]; HeapItem item = items[index];
int current, child; int current;
int child;
for(current = index , child = (2 * current) + 1; for(current = index , child = (2 * current) + 1;
current < size / 2; current < size / 2;
@ -293,14 +294,17 @@ namespace OpenSim.Framework
throw new ArgumentOutOfRangeException("index"); throw new ArgumentOutOfRangeException("index");
items[index].Clear(); items[index].Clear();
if (--size > 0 && index != size) --size;
if (size > 0)
{ if(index != size)
{ {
Set(items[size], index); Set(items[size], index);
items[size].ClearRef(); items[size].ClearRef();
if (!BubbleUp(index)) if (!BubbleUp(index))
BubbleDown(index); BubbleDown(index);
} }
if(size == 0 && items.Length > 4 * minCapacity) }
else if(items.Length > 4 * minCapacity)
items = new HeapItem[minCapacity]; items = new HeapItem[minCapacity];
} }

View File

@ -46,14 +46,14 @@ namespace OpenSim.Framework
/// Total number of queues (priorities) available /// Total number of queues (priorities) available
/// </summary> /// </summary>
public const uint NumberOfQueues = 12; // includes immediate queues, m_queueCounts need to be set acording public const uint NumberOfQueues = 13; // includes immediate queues, m_queueCounts need to be set acording
/// <summary> /// <summary>
/// Number of queuest (priorities) that are processed immediately /// Number of queuest (priorities) that are processed immediately
/// </summary. /// </summary.
public const uint NumberOfImmediateQueues = 2; public const uint NumberOfImmediateQueues = 2;
// first queues are immediate, so no counts // first queues are immediate, so no counts
private static readonly uint[] m_queueCounts = {0, 0, 8, 8, 5, 4, 3, 2, 1, 1, 1, 1}; private static readonly uint[] m_queueCounts = {0, 0, 8, 8, 5, 4, 3, 2, 1, 1, 1, 1, 1 };
// this is ava, ava, attach, <10m, 20,40,80,160m,320,640,1280, + // this is ava, ava, attach, <10m, 20,40,80,160m,320,640,1280, +
private MinHeap<MinHeapItem>[] m_heaps = new MinHeap<MinHeapItem>[NumberOfQueues]; private MinHeap<MinHeapItem>[] m_heaps = new MinHeap<MinHeapItem>[NumberOfQueues];
@ -85,7 +85,7 @@ namespace OpenSim.Framework
public PriorityQueue(int capacity) public PriorityQueue(int capacity)
{ {
m_capacity = capacity; m_capacity = 16;
capacity /= 4; capacity /= 4;
for (int i = 0; i < m_heaps.Length; ++i) for (int i = 0; i < m_heaps.Length; ++i)
@ -137,14 +137,24 @@ namespace OpenSim.Framework
{ {
lookupH = lookup.Handle; lookupH = lookup.Handle;
entry = lookup.Heap[lookupH].EntryOrder; entry = lookup.Heap[lookupH].EntryOrder;
EntityUpdate up = lookup.Heap[lookupH].Value;
value.Update(lookup.Heap[lookupH].Value); value.Update(lookup.Heap[lookupH].Value);
lookup.Heap.Remove(lookupH); lookup.Heap.Remove(lookupH);
if((up.Flags & PrimUpdateFlags.CancelKill) != 0)
entry = m_nextRequest++;
pqueue = Util.Clamp<uint>(pqueue, 0, NumberOfQueues - 1);
lookup.Heap = m_heaps[pqueue];
lookup.Heap.Add(new MinHeapItem(pqueue, entry, value), ref lookup.Handle);
m_lookupTable[localid] = lookup;
return true;
} }
else
{ value.Update();
entry = m_nextRequest++; entry = m_nextRequest++;
++m_added; ++m_added;
}
pqueue = Util.Clamp<uint>(pqueue, 0, NumberOfQueues - 1); pqueue = Util.Clamp<uint>(pqueue, 0, NumberOfQueues - 1);
lookup.Heap = m_heaps[pqueue]; lookup.Heap = m_heaps[pqueue];
@ -178,7 +188,7 @@ namespace OpenSim.Framework
/// oldest item from the next queue in order to provide fair access to /// oldest item from the next queue in order to provide fair access to
/// all of the queues /// all of the queues
/// </summary> /// </summary>
public bool TryDequeue(out EntityUpdate value, out Int32 timeinqueue) public bool TryDequeue(out EntityUpdate value)
{ {
// If there is anything in immediate queues, return it first no // If there is anything in immediate queues, return it first no
// matter what else. Breaks fairness. But very useful. // matter what else. Breaks fairness. But very useful.
@ -189,7 +199,6 @@ namespace OpenSim.Framework
{ {
MinHeapItem item = m_heaps[iq].RemoveMin(); MinHeapItem item = m_heaps[iq].RemoveMin();
m_lookupTable.Remove(item.Value.Entity.LocalId); m_lookupTable.Remove(item.Value.Entity.LocalId);
timeinqueue = Util.EnvironmentTickCountSubtract(item.EntryTime);
value = item.Value; value = item.Value;
return true; return true;
@ -210,9 +219,7 @@ namespace OpenSim.Framework
MinHeapItem item = curheap.RemoveMin(); MinHeapItem item = curheap.RemoveMin();
m_lookupTable.Remove(item.Value.Entity.LocalId); m_lookupTable.Remove(item.Value.Entity.LocalId);
timeinqueue = Util.EnvironmentTickCountSubtract(item.EntryTime);
value = item.Value; value = item.Value;
return true; return true;
} }
@ -232,12 +239,10 @@ namespace OpenSim.Framework
MinHeapItem item = curheap.RemoveMin(); MinHeapItem item = curheap.RemoveMin();
m_lookupTable.Remove(item.Value.Entity.LocalId); m_lookupTable.Remove(item.Value.Entity.LocalId);
timeinqueue = Util.EnvironmentTickCountSubtract(item.EntryTime);
value = item.Value; value = item.Value;
return true; return true;
} }
timeinqueue = 0;
value = default(EntityUpdate); value = default(EntityUpdate);
if(m_lookupTable.Count == 0 && m_added > 8 * m_capacity) if(m_lookupTable.Count == 0 && m_added > 8 * m_capacity)
{ {
@ -247,23 +252,20 @@ namespace OpenSim.Framework
return false; return false;
} }
public bool TryOrderedDequeue(out EntityUpdate value, out Int32 timeinqueue) public bool TryOrderedDequeue(out EntityUpdate value)
{ {
MinHeap<MinHeapItem> curheap;
for (int iq = 0; iq < NumberOfQueues; ++iq) for (int iq = 0; iq < NumberOfQueues; ++iq)
{ {
curheap = m_heaps[iq]; MinHeap<MinHeapItem> curheap = m_heaps[iq];
if (curheap.Count > 0) if (curheap.Count > 0)
{ {
MinHeapItem item = curheap.RemoveMin(); MinHeapItem item = curheap.RemoveMin();
m_lookupTable.Remove(item.Value.Entity.LocalId); m_lookupTable.Remove(item.Value.Entity.LocalId);
timeinqueue = Util.EnvironmentTickCountSubtract(item.EntryTime);
value = item.Value; value = item.Value;
return true; return true;
} }
} }
timeinqueue = 0;
value = default(EntityUpdate); value = default(EntityUpdate);
if(m_lookupTable.Count == 0 && m_added > 8 * m_capacity) if(m_lookupTable.Count == 0 && m_added > 8 * m_capacity)
{ {
@ -280,13 +282,11 @@ namespace OpenSim.Framework
public void Reprioritize(UpdatePriorityHandler handler) public void Reprioritize(UpdatePriorityHandler handler)
{ {
MinHeapItem item; MinHeapItem item;
uint pqueue = 0;
foreach (LookupItem lookup in new List<LookupItem>(m_lookupTable.Values)) foreach (LookupItem lookup in new List<LookupItem>(m_lookupTable.Values))
{ {
if (lookup.Heap.TryGetValue(lookup.Handle, out item)) if (lookup.Heap.TryGetValue(lookup.Handle, out item))
{ {
uint pqueue = item.PriorityQueue;
uint localid = item.Value.Entity.LocalId;
if (handler(ref pqueue, item.Value.Entity)) if (handler(ref pqueue, item.Value.Entity))
{ {
// unless the priority queue has changed, there is no need to modify // unless the priority queue has changed, there is no need to modify
@ -299,14 +299,14 @@ namespace OpenSim.Framework
LookupItem litem = lookup; LookupItem litem = lookup;
litem.Heap = m_heaps[pqueue]; litem.Heap = m_heaps[pqueue];
litem.Heap.Add(new MinHeapItem(pqueue, item), ref litem.Handle); litem.Heap.Add(new MinHeapItem(pqueue, item), ref litem.Handle);
m_lookupTable[localid] = litem; m_lookupTable[item.Value.Entity.LocalId] = litem;
} }
} }
else else
{ {
// m_log.WarnFormat("[PQUEUE]: UpdatePriorityHandler returned false for {0}",item.Value.Entity.UUID); // m_log.WarnFormat("[PQUEUE]: UpdatePriorityHandler returned false for {0}",item.Value.Entity.UUID);
lookup.Heap.Remove(lookup.Handle); lookup.Heap.Remove(lookup.Handle);
m_lookupTable.Remove(localid); m_lookupTable.Remove(item.Value.Entity.LocalId);
} }
} }
} }
@ -345,15 +345,6 @@ namespace OpenSim.Framework
} }
} }
private Int32 entrytime;
internal Int32 EntryTime
{
get
{
return entrytime;
}
}
private UInt64 entryorder; private UInt64 entryorder;
internal UInt64 EntryOrder internal UInt64 EntryOrder
{ {
@ -365,7 +356,6 @@ namespace OpenSim.Framework
internal MinHeapItem(uint _pqueue, MinHeapItem other) internal MinHeapItem(uint _pqueue, MinHeapItem other)
{ {
entrytime = other.entrytime;
entryorder = other.entryorder; entryorder = other.entryorder;
value = other.value; value = other.value;
pqueue = _pqueue; pqueue = _pqueue;
@ -373,7 +363,6 @@ namespace OpenSim.Framework
internal MinHeapItem(uint _pqueue, UInt64 _entryorder, EntityUpdate _value) internal MinHeapItem(uint _pqueue, UInt64 _entryorder, EntityUpdate _value)
{ {
entrytime = Util.EnvironmentTickCount();
entryorder = _entryorder; entryorder = _entryorder;
value = _value; value = _value;
pqueue = _pqueue; pqueue = _pqueue;