Registry.st
changeset 1466 dc55982da0b8
parent 1338 72dc10fbe8fd
child 1467 564c8b54aa98
--- a/Registry.st	Thu Jun 13 17:28:04 1996 +0200
+++ b/Registry.st	Fri Jun 14 13:13:01 1996 +0200
@@ -94,38 +94,37 @@
      p|
 
     registeredObjects isNil ifTrue:[
-	registeredObjects := WeakArray new:10.
-	registeredObjects watcher:self.
-	handleArray := Array basicNew:10.
-	registeredObjects at:1 put:anObject.
-	handleArray at:1 put:aHandle.
-	cleanState := true.
-	ObjectMemory addDependent:self.
-	^ self
+        registeredObjects := WeakArray new:10.
+        registeredObjects watcher:self.
+        handleArray := Array basicNew:10.
+        registeredObjects at:1 put:anObject.
+        handleArray at:1 put:aHandle.
+        ObjectMemory addDependent:self.
+        ^ self
     ].
 
     index := registeredObjects identityIndexOf:anObject ifAbsent:0.
     index ~~ 0 ifTrue:[
-	"already registered"
-	handleArray at:index put:aHandle.
-	self error:'object is already registered'.
-	^ self
+        "already registered"
+        handleArray at:index put:aHandle.
+        self error:'object is already registered'.
+        ^ self
     ].
 
     "search for a free slot, on the fly look for leftovers"
     index := registeredObjects identityIndexOf:nil startingAt:1.
     index ~~ 0 ifTrue:[
-	"is there a leftover ?"
-	p := handleArray at:index.
-	p notNil ifTrue:[
-	    "tell the phantom"
-	    handleArray at:index put:nil.
-	    self informDispose:p.
-	    p := nil.
-	].
-	registeredObjects at:index put:anObject.
-	handleArray at:index put:aHandle.
-	^ self
+        "is there a leftover ?"
+        p := handleArray at:index.
+        p notNil ifTrue:[
+            "tell the phantom"
+            handleArray at:index put:nil.
+            self informDispose:p.
+            p := nil.
+        ].
+        registeredObjects at:index put:anObject.
+        handleArray at:index put:aHandle.
+        ^ self
     ].
 
     "no free slot, add at the end"
@@ -179,20 +178,45 @@
     "an instance has been destroyed - look which one it was"
 
     |phantom
-     sz "{ Class: SmallInteger }"|
+     dstIdx "{ Class: SmallInteger }"
+     sz     "{ Class: SmallInteger }"
+     tally  "{ Class: SmallInteger }"
+     newObjects newHandles|
+
+    sz := handleArray size.
+    tally := 0.
+    1 to:sz do:[:index |
+        (registeredObjects at:index) isNil ifTrue:[
+            phantom := handleArray at:index.
+            phantom notNil ifTrue:[
+                handleArray at:index put:nil.
+                self informDispose:phantom
+            ]
+        ] ifFalse:[
+            tally := tally + 1
+        ]
+    ].
 
-    cleanState ifTrue:[
-	sz := handleArray size.
-	1 to:sz do:[:index |
-	    (registeredObjects at:index) isNil ifTrue:[
-		phantom := handleArray at:index.
-		phantom notNil ifTrue:[
-		    handleArray at:index put:nil.
-		    self informDispose:phantom
-		]
-	    ]
-	]
-    ]
+    sz > 50 ifTrue:[
+        tally < (sz // 2) ifTrue:[
+            "/ shrink
+            newObjects := WeakArray new:(tally * 3 // 2).
+            newHandles := Array new:(newObjects size).
+
+            dstIdx := 1.
+            1 to:sz do:[:index |
+                (phantom := registeredObjects at:index) notNil ifTrue:[
+                    newObjects at:dstIdx put:phantom.
+                    newHandles at:dstIdx put:(handleArray at:index).
+                    dstIdx := dstIdx + 1
+                ]
+            ].
+
+            newObjects watcher:self.
+            registeredObjects := newObjects.
+            handleArray := newHandles.
+        ]
+    ].
 !
 
 informDispose:someHandle
@@ -213,17 +237,17 @@
 
 update:aParameter
     aParameter == #earlyRestart ifTrue:[
-	handleArray notNil ifTrue:[
-	    handleArray atAllPut:nil
-	]
+        handleArray notNil ifTrue:[
+            handleArray atAllPut:nil
+        ]
     ].
-    aParameter == #returnFromSnapshot ifTrue:[
-	cleanState := true
-    ]
+"/    aParameter == #returnFromSnapshot ifTrue:[
+"/        cleanState := true
+"/    ]
 ! !
 
 !Registry class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Registry.st,v 1.25 1996-05-07 09:57:14 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Registry.st,v 1.26 1996-06-14 11:13:01 cg Exp $'
 ! !