Bug 1037302 - Avoid excess string creation in WifiCommand.jsm's getConnectionInfoICS(). r=hchang.
authorNicholas Nethercote <nnethercote@mozilla.com>
Sun, 13 Jul 2014 22:05:27 -0700
changeset 215590 8112c02301302f494a5701b2687f9caf6138799c
parent 215589 c8d9bdeb391612e8f6f8791dfa1a9f9b50413040
child 215591 4580b85d6c447ee94a8538e9c4e5cdfb13d51067
push id515
push userraliiev@mozilla.com
push dateMon, 06 Oct 2014 12:51:51 +0000
treeherdermozilla-release@267c7a481bef [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewershchang
bugs1037302
milestone33.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1037302 - Avoid excess string creation in WifiCommand.jsm's getConnectionInfoICS(). r=hchang. This changes the code to use search() and indexOf() to find the boundaries of the relevant values, and substring() to extract them. This reduces the number of strings created on each invocation by 8x. The patch changes the behaviour if a key appears more than once. With the old code the last occurrence would be used. With the new code the first one is used. Hopefully this doesn't matter.
dom/wifi/WifiCommand.jsm
--- a/dom/wifi/WifiCommand.jsm
+++ b/dom/wifi/WifiCommand.jsm
@@ -235,36 +235,41 @@ this.WifiCommand = function(aControlMess
     doStringCommand("DRIVER LINKSPEED", function(reply) {
       if (reply) {
         reply = reply.split(" ")[1] | 0; // Format: LinkSpeed XX
       }
       callback(reply);
     });
   };
 
+  let infoKeys = [{regexp: /RSSI=/i,      prop: 'rssi'},
+                  {regexp: /LINKSPEED=/i, prop: 'linkspeed'}];
+
   command.getConnectionInfoICS = function (callback) {
     doStringCommand("SIGNAL_POLL", function(reply) {
       if (!reply) {
         callback(null);
         return;
       }
 
+      // Find any values matching |infoKeys|. This gets executed frequently
+      // enough that we want to avoid creating intermediate strings as much as
+      // possible.
       let rval = {};
-      var lines = reply.split("\n");
-      for (let i = 0; i < lines.length; ++i) {
-        let [key, value] = lines[i].split("=");
-        switch (key.toUpperCase()) {
-          case "RSSI":
-            rval.rssi = value | 0;
-            break;
-          case "LINKSPEED":
-            rval.linkspeed = value | 0;
-            break;
-          default:
-            // Ignore.
+      for (let i = 0; i < infoKeys.length; i++) {
+        let re = infoKeys[i].regexp;
+        let iKeyStart = reply.search(re);
+        if (iKeyStart !== -1) {
+          let prop = infoKeys[i].prop;
+          let iValueStart = reply.indexOf('=', iKeyStart) + 1;
+          let iNewlineAfterValue = reply.indexOf('\n', iValueStart);
+          let iValueEnd = iNewlineAfterValue !== -1
+                        ? iNewlineAfterValue
+                        : reply.length;
+          rval[prop] = reply.substring(iValueStart, iValueEnd) | 0;
         }
       }
 
       callback(rval);
     });
   };
 
   command.getMacAddress = function (callback) {