diff --git a/src/DebugEngineHost.VSCode/VSCode/HandleCollection.cs b/src/DebugEngineHost.VSCode/VSCode/HandleCollection.cs index 64a26d7fe..1d23ea8a7 100644 --- a/src/DebugEngineHost.VSCode/VSCode/HandleCollection.cs +++ b/src/DebugEngineHost.VSCode/VSCode/HandleCollection.cs @@ -2,6 +2,8 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System; +using System.Linq; using System.Collections.Generic; namespace Microsoft.DebugEngineHost.VSCode @@ -9,62 +11,73 @@ namespace Microsoft.DebugEngineHost.VSCode public sealed class HandleCollection { private const int START_HANDLE = 1000; - private List _collection = new List(); - public void Clear() + private int _nextHandle; + private Dictionary _handleMap; + + public HandleCollection() + { + _nextHandle = START_HANDLE; + _handleMap = new Dictionary(); + } + + public void Reset() { - _collection.Clear(); + _nextHandle = START_HANDLE; + _handleMap.Clear(); } public int Create(T value) { - _collection.Add(value); - return _collection.Count + START_HANDLE - 1; + var handle = _nextHandle++; + _handleMap[handle] = value; + return handle; } public bool TryGet(int handle, out T value) { - if (handle < 0 || handle - START_HANDLE >= _collection.Count) + if (_handleMap.TryGetValue(handle, out value)) { - value = default; - return false; + return true; } - - value = _collection[handle - START_HANDLE]; - return true; + return false; } public bool TryGetFirst(out T value) { if (IsEmpty) { - value = default; + value = default(T); return false; } - value = _collection[0]; - return true; + return TryGet(_handleMap.Keys.Min(), out value); } public T this[int handle] { get { - return _collection[handle - START_HANDLE]; + T value; + if (!TryGet(handle, out value)) + { + throw new ArgumentOutOfRangeException(nameof(handle)); + } + + return value; } } public bool Remove(int handle) { - _collection.RemoveAt(handle - START_HANDLE); - return true; + return _handleMap.Remove(handle); } public bool IsEmpty { get { - return _collection.Count == 0; + return _handleMap.Count == 0; } } } diff --git a/src/OpenDebugAD7/AD7DebugSession.cs b/src/OpenDebugAD7/AD7DebugSession.cs index 10ebd7de4..9f65bcf1f 100644 --- a/src/OpenDebugAD7/AD7DebugSession.cs +++ b/src/OpenDebugAD7/AD7DebugSession.cs @@ -42,7 +42,7 @@ internal sealed class AD7DebugSession : DebugAdapterBase, IDebugPortNotify2, IDe private readonly DebugEventLogger m_logger; private readonly Dictionary> m_breakpoints; - private readonly HandleCollection m_gotoCodeContexts = new HandleCollection(); + private readonly List m_gotoCodeContexts = new List(); private Dictionary m_functionBreakpoints; private readonly Dictionary m_threadFrameEnumInfos = new Dictionary(); @@ -341,7 +341,7 @@ public void BeforeContinue() m_isStepping = false; m_isStopped = false; m_variableManager.Reset(); - m_frameHandles.Clear(); + m_frameHandles.Reset(); m_threadFrameEnumInfos.Clear(); m_gotoCodeContexts.Clear(); } diff --git a/src/OpenDebugAD7/VariableManager.cs b/src/OpenDebugAD7/VariableManager.cs index 09a5101cd..553ef9e06 100644 --- a/src/OpenDebugAD7/VariableManager.cs +++ b/src/OpenDebugAD7/VariableManager.cs @@ -26,7 +26,7 @@ internal VariableManager() internal void Reset() { - m_variableHandles.Clear(); + m_variableHandles.Reset(); } internal Boolean IsEmpty()