Bug 1389381 - Part 2: Add slop to the maximum size of a LimitedSet. r=aswan, a=sledru
authorKris Maglione <maglione.k@gmail.com>
Sat, 12 Aug 2017 11:08:35 -0700
changeset 421168 ffabdaaebceac4ac77c595703494b6f919d998e8
parent 421167 3323f34f84bb5303aeb01dc6dd07d7ccb1abefb4
child 421169 b301a9babee6ac845939eb93352988b321b09efd
push id7612
push userryanvm@gmail.com
push dateMon, 14 Aug 2017 14:21:34 +0000
treeherdermozilla-beta@b301a9babee6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaswan, sledru
bugs1389381
milestone56.0
Bug 1389381 - Part 2: Add slop to the maximum size of a LimitedSet. r=aswan, a=sledru Truncating a Set can be relatively expensive, so truncating only after several excessive entries have been added can save some cycles. MozReview-Commit-ID: DIAXQ3ow2fa
toolkit/components/extensions/ExtensionUtils.jsm
--- a/toolkit/components/extensions/ExtensionUtils.jsm
+++ b/toolkit/components/extensions/ExtensionUtils.jsm
@@ -258,34 +258,43 @@ class EventEmitter {
 
     return Promise.all(promises);
   }
 }
 
 /**
  * A set with a limited number of slots, which flushes older entries as
  * newer ones are added.
+ *
+ * @param {integer} limit
+ *        The maximum size to trim the set to after it grows too large.
+ * @param {integer} [slop = limit * .25]
+ *        The number of extra entries to allow in the set after it
+ *        reaches the size limit, before it is truncated to the limit.
+ * @param {iterable} [iterable]
+ *        An iterable of initial entries to add to the set.
  */
 class LimitedSet extends Set {
-  constructor(limit, iterable = undefined) {
+  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;
       }
       this.delete(item);
     }
   }
 
   add(item) {
-    if (!this.has(item) && this.size >= this.limit) {
+    if (!this.has(item) && this.size >= this.limit + this.slop) {
       this.truncate(this.limit - 1);
     }
     super.add(item);
   }
 }
 
 /**
  * Returns a Promise which resolves when the given document's DOM has