@@ -25,9 +25,10 @@ public static class Bindings
2525 // Holds objects and provides handles to them in the form of ints
2626 public static class ObjectStore
2727 {
28- static Dictionary < uint , List < int > > handleBucketByHash ;
29- static Dictionary < int , uint > hashLookupByHandle ;
30- static HashSet < int > hash ;
28+ // Lookup handles by object.
29+ static Dictionary < object , int > objectHandleCache ;
30+
31+ // Stack of available handles.
3132 static Stack < int > handles ;
3233
3334 // Stored objects. The first is never used so 0 can be "null".
@@ -39,9 +40,8 @@ public static class ObjectStore
3940 public static void Init ( int maxObjects )
4041 {
4142 ObjectStore . maxObjects = maxObjects ;
42- handleBucketByHash = new Dictionary < uint , List < int > > ( ) ;
43- hashLookupByHandle = new Dictionary < int , uint > ( ) ;
44- handles = new Stack < int > ( ) ;
43+ objectHandleCache = new Dictionary < object , int > ( maxObjects ) ;
44+ handles = new Stack < int > ( maxObjects ) ;
4545
4646 // Initialize the objects as all null plus room for the
4747 // first to always be null.
@@ -67,28 +67,12 @@ public static int Store(object obj)
6767
6868 lock ( objects )
6969 {
70- // Get the hash of the object
71- uint hash = ( uint ) obj . GetHashCode ( ) ;
72-
7370 // Pop a handle off the stack
7471 int handle = handles . Pop ( ) ;
7572
7673 // Store the object
7774 objects [ handle ] = obj ;
78-
79- List < int > handleBucket = null ;
80-
81- // Create new handle bucket if it does not exist
82- if ( ! handleBucketByHash . TryGetValue ( hash , out handleBucket ) )
83- {
84- handleBucket = new List < int > ( ) ;
85- handleBucketByHash [ hash ] = handleBucket ;
86- }
87-
88- // Insert into hash table
89- handleBucket . Add ( handle ) ;
90-
91- hashLookupByHandle [ handle ] = hash ;
75+ objectHandleCache . Add ( obj , handle ) ;
9276
9377 return handle ;
9478 }
@@ -109,22 +93,13 @@ public static int GetHandle(object obj)
10993
11094 lock ( objects )
11195 {
112- // Look up the handle in the hash table
113- uint hash = ( uint ) obj . GetHashCode ( ) ;
114- List < int > handleBucket = null ;
96+ // A handle with a value of 0 is NULL
97+ int handle = 0 ;
11598
116- if ( handleBucketByHash . TryGetValue ( hash , out handleBucket ) )
99+ // Get handle from object cache
100+ if ( objectHandleCache . TryGetValue ( obj , out handle ) )
117101 {
118- for ( int i = 0 ; i < handleBucket . Count ; i ++ )
119- {
120- int handleInBucket = handleBucket [ i ] ;
121- object objectInBucket = objects [ handleInBucket ] ;
122-
123- if ( object . ReferenceEquals ( objectInBucket , obj ) )
124- {
125- return handleInBucket ;
126- }
127- }
102+ return handle ;
128103 }
129104 }
130105
@@ -149,22 +124,8 @@ public static object Remove(int handle)
149124 // Push the handle onto the stack
150125 handles . Push ( handle ) ;
151126
152- uint hash = hashLookupByHandle [ handle ] ;
153- List < int > handleBucket = null ;
154-
155- // Remove the object from the hash dictionary's
156- if ( handleBucketByHash . TryGetValue ( hash , out handleBucket ) )
157- {
158- for ( int i = 0 ; i < handleBucket . Count ; i ++ )
159- {
160- int handleInBucket = handleBucket [ i ] ;
161- if ( handleInBucket == handle )
162- {
163- handleBucket . RemoveAt ( i ) ;
164- break ;
165- }
166- }
167- }
127+ // Remove the object from the cache
128+ objectHandleCache . Remove ( obj ) ;
168129
169130 return obj ;
170131 }
0 commit comments