From ac9dc3db301e6f8b8450028365b61c04676df258 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Sun, 13 Jul 2014 22:05:27 -0700 Subject: [PATCH] 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 | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/dom/wifi/WifiCommand.jsm b/dom/wifi/WifiCommand.jsm index 904c8d2eca9b..35763de50368 100644 --- a/dom/wifi/WifiCommand.jsm +++ b/dom/wifi/WifiCommand.jsm @@ -240,6 +240,9 @@ this.WifiCommand = function(aControlMessage, aInterface, aSdkVersion) { }); }; + let infoKeys = [{regexp: /RSSI=/i, prop: 'rssi'}, + {regexp: /LINKSPEED=/i, prop: 'linkspeed'}]; + command.getConnectionInfoICS = function (callback) { doStringCommand("SIGNAL_POLL", function(reply) { if (!reply) { @@ -247,19 +250,21 @@ this.WifiCommand = function(aControlMessage, aInterface, aSdkVersion) { 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; } }