Skip to content

Commit 1297623

Browse files
committed
Mozilla bug 1807017 - Consider to reuse the memory when nsHtml5Tokenizer::end() is called, r=hsivonen
Differential Revision: https://phabricator.services.mozilla.com/D201982
1 parent 12dc2b7 commit 1297623

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,8 @@ public class Tokenizer implements Locator, Locator2 {
511511

512512
private boolean shouldSuspend;
513513

514+
private boolean keepBuffer;
515+
514516
protected boolean confident;
515517

516518
private int line;
@@ -570,6 +572,7 @@ public Tokenizer(TokenHandler tokenHandler, boolean newAttributesEachTime) {
570572
this.systemIdentifier = null;
571573
this.attributes = null;
572574
this.shouldSuspend = false;
575+
this.keepBuffer = false;
573576
this.confident = false;
574577
this.line = 0;
575578
// CPPONLY: this.attributeLine = 0;
@@ -632,6 +635,7 @@ public Tokenizer(TokenHandler tokenHandler
632635
// CPPONLY: this.attributes = tokenHandler.HasBuilder() ? new HtmlAttributes(mappingLangToXmlLang) : null;
633636
// CPPONLY: this.newAttributesEachTime = !tokenHandler.HasBuilder();
634637
this.shouldSuspend = false;
638+
this.keepBuffer = false;
635639
this.confident = false;
636640
this.line = 0;
637641
// CPPONLY: this.attributeLine = 0;
@@ -653,6 +657,18 @@ public void initLocation(String newPublicId, String newSystemId) {
653657
// CPPONLY: return viewingXmlSource;
654658
// CPPONLY: }
655659

660+
public void setKeepBuffer(boolean keepBuffer) {
661+
this.keepBuffer = keepBuffer;
662+
}
663+
664+
public boolean dropBufferIfLongerThan(int length) {
665+
if (strBuf.length > length) {
666+
strBuf = null;
667+
return true;
668+
}
669+
return false;
670+
}
671+
656672
// [NOCPP[
657673

658674
/**
@@ -7225,7 +7241,9 @@ private void emitOrAppendOne(@Const @NoLength char[] val, int returnState)
72257241
}
72267242

72277243
public void end() throws SAXException {
7228-
strBuf = null;
7244+
if (!keepBuffer) {
7245+
strBuf = null;
7246+
}
72297247
doctypeName = null;
72307248
if (systemIdentifier != null) {
72317249
Portability.releaseString(systemIdentifier);
@@ -7415,7 +7433,9 @@ public void loadState(Tokenizer other) throws SAXException {
74157433

74167434
public void initializeWithoutStarting() throws SAXException {
74177435
confident = false;
7418-
strBuf = null;
7436+
if (!keepBuffer) {
7437+
strBuf = null;
7438+
}
74197439
line = 1;
74207440
// CPPONLY: attributeLine = 1;
74217441
// [NOCPP[

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ public abstract class TreeBuilder<T> implements TokenHandler,
436436

437437
private boolean allowDeclarativeShadowRoots = false;
438438

439+
private boolean keepBuffer = false;
440+
439441
// [NOCPP[
440442

441443
private boolean reportingDoctype = true;
@@ -577,6 +579,18 @@ final void warn(String message, Locator locator) throws SAXException {
577579

578580
// ]NOCPP]
579581

582+
public void setKeepBuffer(boolean keepBuffer) {
583+
this.keepBuffer = keepBuffer;
584+
}
585+
586+
public boolean dropBufferIfLongerThan(int length) {
587+
if (charBuffer.length > length) {
588+
charBuffer = null;
589+
return true;
590+
}
591+
return false;
592+
}
593+
580594
@SuppressWarnings("unchecked") public final void startTokenization(Tokenizer self) throws SAXException {
581595
tokenizer = self;
582596
stackNodes = new StackNode[64];
@@ -598,7 +612,9 @@ final void warn(String message, Locator locator) throws SAXException {
598612
// ]NOCPP]
599613
start(fragment);
600614
charBufferLen = 0;
601-
charBuffer = null;
615+
if (!keepBuffer) {
616+
charBuffer = null;
617+
}
602618
framesetOk = true;
603619
if (fragment) {
604620
T elt;
@@ -1451,7 +1467,10 @@ public final void endTokenization() throws SAXException {
14511467
// [NOCPP[
14521468
idLocations.clear();
14531469
// ]NOCPP]
1454-
charBuffer = null;
1470+
1471+
if (!keepBuffer) {
1472+
charBuffer = null;
1473+
}
14551474
end();
14561475
}
14571476

0 commit comments

Comments
 (0)