Bug 1499906 - Use toLocaleString(). r=erahm

This replaces a bunch of code that inserted separators by hand.

For now I've kept the output mostly the same by forcing the locale to en-US.
But at least now we could consider localizing the output.

The places where the output is different, it's more consistent with the new
code. E.g. printing "-05.55%" (which matches "05.55%") instead of "-5.55%".

--HG--
extra : rebase_source : 7c7161e5ab07e55e514020070bdfa4a774644a53
This commit is contained in:
Nicholas Nethercote 2018-10-19 11:19:32 +11:00
Родитель e97d37ffe2
Коммит 352b12fba0
3 изменённых файлов: 80 добавлений и 90 удалений

Просмотреть файл

@ -1127,7 +1127,7 @@ TreeNode.prototype = {
switch (this._units) {
case UNITS_BYTES: return formatBytes(this._amount);
case UNITS_COUNT:
case UNITS_COUNT_CUMULATIVE: return formatInt(this._amount);
case UNITS_COUNT_CUMULATIVE: return formatNum(this._amount);
case UNITS_PERCENTAGE: return formatPercentage(this._amount);
default:
throw "Invalid memory report(s): bad units in TreeNode.toString";
@ -1505,64 +1505,46 @@ function appendProcessAboutMemoryElements(aP, aN, aProcess, aTrees,
appendLink("end", "start", "↑");
}
/**
* Determines if a number has a negative sign when converted to a string.
* Works even for -0.
*
* @param aN
* The number.
* @return A boolean.
*/
function hasNegativeSign(aN) {
if (aN === 0) { // this succeeds for 0 and -0
return 1 / aN === -Infinity; // this succeeds for -0
}
return aN < 0;
}
// Used for UNITS_BYTES values that are printed as MiB.
const kMBStyle = {
minimumFractionDigits: 2,
maximumFractionDigits: 2
};
// Used for UNITS_PERCENTAGE values.
const kPercStyle = {
style: "percent",
minimumFractionDigits: 2,
maximumFractionDigits: 2
};
// Used for fractions within the tree.
const kFracStyle = {
style: "percent",
minimumIntegerDigits: 2,
minimumFractionDigits: 2,
maximumFractionDigits: 2
};
// Used for special-casing 100% fractions within the tree.
const kFrac1Style = {
style: "percent",
minimumIntegerDigits: 3,
minimumFractionDigits: 1,
maximumFractionDigits: 1
};
/**
* Formats an int as a human-readable string.
*
* @param aN
* The integer to format.
* @param aExtra
* An extra string to tack onto the end.
* @param aOptions
* Optional options object.
* @return A human-readable string representing the int.
*
* Note: building an array of chars and converting that to a string with
* Array.join at the end is more memory efficient than using string
* concatenation. See bug 722972 for details.
*/
function formatInt(aN, aExtra) {
let neg = false;
if (hasNegativeSign(aN)) {
neg = true;
aN = -aN;
}
let s = [];
while (true) {
let k = aN % 1000;
aN = Math.floor(aN / 1000);
if (aN > 0) {
if (k < 10) {
s.unshift(",00", k);
} else if (k < 100) {
s.unshift(",0", k);
} else {
s.unshift(",", k);
}
} else {
s.unshift(k);
break;
}
}
if (neg) {
s.unshift("-");
}
if (aExtra) {
s.push(aExtra);
}
return s.join("");
function formatNum(aN, aOptions) {
return aN.toLocaleString('en-US', aOptions);
}
/**
@ -1573,29 +1555,43 @@ function formatInt(aN, aExtra) {
* @return The string representation.
*/
function formatBytes(aBytes) {
let unit = gVerbose.checked ? " B" : " MB";
let s;
if (gVerbose.checked) {
s = formatInt(aBytes, unit);
} else {
let mbytes = (aBytes / (1024 * 1024)).toFixed(2);
let a = String(mbytes).split(".");
// If the argument to formatInt() is -0, it will print the negative sign.
s = formatInt(Number(a[0])) + "." + a[1] + unit;
}
return s;
return gVerbose.checked
? `${formatNum(aBytes)} B`
: `${formatNum(aBytes / (1024 * 1024), kMBStyle)} MB`;
}
/**
* Converts a percentage to an appropriate string representation.
* Converts a UNITS_PERCENTAGE value to an appropriate string representation.
*
* @param aPerc100x
* The percentage, multiplied by 100 (see nsIMemoryReporter).
* @return The string representation
*/
function formatPercentage(aPerc100x) {
return (aPerc100x / 100).toFixed(2) + "%";
// A percentage like 12.34% will have an aPerc100x value of 1234, and we need
// to divide that by 10,000 to get the 0.1234 that toLocaleString() wants.
return formatNum(aPerc100x / 10000, kPercStyle);
}
/*
* Converts a tree fraction to an appropriate string representation.
*
* @param aNum
* The numerator.
* @param aDenom
* The denominator.
* @return The string representation
*/
function formatTreeFrac(aNum, aDenom) {
// Two special behaviours here:
// - We treat 0 / 0 as 100%.
// - We want 4 digits, as much as possible, because it gives good vertical
// alignment. For positive numbers, 00.00%--99.99% works straighforwardly,
// but 100.0% needs special handling.
let num = aDenom === 0 ? 1 : (aNum / aDenom);
return (0.99995 <= num && num <= 1)
? formatNum(1, kFrac1Style)
: formatNum(num, kFracStyle);
}
/**
@ -1849,15 +1845,9 @@ function appendTreeElements(aP, aRoot, aProcess, aPadText) {
valueText);
// The percentage (omitted for single entries).
let percText;
if (!aT._isDegenerate) {
// Treat 0 / 0 as 100%.
let num = aRoot._amount === 0 ? 100 : (100 * aT._amount / aRoot._amount);
let numText = num.toFixed(2);
percText = numText === "100.00"
? " (100.0%)"
: (0 <= num && num < 10 ? " (0" : " (") + numText + "%)";
appendElementWithText(d, "span", "mrPerc", percText);
let percText = formatTreeFrac(aT._amount, aRoot._amount);
appendElementWithText(d, "span", "mrPerc", ` (${percText})`);
}
// The separator.

Просмотреть файл

@ -271,7 +271,7 @@ Explicit Allocations\n\
├──150.00 MB (153.06%) ── js/compartment(http://too-big.com/)/stuff [?!]\n\
├───5.00 MB (05.10%) ── ok\n\
└──-57.00 MB (-58.16%) -- (2 tiny) [?!]\n\
├───-2.00 MB (-2.04%) ── neg1 [?!]\n\
├───-2.00 MB (-02.04%) ── neg1 [?!]\n\
└──-55.00 MB (-56.12%) ── heap-unclassified [?!]\n\
\n\
Other Measurements\n\
@ -338,14 +338,14 @@ Explicit Allocations\n\
├──0.96 MB (00.96%) ── heap-unclassified\n\
├──0.01 MB (00.01%) -- a\n\
│ ├──0.04 MB (00.04%) ── pos\n\
│ ├──-0.01 MB (-0.01%) ── neg2 [?!]\n\
│ └──-0.02 MB (-0.02%) ── neg1 [?!]\n\
└──-0.02 MB (-0.02%) -- b/c [?!]\n\
│ ├──-0.01 MB (-00.01%) ── neg2 [?!]\n\
│ └──-0.02 MB (-00.02%) ── neg1 [?!]\n\
└──-0.02 MB (-00.02%) -- b/c [?!]\n\
├───0.01 MB (00.01%) ── g/h\n\
├───0.00 MB (00.00%) ── i/j\n\
└──-0.04 MB (-0.04%) -- d [?!]\n\
└──-0.04 MB (-00.04%) -- d [?!]\n\
├───0.02 MB (00.02%) ── e\n\
└──-0.06 MB (-0.06%) ── f [?!]\n\
└──-0.06 MB (-00.06%) ── f [?!]\n\
\n\
Other Measurements\n\
\n\
@ -441,7 +441,7 @@ Explicit Allocations\n\
102,760,448 B (100.0%) -- explicit\n\
├──157,286,400 B (153.06%) ── js/compartment(http://too-big.com/)/stuff [?!]\n\
├────5,242,880 B (05.10%) ── ok\n\
├───-2,097,152 B (-2.04%) ── neg1 [?!]\n\
├───-2,097,152 B (-02.04%) ── neg1 [?!]\n\
└──-57,671,680 B (-56.12%) ── heap-unclassified [?!]\n\
\n\
Other Measurements\n\
@ -507,14 +507,14 @@ Explicit Allocations\n\
├────1,007,616 B (00.96%) ── heap-unclassified\n\
├───────10,240 B (00.01%) -- a\n\
│ ├──40,960 B (00.04%) ── pos\n\
│ ├──-10,240 B (-0.01%) ── neg2 [?!]\n\
│ └──-20,480 B (-0.02%) ── neg1 [?!]\n\
└──────-25,600 B (-0.02%) -- b/c [?!]\n\
│ ├──-10,240 B (-00.01%) ── neg2 [?!]\n\
│ └──-20,480 B (-00.02%) ── neg1 [?!]\n\
└──────-25,600 B (-00.02%) -- b/c [?!]\n\
├───10,240 B (00.01%) ── g/h\n\
├────5,120 B (00.00%) ── i/j\n\
└──-40,960 B (-0.04%) -- d [?!]\n\
└──-40,960 B (-00.04%) -- d [?!]\n\
├───20,480 B (00.02%) ── e\n\
└──-61,440 B (-0.06%) ── f [?!]\n\
└──-61,440 B (-00.06%) ── f [?!]\n\
\n\
Other Measurements\n\
\n\

Просмотреть файл

@ -363,11 +363,11 @@ Other Measurements\n\
│ │ ├──-0.00 MB (18.18%) ── e [-]\n\
│ │ └──-0.00 MB (13.64%) ── d [-]\n\
│ ├──-0.00 MB (22.73%) ── f [-]\n\
│ └───0.00 MB (-4.55%) ── (fake child) [!]\n\
│ └───0.00 MB (-04.55%) ── (fake child) [!]\n\
└──-0.00 MB (50.00%) -- g\n\
├──-0.00 MB (31.82%) ── i [-]\n\
├──-0.00 MB (27.27%) ── h [-]\n\
└───0.00 MB (-9.09%) ── (fake child) [!]\n\
└───0.00 MB (-09.09%) ── (fake child) [!]\n\
\n\
End of P8\n\
";
@ -383,7 +383,7 @@ Explicit Allocations\n\
-10,005 B (100.0%) -- explicit\n\
├──-10,000 B (99.95%) ── storage/prefixset/goog-phish-shavar\n\
├───────-6 B (00.06%) ── spell-check [2]\n\
└────────1 B (-0.01%) ── xpcom/category-manager\n\
└────────1 B (-00.01%) ── xpcom/category-manager\n\
\n\
Other Measurements\n\
\n\
@ -461,11 +461,11 @@ Other Measurements\n\
│ │ ├──-4 B (18.18%) ── e [-]\n\
│ │ └──-3 B (13.64%) ── d [-]\n\
│ ├───-5 B (22.73%) ── f [-]\n\
│ └────1 B (-4.55%) ── (fake child) [!]\n\
│ └────1 B (-04.55%) ── (fake child) [!]\n\
└──-11 B (50.00%) -- g\n\
├───-7 B (31.82%) ── i [-]\n\
├───-6 B (27.27%) ── h [-]\n\
└────2 B (-9.09%) ── (fake child) [!]\n\
└────2 B (-09.09%) ── (fake child) [!]\n\
\n\
End of P8\n\
";