@@ -25,9 +25,10 @@ public static class Bindings
25
25
// Holds objects and provides handles to them in the form of ints
26
26
public static class ObjectStore
27
27
{
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.
31
32
static Stack < int > handles ;
32
33
33
34
// Stored objects. The first is never used so 0 can be "null".
@@ -39,9 +40,8 @@ public static class ObjectStore
39
40
public static void Init ( int maxObjects )
40
41
{
41
42
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 ) ;
45
45
46
46
// Initialize the objects as all null plus room for the
47
47
// first to always be null.
@@ -67,28 +67,12 @@ public static int Store(object obj)
67
67
68
68
lock ( objects )
69
69
{
70
- // Get the hash of the object
71
- uint hash = ( uint ) obj . GetHashCode ( ) ;
72
-
73
70
// Pop a handle off the stack
74
71
int handle = handles . Pop ( ) ;
75
72
76
73
// Store the object
77
74
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 ) ;
92
76
93
77
return handle ;
94
78
}
@@ -109,22 +93,13 @@ public static int GetHandle(object obj)
109
93
110
94
lock ( objects )
111
95
{
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 ;
115
98
116
- if ( handleBucketByHash . TryGetValue ( hash , out handleBucket ) )
99
+ // Get handle from object cache
100
+ if ( objectHandleCache . TryGetValue ( obj , out handle ) )
117
101
{
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 ;
128
103
}
129
104
}
130
105
@@ -149,22 +124,8 @@ public static object Remove(int handle)
149
124
// Push the handle onto the stack
150
125
handles . Push ( handle ) ;
151
126
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 ) ;
168
129
169
130
return obj ;
170
131
}
0 commit comments