mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-29 15:52:07 +00:00
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.
This commit is contained in:
parent
7a59c63623
commit
ac9dc3db30
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user