Skip to content

Commit a96e7d8

Browse files
committed
Mozilla bug 1998923 - Make better inline and never-inline decisions around EnsureBufferSpace in the tokenizer. r=farre
Differential Revision: https://phabricator.services.mozilla.com/D271752
1 parent 797f482 commit a96e7d8

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

src/nu/validator/htmlparser/impl/Portability.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
public final class Portability {
3333

34+
// [NOCPP[
3435
public static int checkedAdd(int a, int b) throws SAXException {
3536
// This can't be translated code, because in C++ signed integer overflow is UB, so the below code would be wrong.
3637
assert a >= 0;
@@ -41,6 +42,7 @@ public static int checkedAdd(int a, int b) throws SAXException {
4142
}
4243
return sum;
4344
}
45+
// ]NOCPP]
4446

4547
// Allocating methods
4648

src/nu/validator/htmlparser/impl/Tokenizer.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -982,12 +982,9 @@ private void emitOrAppendCharRefBuf(int returnState) throws SAXException {
982982
* @param c
983983
* the UTF-16 code unit to append
984984
*/
985-
private void appendStrBuf(char c) {
986-
// CPPONLY: assert strBufLen < strBuf.length: "Previous buffer length insufficient.";
985+
@Inline private void appendStrBuf(char c) {
987986
// CPPONLY: if (strBufLen == strBuf.length) {
988-
// CPPONLY: if (!EnsureBufferSpace(1)) {
989-
// CPPONLY: assert false: "RELEASE: Unable to recover from buffer reallocation failure";
990-
// CPPONLY: } // TODO: Add telemetry when outer if fires but inner does not
987+
// CPPONLY: EnsureBufferSpaceShouldNeverHappen(1);
991988
// CPPONLY: }
992989
strBuf[strBufLen++] = c;
993990
}
@@ -1094,13 +1091,12 @@ private void maybeAppendSpaceToBogusComment() throws SAXException {
10941091
// ]NOCPP]
10951092
}
10961093

1097-
private void appendStrBuf(@NoLength char[] buffer, int offset, int length) throws SAXException {
1098-
int newLen = Portability.checkedAdd(strBufLen, length);
1099-
// CPPONLY: assert newLen <= strBuf.length: "Previous buffer length insufficient.";
1094+
@Inline private void appendStrBuf(@NoLength char[] buffer, int offset, int length) throws SAXException {
1095+
// Years of crash stats have shown that the this addition doesn't overflow, as it logically
1096+
// shouldn't.
1097+
int newLen = strBufLen + length;
11001098
// CPPONLY: if (strBuf.length < newLen) {
1101-
// CPPONLY: if (!EnsureBufferSpace(length)) {
1102-
// CPPONLY: assert false: "RELEASE: Unable to recover from buffer reallocation failure";
1103-
// CPPONLY: } // TODO: Add telemetry when outer if fires but inner does not
1099+
// CPPONLY: EnsureBufferSpaceShouldNeverHappen(length);
11041100
// CPPONLY: }
11051101
System.arraycopy(buffer, offset, strBuf, strBufLen, length);
11061102
strBufLen = newLen;

0 commit comments

Comments
 (0)