Skip to content

Commit

Permalink
Mozilla bug 1807017 - Consider to reuse the memory when nsHtml5Tokeni…
Browse files Browse the repository at this point in the history
…zer::end() is called, r=hsivonen

Differential Revision: https://phabricator.services.mozilla.com/D201982
  • Loading branch information
hsivonen committed Oct 30, 2024
1 parent 12dc2b7 commit 1297623
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
24 changes: 22 additions & 2 deletions src/nu/validator/htmlparser/impl/Tokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,8 @@ public class Tokenizer implements Locator, Locator2 {

private boolean shouldSuspend;

private boolean keepBuffer;

protected boolean confident;

private int line;
Expand Down Expand Up @@ -570,6 +572,7 @@ public Tokenizer(TokenHandler tokenHandler, boolean newAttributesEachTime) {
this.systemIdentifier = null;
this.attributes = null;
this.shouldSuspend = false;
this.keepBuffer = false;
this.confident = false;
this.line = 0;
// CPPONLY: this.attributeLine = 0;
Expand Down Expand Up @@ -632,6 +635,7 @@ public Tokenizer(TokenHandler tokenHandler
// CPPONLY: this.attributes = tokenHandler.HasBuilder() ? new HtmlAttributes(mappingLangToXmlLang) : null;
// CPPONLY: this.newAttributesEachTime = !tokenHandler.HasBuilder();
this.shouldSuspend = false;
this.keepBuffer = false;
this.confident = false;
this.line = 0;
// CPPONLY: this.attributeLine = 0;
Expand All @@ -653,6 +657,18 @@ public void initLocation(String newPublicId, String newSystemId) {
// CPPONLY: return viewingXmlSource;
// CPPONLY: }

public void setKeepBuffer(boolean keepBuffer) {
this.keepBuffer = keepBuffer;
}

public boolean dropBufferIfLongerThan(int length) {
if (strBuf.length > length) {
strBuf = null;
return true;
}
return false;
}

// [NOCPP[

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

public void end() throws SAXException {
strBuf = null;
if (!keepBuffer) {
strBuf = null;
}
doctypeName = null;
if (systemIdentifier != null) {
Portability.releaseString(systemIdentifier);
Expand Down Expand Up @@ -7415,7 +7433,9 @@ public void loadState(Tokenizer other) throws SAXException {

public void initializeWithoutStarting() throws SAXException {
confident = false;
strBuf = null;
if (!keepBuffer) {
strBuf = null;
}
line = 1;
// CPPONLY: attributeLine = 1;
// [NOCPP[
Expand Down
23 changes: 21 additions & 2 deletions src/nu/validator/htmlparser/impl/TreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ public abstract class TreeBuilder<T> implements TokenHandler,

private boolean allowDeclarativeShadowRoots = false;

private boolean keepBuffer = false;

// [NOCPP[

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

// ]NOCPP]

public void setKeepBuffer(boolean keepBuffer) {
this.keepBuffer = keepBuffer;
}

public boolean dropBufferIfLongerThan(int length) {
if (charBuffer.length > length) {
charBuffer = null;
return true;
}
return false;
}

@SuppressWarnings("unchecked") public final void startTokenization(Tokenizer self) throws SAXException {
tokenizer = self;
stackNodes = new StackNode[64];
Expand All @@ -598,7 +612,9 @@ final void warn(String message, Locator locator) throws SAXException {
// ]NOCPP]
start(fragment);
charBufferLen = 0;
charBuffer = null;
if (!keepBuffer) {
charBuffer = null;
}
framesetOk = true;
if (fragment) {
T elt;
Expand Down Expand Up @@ -1451,7 +1467,10 @@ public final void endTokenization() throws SAXException {
// [NOCPP[
idLocations.clear();
// ]NOCPP]
charBuffer = null;

if (!keepBuffer) {
charBuffer = null;
}
end();
}

Expand Down

0 comments on commit 1297623

Please sign in to comment.