Bug 1389381: Part 3 - Loop over the entire set iterator when truncating a LimitedSet. r?aswan
MozReview-Commit-ID: 3imHF9IRI2N
--- a/toolkit/components/extensions/ExtensionUtils.jsm
+++ b/toolkit/components/extensions/ExtensionUtils.jsm
@@ -274,20 +274,24 @@ class LimitedSet extends Set {
constructor(limit, slop = Math.round(limit * .25), iterable = undefined) {
super(iterable);
this.limit = limit;
this.slop = slop;
}
truncate(limit) {
for (let item of this) {
- if (this.size <= limit) {
- break;
+ // Live set iterators can ge relatively expensive, since they need
+ // to be updated after every modification to the set. Since
+ // breaking out of the loop early will keep the iterator alive
+ // until the next full GC, we're currently better off finishing
+ // the entire loop even after we're done truncating.
+ if (this.size > limit) {
+ this.delete(item);
}
- this.delete(item);
}
}
add(item) {
if (!this.has(item) && this.size >= this.limit + this.slop) {
this.truncate(this.limit - 1);
}
super.add(item);