the point is that weak links are expensive ... they both slower and consume more space ... here is some control code:
package { //{ region imports import flash.display.Sprite; import flash.events.Event; import flash.events.EventDispatcher; import flash.system.System; import flash.utils.*; //} endregion public class Main extends Sprite { public function Main():void { switch (0) { case 0: this.benchmarkDispatchers(false); break; case 1: this.benchmarkDispatchers(true); break; case 2: this.benchmarkDictionaries(false); break; case 3: this.benchmarkDictionaries(true); break; } } private function benchmarkDictionaries(weakKeys:Boolean, size:uint = 1000000):void { var a:Array = []; for (var i:int = 0; i < size; i++) a.push( { "foo":i } ); var d:Dictionary = new Dictionary(weakKeys); var start:int = getTimer(); var mem0:int = System.totalMemory; for (var j:int = 0; j < size; j++) d[a[j]] = j; trace("adding "+size+" keys took "+(getTimer()-start)+" msecs and "+(System.totalMemory-mem0)+" B of memory with weakKeys == "+weakKeys); } private function benchmarkDispatchers(weakRef:Boolean, size:uint = 100000):void { var a:Array = []; var f:Function = function (i:*):Function { return function ():void { i; } } for (var i:int = 0; i < size; i++) a.push( f(i) ); var e:EventDispatcher = new EventDispatcher(); var start:int = getTimer(); var mem0:uint = System.totalMemory; for (var j:int = 0; j < size; j++) e.addEventListener("foo", a[j], false, 0, weakRef); trace("adding " + size + " event handlers took " + (getTimer() - start) + " msecs and " + (System.totalMemory - mem0) + " B of memory with weakKeys == " + weakRef); } } }
this is what i get on my machine:
adding 100000 event handlers took 679 msecs and 6922240 B of memory with weakKeys == false adding 100000 event handlers took 1348 msecs and 13606912 B of memory with weakKeys == true adding 1000000 keys took 283 msecs and 16781312 B of memory with weakKeys == false adding 1000000 keys took 906 msecs and 42164224 B of memory with weakKeys == true
the results are a little more radical for dictionaries, most likely due to the lack of ActionScript calls with respect to time, and since some storage costs in registering an event handler reduce the difference between the required memory (as you can see, these are 69 bytes / handler and 16 bytes / key when comparing weak links) ...
so yes, it’s about performance ... using weak links is not a healthy fact that you don’t have to delete the listener for the object to die ... if you want to have a scalable application, you need to do such things yourself, and if you want it to be 100% reliable, you cannot hope that the GC will do your work, but you need to do the cleaning yourself ... and also, if you have a good hierarchy in your application, you probably don't run into this problem often often ... in a way, it's a luxury l if you do not want to spend time creating an appropriate object because problems that cannot be solved without weak links are rare ... it should be used when it offers a real advantage, and not just because of laziness ... I I think this is wrong by default ...
hope this helps ...;)
Greetz
back2dos
source share