@@ -28,11 +28,14 @@ public static class ObjectStore
28
28
// Lookup handles by object.
29
29
static Dictionary < object , int > objectHandleCache ;
30
30
31
- // Stack of available handles.
32
- static Stack < int > handles ;
33
-
34
31
// Stored objects. The first is never used so 0 can be "null".
35
32
static object [ ] objects ;
33
+
34
+ // Stack of available handles.
35
+ static int [ ] handles ;
36
+
37
+ // Index of the next available handle
38
+ static int nextHandleIndex ;
36
39
37
40
// The maximum number of objects to store. Must be positive.
38
41
static int maxObjects ;
@@ -41,20 +44,21 @@ public static void Init(int maxObjects)
41
44
{
42
45
ObjectStore . maxObjects = maxObjects ;
43
46
objectHandleCache = new Dictionary < object , int > ( maxObjects ) ;
44
- handles = new Stack < int > ( maxObjects ) ;
45
47
46
48
// Initialize the objects as all null plus room for the
47
49
// first to always be null.
48
50
objects = new object [ maxObjects + 1 ] ;
49
51
50
52
// Initialize the handles stack as 1, 2, 3, ...
53
+ handles = new int [ maxObjects ] ;
51
54
for (
52
55
int i = 0 , handle = maxObjects ;
53
56
i < maxObjects ;
54
57
++ i , -- handle )
55
58
{
56
- handles . Push ( handle ) ;
59
+ handles [ i ] = handle ;
57
60
}
61
+ nextHandleIndex = maxObjects - 1 ;
58
62
}
59
63
60
64
public static int Store ( object obj )
@@ -68,7 +72,8 @@ public static int Store(object obj)
68
72
lock ( objects )
69
73
{
70
74
// Pop a handle off the stack
71
- int handle = handles . Pop ( ) ;
75
+ int handle = handles [ nextHandleIndex ] ;
76
+ nextHandleIndex -- ;
72
77
73
78
// Store the object
74
79
objects [ handle ] = obj ;
@@ -121,7 +126,8 @@ public static object Remove(int handle)
121
126
objects [ handle ] = null ;
122
127
123
128
// Push the handle onto the stack
124
- handles . Push ( handle ) ;
129
+ nextHandleIndex ++ ;
130
+ handles [ nextHandleIndex ] = handle ;
125
131
126
132
// Remove the object from the cache
127
133
objectHandleCache . Remove ( obj ) ;
0 commit comments