44using System . Collections ;
55using System . IO ;
66using System . Runtime . InteropServices ;
7+ using System . Collections . Generic ;
78
89using UnityEngine ;
910
@@ -24,25 +25,25 @@ public static class Bindings
2425 // Holds objects and provides handles to them in the form of ints
2526 public static class ObjectStore
2627 {
28+ // Lookup handles by object.
29+ static Dictionary < object , int > objectHandleCache ;
30+
2731 // Stored objects. The first is never used so 0 can be "null".
2832 static object [ ] objects ;
29-
30- // Stack of available handles
33+
34+ // Stack of available handles.
3135 static int [ ] handles ;
32-
33- // Hash table of stored objects to their handles.
34- static object [ ] keys ;
35- static int [ ] values ;
36-
36+
3737 // Index of the next available handle
3838 static int nextHandleIndex ;
39-
39+
4040 // The maximum number of objects to store. Must be positive.
4141 static int maxObjects ;
4242
4343 public static void Init ( int maxObjects )
4444 {
4545 ObjectStore . maxObjects = maxObjects ;
46+ objectHandleCache = new Dictionary < object , int > ( maxObjects ) ;
4647
4748 // Initialize the objects as all null plus room for the
4849 // first to always be null.
@@ -58,10 +59,6 @@ public static void Init(int maxObjects)
5859 handles [ i ] = handle ;
5960 }
6061 nextHandleIndex = maxObjects - 1 ;
61-
62- // Initialize the hash table
63- keys = new object [ maxObjects ] ;
64- values = new int [ maxObjects ] ;
6562 }
6663
6764 public static int Store ( object obj )
@@ -80,22 +77,7 @@ public static int Store(object obj)
8077
8178 // Store the object
8279 objects [ handle ] = obj ;
83-
84- // Insert into the hash table
85- int initialIndex = ( int ) (
86- ( ( uint ) obj . GetHashCode ( ) ) % maxObjects ) ;
87- int index = initialIndex ;
88- do
89- {
90- if ( object . ReferenceEquals ( keys [ index ] , null ) )
91- {
92- keys [ index ] = obj ;
93- values [ index ] = handle ;
94- break ;
95- }
96- index = ( index + 1 ) % maxObjects ;
97- }
98- while ( index != initialIndex ) ;
80+ objectHandleCache . Add ( obj , handle ) ;
9981
10082 return handle ;
10183 }
@@ -116,19 +98,13 @@ public static int GetHandle(object obj)
11698
11799 lock ( objects )
118100 {
119- // Look up the object in the hash table
120- int initialIndex = ( int ) (
121- ( ( uint ) obj . GetHashCode ( ) ) % maxObjects ) ;
122- int index = initialIndex ;
123- do
101+ int handle ;
102+
103+ // Get handle from object cache
104+ if ( objectHandleCache . TryGetValue ( obj , out handle ) )
124105 {
125- if ( object . ReferenceEquals ( keys [ index ] , obj ) )
126- {
127- return values [ index ] ;
128- }
129- index = ( index + 1 ) % maxObjects ;
106+ return handle ;
130107 }
131- while ( index != initialIndex ) ;
132108 }
133109
134110 // Object not found
@@ -152,26 +128,9 @@ public static object Remove(int handle)
152128 // Push the handle onto the stack
153129 nextHandleIndex ++ ;
154130 handles [ nextHandleIndex ] = handle ;
155-
156- // Remove the object from the hash table
157- int initialIndex = ( int ) (
158- ( ( uint ) obj . GetHashCode ( ) ) % maxObjects ) ;
159- int index = initialIndex ;
160- do
161- {
162- if ( object . ReferenceEquals ( keys [ index ] , obj ) )
163- {
164- // Only the key needs to be removed (set to null)
165- // because values corresponding to null will never
166- // be read and the values are just integers, so
167- // we're not holding on to a managed reference that
168- // will prevent GC.
169- keys [ index ] = null ;
170- break ;
171- }
172- index = ( index + 1 ) % maxObjects ;
173- }
174- while ( index != initialIndex ) ;
131+
132+ // Remove the object from the cache
133+ objectHandleCache . Remove ( obj ) ;
175134
176135 return obj ;
177136 }
0 commit comments