Bug 1585224 - Cache calUtils getService calls; r=Fallen
authorGeoff Lankow <geoff@darktrojan.net>
Tue, 01 Oct 2019 14:30:42 +1300
changeset 27857 b3b5a9cd7a32763d0f3f3a1719296824c1af9eca
parent 27856 e7476e934b191ffac787208323191c9c034049ce
child 27858 58d372a30be2d1d5b5a9ab430155ae7efc8e29cd
push id16519
push usergeoff@darktrojan.net
push dateMon, 07 Oct 2019 07:38:46 +0000
treeherdercomm-central@72b1cec1260e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersFallen
bugs1585224
Bug 1585224 - Cache calUtils getService calls; r=Fallen
calendar/base/modules/calUtils.jsm
--- a/calendar/base/modules/calUtils.jsm
+++ b/calendar/base/modules/calUtils.jsm
@@ -13,16 +13,21 @@ Cc["@mozilla.org/calendar/backend-loader
 
 // The calendar console instance
 var gCalendarConsole = new ConsoleAPI({
   prefix: "Lightning",
   consoleID: "calendar",
   maxLogLevel: Services.prefs.getBoolPref("calendar.debug.log", false) ? "all" : "warn",
 });
 
+// Cache services to avoid calling getService over and over again. The cache is
+// a separate object to avoid polluting `cal`, and is defined here since a call
+// to `_service` will require it to already exist.
+var gServiceCache = {};
+
 this.EXPORTED_SYMBOLS = ["cal"];
 var cal = {
   // These functions exist to reduce boilerplate code for creating instances
   // as well as getting services and other (cached) objects.
   createEvent: _instance("@mozilla.org/calendar/event;1", Ci.calIEvent, "icalString"),
   createTodo: _instance("@mozilla.org/calendar/todo;1", Ci.calITodo, "icalString"),
   createDateTime: _instance("@mozilla.org/calendar/datetime;1", Ci.calIDateTime, "icalString"),
   createDuration: _instance("@mozilla.org/calendar/duration;1", Ci.calIDuration, "icalString"),
@@ -45,30 +50,27 @@ var cal = {
     "icalString"
   ),
   createRecurrenceInfo: _instance(
     "@mozilla.org/calendar/recurrence-info;1",
     Ci.calIRecurrenceInfo,
     "item"
   ),
 
-  getCalendarManager: _service("@mozilla.org/calendar/manager;1", Ci.calICalendarManager),
-  getIcsService: _service("@mozilla.org/calendar/ics-service;1", Ci.calIICSService),
-  getTimezoneService: _service("@mozilla.org/calendar/timezone-service;1", Ci.calITimezoneService),
+  getCalendarManager: _service("@mozilla.org/calendar/manager;1", "calICalendarManager"),
+  getIcsService: _service("@mozilla.org/calendar/ics-service;1", "calIICSService"),
+  getTimezoneService: _service("@mozilla.org/calendar/timezone-service;1", "calITimezoneService"),
   getCalendarSearchService: _service(
     "@mozilla.org/calendar/calendarsearch-service;1",
-    Ci.calICalendarSearchProvider
+    "calICalendarSearchProvider"
   ),
-  getFreeBusyService: _service("@mozilla.org/calendar/freebusy-service;1", Ci.calIFreeBusyService),
-  getWeekInfoService: _service("@mozilla.org/calendar/weekinfo-service;1", Ci.calIWeekInfoService),
-  getDateFormatter: _service(
-    "@mozilla.org/calendar/datetime-formatter;1",
-    Ci.calIDateTimeFormatter
-  ),
-  getDragService: _service("@mozilla.org/widget/dragservice;1", Ci.nsIDragService),
+  getFreeBusyService: _service("@mozilla.org/calendar/freebusy-service;1", "calIFreeBusyService"),
+  getWeekInfoService: _service("@mozilla.org/calendar/weekinfo-service;1", "calIWeekInfoService"),
+  getDateFormatter: _service("@mozilla.org/calendar/datetime-formatter;1", "calIDateTimeFormatter"),
+  getDragService: _service("@mozilla.org/widget/dragservice;1", "nsIDragService"),
 
   /**
    * The calendar console instance
    */
   console: gCalendarConsole,
 
   /**
    * Logs a calendar message to the console. Needs calendar.debug.log enabled to show messages.
@@ -548,18 +550,20 @@ XPCOMUtils.defineLazyModuleGetter(
 /**
  * Returns a function that provides access to the given service.
  *
  * @param cid           The contract id to create
  * @param iid           The interface id to create with
  * @return {function}   A function that returns the given service
  */
 function _service(cid, iid) {
+  let name = `_${iid}`;
+  XPCOMUtils.defineLazyServiceGetter(gServiceCache, name, cid, iid);
   return function() {
-    return Cc[cid].getService(iid);
+    return gServiceCache[name];
   };
 }
 
 /**
  * Returns a function that creates an instance of the given component and
  * optionally initializes it using the property name passed.
  *
  * @param cid           The contract id to create