From 0f4df1b9486d1c5e68a626d2a3ac61994704ace3 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Wed, 6 Apr 2022 20:07:38 -0700 Subject: [PATCH] optimize checking component existence --- src/IDStorage.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/IDStorage.cs b/src/IDStorage.cs index c46e238..26da620 100644 --- a/src/IDStorage.cs +++ b/src/IDStorage.cs @@ -5,12 +5,15 @@ internal class IDStorage private int nextID = 0; private readonly Stack availableIDs = new Stack(); + private readonly HashSet availableIDHash = new HashSet(); public int NextID() { if (availableIDs.Count > 0) { - return availableIDs.Pop(); + var id = availableIDs.Pop(); + availableIDHash.Remove(id); + return id; } else { @@ -22,11 +25,12 @@ internal class IDStorage public bool Taken(int id) { - return !availableIDs.Contains(id) && id < nextID; + return !availableIDHash.Contains(id) && id < nextID; } public void Release(int id) { availableIDs.Push(id); + availableIDHash.Add(id); } }