This is an automated email from the ASF dual-hosted git repository. chibenwa pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-mime4j.git
commit 9a80a7326b05448284c16df683f5ba0e148d4774 Author: Benoit TELLIER <[email protected]> AuthorDate: Thu Nov 19 06:26:26 2026 +0100 [PERF] RawFieldParser: copy a run at once and test stop chars with bit masks copyContent and copyUnquotedContent appended one char at a time to a builder of 16 chars and ran BitSet#get, isWhitespace and the '(' / '"' tests per byte. The run is now found first, with the delimiters, whitespace, '(' and '"' as two longs for ASCII (BitSet#get remains for a non-ASCII delimiter), then appended at once. CR and LF are still deleted and a run holding a non-ASCII byte is still decoded as UTF-8. The builders of parseValue, parseDomain and parseLocalPart are sized from the cursor. --- .../apache/james/mime4j/stream/RawFieldParser.java | 178 ++++++++++----------- .../mime4j/field/address/LenientAddressParser.java | 11 +- 2 files changed, 96 insertions(+), 93 deletions(-) diff --git a/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java b/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java index 2d0f689e..cd2e5655 100644 --- a/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java +++ b/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java @@ -230,7 +230,7 @@ public class RawFieldParser { private String parseValue(final ByteSequence buf, final ParserCursor cursor, final BitSet delimiters, final boolean boundary, final boolean unescape) { - StringBuilder dst = new StringBuilder(); + StringBuilder dst = new StringBuilder(initialCapacity(cursor)); boolean whitespace = false; while (!cursor.atEnd()) { char current = (char) (buf.byteAt(cursor.getPos()) & 0xff); @@ -367,54 +367,7 @@ public class RawFieldParser { */ public void copyContent(final ByteSequence buf, final ParserCursor cursor, final BitSet delimiters, final StringBuilder dst) { - int pos = cursor.getPos(); - int indexFrom = pos; - int indexTo = cursor.getUpperBound(); - int dstStart = dst.length(); - for (int i = indexFrom; i < indexTo; i++) { - byte bVal = buf.byteAt(i); - char current = (char) (bVal & 0xff); - if (isLineBreak(current)) { - // deleted, as by MimeUtil#unfold: the word goes on - pos++; - continue; - } - if ((delimiters != null && delimiters.get(current)) - || CharsetUtil.isWhitespace(current) || current == '(') { - break; - } - pos++; - if (bVal < 0) { - // Non-ASCII byte: undo the ASCII chars written so far, redo via UTF-8 decode - dst.setLength(dstStart); - ByteArrayBuffer dstRaw = DECODE_BUFFER.get(); - dstRaw.clear(); - for (int j = indexFrom; j <= i; j++) { - if (!isLineBreak((char) (buf.byteAt(j) & 0xff))) { - dstRaw.append(buf.byteAt(j)); - } - } - for (int k = i + 1; k < indexTo; k++) { - byte bk = buf.byteAt(k); - char ck = (char) (bk & 0xff); - if (isLineBreak(ck)) { - pos++; - continue; - } - if ((delimiters != null && delimiters.get(ck)) - || CharsetUtil.isWhitespace(ck) || ck == '(') { - break; - } - pos++; - dstRaw.append(bk); - } - cursor.updatePos(pos); - dst.append(ContentUtil.decode(StandardCharsets.UTF_8, dstRaw)); - return; - } - dst.append(current); - } - cursor.updatePos(pos); + copyRun(buf, cursor, delimiters, dst, false); } /** @@ -429,54 +382,90 @@ public class RawFieldParser { */ public void copyUnquotedContent(final ByteSequence buf, final ParserCursor cursor, final BitSet delimiters, final StringBuilder dst) { - int pos = cursor.getPos(); - int indexFrom = pos; - int indexTo = cursor.getUpperBound(); - int dstStart = dst.length(); - for (int i = indexFrom; i < indexTo; i++) { - byte bVal = buf.byteAt(i); - char current = (char) (bVal & 0xff); - if (isLineBreak(current)) { - // deleted, as by MimeUtil#unfold: the word goes on - pos++; - continue; + copyRun(buf, cursor, delimiters, dst, true); + } + + // Bits of the stop chars below 64 that are not delimiters: whitespace (CR and LF are deleted + // before this test) and '(' + private static final long WSP_OR_COMMENT = 1L << ' ' | 1L << '\t' | 1L << '('; + private static final long QUOTE = 1L << '\"'; + + /** + * Copies the run of bytes from the cursor to the first whitespace, '(', '"' when + * <code>stopAtQuote</code>, or delimiter. CR and LF are deleted, as by MimeUtil#unfold: the + * word goes on. A run holding a non-ASCII byte is decoded as UTF-8. + * <p> + * The run is found first, then appended at once: the stop test is two bit masks for ASCII. + */ + private void copyRun(final ByteSequence buf, final ParserCursor cursor, final BitSet delimiters, + final StringBuilder dst, final boolean stopAtQuote) { + long stopLow = WSP_OR_COMMENT | (stopAtQuote ? QUOTE : 0L); + long stopHigh = 0L; + boolean nonAsciiDelimiters = false; + if (delimiters != null) { + for (int d = delimiters.nextSetBit(0); d >= 0; d = delimiters.nextSetBit(d + 1)) { + if (d < 64) { + stopLow |= 1L << d; + } else if (d < 128) { + stopHigh |= 1L << (d - 64); + } else { + nonAsciiDelimiters = true; + break; + } } - if ((delimiters != null && delimiters.get(current)) - || CharsetUtil.isWhitespace(current) || current == '(' || current == '\"') { - break; + } + final int indexFrom = cursor.getPos(); + final int indexTo = cursor.getUpperBound(); + boolean nonAscii = false; + boolean lineBreak = false; + int end = indexFrom; + for (; end < indexTo; end++) { + int current = buf.byteAt(end) & 0xff; + if (current == '\r' || current == '\n') { + lineBreak = true; + continue; } - pos++; - if (bVal < 0) { - // Non-ASCII byte: undo the ASCII chars written so far, redo via UTF-8 decode - dst.setLength(dstStart); - ByteArrayBuffer dstRaw = DECODE_BUFFER.get(); - dstRaw.clear(); - for (int j = indexFrom; j <= i; j++) { - if (!isLineBreak((char) (buf.byteAt(j) & 0xff))) { - dstRaw.append(buf.byteAt(j)); - } + if (current < 64) { + if ((stopLow >>> current & 1L) != 0) { + break; } - for (int k = i + 1; k < indexTo; k++) { - byte bk = buf.byteAt(k); - char ck = (char) (bk & 0xff); - if (isLineBreak(ck)) { - pos++; - continue; - } - if ((delimiters != null && delimiters.get(ck)) - || CharsetUtil.isWhitespace(ck) || ck == '(' || ck == '\"') { - break; - } - pos++; - dstRaw.append(bk); + } else if (current < 128) { + if ((stopHigh >>> (current - 64) & 1L) != 0) { + break; + } + } else { + if (nonAsciiDelimiters && delimiters.get(current)) { + break; } - cursor.updatePos(pos); - dst.append(ContentUtil.decode(StandardCharsets.UTF_8, dstRaw)); - return; + nonAscii = true; + } + } + cursor.updatePos(end); + if (nonAscii) { + ByteArrayBuffer dstRaw = DECODE_BUFFER.get(); + dstRaw.clear(); + for (int j = indexFrom; j < end; j++) { + byte bj = buf.byteAt(j); + if (bj != '\r' && bj != '\n') { + dstRaw.append(bj); + } + } + dst.append(ContentUtil.decode(StandardCharsets.UTF_8, dstRaw)); + return; + } + dst.ensureCapacity(dst.length() + (end - indexFrom)); + if (!lineBreak) { + for (int j = indexFrom; j < end; j++) { + dst.append((char) (buf.byteAt(j) & 0xff)); + } + return; + } + for (int j = indexFrom; j < end; j++) { + char c = (char) (buf.byteAt(j) & 0xff); + if (!isLineBreak(c)) { + dst.append(c); } - dst.append(current); } - cursor.updatePos(pos); } /** @@ -506,6 +495,13 @@ public class RawFieldParser { copyQuotedContent(buf, cursor, dst, !decodeEncodedWords, false); } + /** + * The capacity of a builder for what is left under the cursor, capped: most values are short. + */ + private static int initialCapacity(final ParserCursor cursor) { + return Math.max(16, Math.min(64, cursor.getUpperBound() - cursor.getPos())); + } + private static boolean isLineBreak(final char c) { return c == '\r' || c == '\n'; } diff --git a/dom/src/main/java/org/apache/james/mime4j/field/address/LenientAddressParser.java b/dom/src/main/java/org/apache/james/mime4j/field/address/LenientAddressParser.java index c5fdff85..14c5b71e 100644 --- a/dom/src/main/java/org/apache/james/mime4j/field/address/LenientAddressParser.java +++ b/dom/src/main/java/org/apache/james/mime4j/field/address/LenientAddressParser.java @@ -89,7 +89,7 @@ public class LenientAddressParser implements AddressParser { } String parseDomain(final ByteSequence buf, final ParserCursor cursor, final BitSet delimiters) { - StringBuilder dst = new StringBuilder(); + StringBuilder dst = new StringBuilder(initialCapacity(cursor)); // Every '[' before this index has no ']' before the next delimiter: a failed scan for // the closing bracket is not repeated for each of them, which would be quadratic. int unclosedUntil = -1; @@ -280,7 +280,7 @@ public class LenientAddressParser implements AddressParser { } private String parseLocalPart(final ByteSequence buf, final ParserCursor cursor, final BitSet delimiters) { - StringBuilder dst = new StringBuilder(); + StringBuilder dst = new StringBuilder(initialCapacity(cursor)); boolean whitespace = false; while (!cursor.atEnd()) { char current = (char) (buf.byteAt(cursor.getPos()) & 0xff); @@ -492,4 +492,11 @@ public class LenientAddressParser implements AddressParser { return parseAddressList(raw, cursor); } + /** + * The capacity of a builder for what is left under the cursor, capped: a domain or a local part is short. + */ + private static int initialCapacity(final ParserCursor cursor) { + return Math.max(16, Math.min(64, cursor.getUpperBound() - cursor.getPos())); + } + } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
