Bug 363318: use native btoa() where possible, and optimize the JS-implemented b64() for the search service (large slowdown seen when the feed preview page is shown), r=mano

git-svn-id: svn://10.0.0.236/trunk@216858 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
gavin%gavinsharp.com
2006-12-11 21:29:38 +00:00
parent 06fd784c9f
commit 14050ba106
2 changed files with 35 additions and 54 deletions

View File

@@ -981,40 +981,6 @@ FeedWriter.prototype = {
}
};
// copied over from nsSearchService.js
function b64(aBytes) {
const B64_CHARS =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var out = "", bits, i, j;
while (aBytes.length >= 3) {
bits = 0;
for (i = 0; i < 3; i++) {
bits <<= 8;
bits |= aBytes[i];
}
for (j = 18; j >= 0; j -= 6)
out += B64_CHARS[(bits>>j) & 0x3F];
aBytes.splice(0, 3);
}
switch (aBytes.length) {
case 2:
out += B64_CHARS[(aBytes[0]>>2) & 0x3F];
out += B64_CHARS[((aBytes[0] & 0x03) << 4) | ((aBytes[1] >> 4) & 0x0F)];
out += B64_CHARS[((aBytes[1] & 0x0F) << 2)];
out += "=";
break;
case 1:
out += B64_CHARS[(aBytes[0]>>2) & 0x3F];
out += B64_CHARS[(aBytes[0] & 0x03) << 4];
out += "==";
break;
}
return out;
}
function iconDataURIGenerator(aURISpec, aElement) {
var ios = Cc["@mozilla.org/network/io-service;1"].
getService(Ci.nsIIOService);
@@ -1061,11 +1027,13 @@ iconDataURIGenerator.prototype = {
requestFailed = !aRequest.requestSucceeded;
if (!requestFailed && this._countRead != 0) {
var str = String.fromCharCode.apply(null, this._bytes);
try {
var dataURI = ICON_DATAURL_PREFIX + b64(this._bytes);
var dataURI = ICON_DATAURL_PREFIX +
this._element.ownerDocument.defaultView.btoa(str);
this._element.setAttribute("image", dataURI);
}
catch(ex) { }
catch(ex) {}
}
this._channel = null;
this._element = null;

View File

@@ -275,33 +275,46 @@ function ENSURE_ARG(assertion, message) {
function b64(aBytes) {
const B64_CHARS =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var out = "", bits, i, j;
while (aBytes.length >= 3) {
bits = 0;
for (i = 0; i < 3; i++) {
bits <<= 8;
bits |= aBytes[i];
}
for (j = 18; j >= 0; j -= 6)
out += B64_CHARS[(bits>>j) & 0x3F];
var index = 0;
function get3Bytes() {
if (aBytes.length - index < 3)
return null; // Less than three bytes remaining
aBytes.splice(0, 3);
// Return the next three bytes in the array, and increment index for our
// next invocation
return aBytes.slice(index, index += 3);
}
switch (aBytes.length) {
var out = "";
var bytes = null;
while ((bytes = get3Bytes())) {
var bits = 0;
for (var i = 0; i < 3; i++) {
bits <<= 8;
bits |= bytes[i];
}
for (var j = 18; j >= 0; j -= 6)
out += B64_CHARS[(bits>>j) & 0x3F];
}
// Get the remaining bytes
bytes = aBytes.slice(index);
switch (bytes.length) {
case 2:
out += B64_CHARS[(aBytes[0]>>2) & 0x3F];
out += B64_CHARS[((aBytes[0] & 0x03) << 4) | ((aBytes[1] >> 4) & 0x0F)];
out += B64_CHARS[((aBytes[1] & 0x0F) << 2)];
out += "=";
out += B64_CHARS[(bytes[0]>>2) & 0x3F] +
B64_CHARS[((bytes[0] & 0x03) << 4) | ((bytes[1] >> 4) & 0x0F)] +
B64_CHARS[((bytes[1] & 0x0F) << 2)] +
"=";
break;
case 1:
out += B64_CHARS[(aBytes[0]>>2) & 0x3F];
out += B64_CHARS[(aBytes[0] & 0x03) << 4];
out += "==";
out += B64_CHARS[(bytes[0]>>2) & 0x3F] +
B64_CHARS[(bytes[0] & 0x03) << 4] +
"==";
break;
}
return out;
}