Hash: make .size() constant time

MessageTray calls .size() very often to update the no messages label,
so a linear time implementation is not good enough.

https://bugzilla.gnome.org/show_bug.cgi?id=700194
This commit is contained in:
Giovanni Campagna 2013-05-12 20:13:08 +02:00
parent c72ae375c8
commit b0dc841e00

View File

@ -58,6 +58,7 @@ const Map = new Lang.Class({
_init: function(iterable) { _init: function(iterable) {
this._pool = { }; this._pool = { };
this._size = 0;
if (iterable) { if (iterable) {
for (let i = 0; i < iterable.length; i++) { for (let i = 0; i < iterable.length; i++) {
@ -99,6 +100,7 @@ const Map = new Lang.Class({
node.value = value; node.value = value;
} else { } else {
this._pool[hash] = { key: key, value: value }; this._pool[hash] = { key: key, value: value };
this._size++;
} }
}, },
@ -108,6 +110,7 @@ const Map = new Lang.Class({
if (node && _sameValue(node.key, key)) { if (node && _sameValue(node.key, key)) {
delete this._pool[hash]; delete this._pool[hash];
this._size--;
return [node.key, node.value]; return [node.key, node.value];
} else { } else {
return [null, null]; return [null, null];
@ -136,6 +139,6 @@ const Map = new Lang.Class({
}, },
size: function() { size: function() {
return Object.getOwnPropertyNames(this._pool).length; return this._size;
}, },
}); });