From 54b4523da85554dfeff9369ec8b7bfc438953dcc Mon Sep 17 00:00:00 2001 From: UbitUmarov Date: Sun, 6 Jan 2019 02:10:17 +0000 Subject: [PATCH] let DoubleDictionary... also have a values array --- .../DoubleDictionaryThreadAbortSafe.cs | 47 +++++++++++++++++++ .../Region/Framework/Scenes/EntityManager.cs | 4 +- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/OpenSim/Framework/DoubleDictionaryThreadAbortSafe.cs b/OpenSim/Framework/DoubleDictionaryThreadAbortSafe.cs index 816523b770..e4c85e412c 100644 --- a/OpenSim/Framework/DoubleDictionaryThreadAbortSafe.cs +++ b/OpenSim/Framework/DoubleDictionaryThreadAbortSafe.cs @@ -41,18 +41,27 @@ namespace OpenSim.Framework { Dictionary Dictionary1; Dictionary Dictionary2; + private TValue[] m_array; + private int m_lastArrayVersion; + private int m_arrayVersion; + ReaderWriterLockSlim rwLock = new ReaderWriterLockSlim(); public DoubleDictionaryThreadAbortSafe() { Dictionary1 = new Dictionary(); Dictionary2 = new Dictionary(); + m_array = new TValue[0]; + m_lastArrayVersion = 0; + m_arrayVersion = 0; } public DoubleDictionaryThreadAbortSafe(int capacity) { Dictionary1 = new Dictionary(capacity); Dictionary2 = new Dictionary(capacity); + m_lastArrayVersion = 0; + m_arrayVersion = 0; } ~DoubleDictionaryThreadAbortSafe() @@ -86,6 +95,7 @@ namespace OpenSim.Framework } Dictionary1[key1] = value; Dictionary2[key2] = value; + ++m_arrayVersion; } } finally @@ -112,6 +122,7 @@ namespace OpenSim.Framework gotLock = true; Dictionary1.Remove(key1); success = Dictionary2.Remove(key2); + ++m_arrayVersion; } } finally @@ -153,6 +164,7 @@ namespace OpenSim.Framework { Dictionary1.Remove(key1); Dictionary2.Remove(kvp.Key); + ++m_arrayVersion; } found = true; break; @@ -199,6 +211,7 @@ namespace OpenSim.Framework { Dictionary2.Remove(key2); Dictionary1.Remove(kvp.Key); + ++m_arrayVersion; } found = true; break; @@ -231,6 +244,9 @@ namespace OpenSim.Framework gotLock = true; Dictionary1.Clear(); Dictionary2.Clear(); + m_array = new TValue[0]; + m_arrayVersion = 0; + m_lastArrayVersion = 0; } } finally @@ -497,6 +513,7 @@ namespace OpenSim.Framework for (int i = 0; i < list2.Count; i++) Dictionary2.Remove(list2[i]); + ++m_arrayVersion; } } finally @@ -513,5 +530,35 @@ namespace OpenSim.Framework return list.Count; } + + public TValue[] GetArray() + { + TValue[] ret = new TValue[0]; + bool gotLock = false; + try + { + try { } + finally + { + rwLock.EnterWriteLock(); + gotLock = true; + + if (m_lastArrayVersion != m_arrayVersion) + { + TValue[] array = new TValue[Dictionary1.Count]; + Dictionary1.Values.CopyTo(array, 0); + m_array = array; + m_lastArrayVersion = m_arrayVersion; + } + ret = m_array; + } + } + finally + { + if (gotLock) + rwLock.ExitWriteLock(); + } + return ret; + } } } \ No newline at end of file diff --git a/OpenSim/Region/Framework/Scenes/EntityManager.cs b/OpenSim/Region/Framework/Scenes/EntityManager.cs index 4654efe29a..f034de898f 100644 --- a/OpenSim/Region/Framework/Scenes/EntityManager.cs +++ b/OpenSim/Region/Framework/Scenes/EntityManager.cs @@ -94,9 +94,7 @@ namespace OpenSim.Region.Framework.Scenes public EntityBase[] GetEntities() { - List tmp = new List(m_entities.Count); - ForEach(delegate(EntityBase entity) { tmp.Add(entity); }); - return tmp.ToArray(); + return m_entities.GetArray(); } public void ForEach(Action action)