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-jdkim.git
commit e5dd9c50635e28bd0d7a4242056d33a5e634af05 Author: Benoit TELLIER <[email protected]> AuthorDate: Thu Nov 19 06:04:59 2026 +0100 [PERF] Body canonicalizers: hand the digest blocks of canonical octets SimpleBodyCanonicalizer wrote every CRLF, every octet after a CR and every octet after a WSP run with write(int), down to a one-octet MessageDigest#update, and allocated "\r\n".getBytes() for each line end. With relaxed canonicalization each space between words took that path too, so the digest itself was a small part of the body hash. SimpleBodyCanonicalizer now collects its output in a 4 KB array that it writes on flush, on close or when full; the CRLFs go into it with no allocation. RelaxedBodyCanonicalizer copies a single SP followed by an ordinary octet with the rest of the run, since it is already canonical. The canonical octets are unchanged: a test compares random block writes with single-byte writes. --- .../jdkim/canon/RelaxedBodyCanonicalizer.java | 18 ++++- .../james/jdkim/canon/SimpleBodyCanonicalizer.java | 51 ++++++++++++-- .../jdkim/canon/BodyCanonicalizerBlocksTest.java | 81 ++++++++++++++++++++++ 3 files changed, 143 insertions(+), 7 deletions(-) diff --git a/main/src/main/java/org/apache/james/jdkim/canon/RelaxedBodyCanonicalizer.java b/main/src/main/java/org/apache/james/jdkim/canon/RelaxedBodyCanonicalizer.java index 2031d6b..88b702b 100644 --- a/main/src/main/java/org/apache/james/jdkim/canon/RelaxedBodyCanonicalizer.java +++ b/main/src/main/java/org/apache/james/jdkim/canon/RelaxedBodyCanonicalizer.java @@ -47,10 +47,18 @@ public class RelaxedBodyCanonicalizer extends FilterOutputStream { k++; continue; } - // nothing is pending: copy everything up to the next WSP or CR as is + // nothing is pending: copy everything up to the next WSP or CR as is. A single SP followed by + // an octet that is neither WSP nor CR is canonical already, so it is copied with the rest. int next = k; - while (next < end && buffer[next] != ' ' && buffer[next] != '\t' && buffer[next] != '\r') { - next++; + while (next < end) { + byte c = buffer[next]; + if (c == ' ' && next + 1 < end && isOrdinary(buffer[next + 1])) { + next += 2; + } else if (isOrdinary(c)) { + next++; + } else { + break; + } } if (next == k) { write(buffer[k]); @@ -62,6 +70,10 @@ public class RelaxedBodyCanonicalizer extends FilterOutputStream { } } + private static boolean isOrdinary(byte b) { + return b != ' ' && b != '\t' && b != '\r'; + } + public void write(int b) throws IOException { // OutputStream#write(int): the 24 high-order bits are ignored b &= 0xFF; diff --git a/main/src/main/java/org/apache/james/jdkim/canon/SimpleBodyCanonicalizer.java b/main/src/main/java/org/apache/james/jdkim/canon/SimpleBodyCanonicalizer.java index d2271e8..57739b3 100644 --- a/main/src/main/java/org/apache/james/jdkim/canon/SimpleBodyCanonicalizer.java +++ b/main/src/main/java/org/apache/james/jdkim/canon/SimpleBodyCanonicalizer.java @@ -37,6 +37,9 @@ public class SimpleBodyCanonicalizer extends FilterOutputStream { // CRLFs read but not written yet: only the trailing ones are dropped private int countCRLF; private boolean hasContent; + // canonical octets not written yet: the digest gets them in blocks rather than one or two at a time + private final byte[] pending = new byte[4096]; + private int count; public SimpleBodyCanonicalizer(OutputStream arg0) { this(arg0, false); @@ -69,7 +72,7 @@ public class SimpleBodyCanonicalizer extends FilterOutputStream { while (next < end && b[next] != '\r') { next++; } - out.write(b, i, next - i); + emit(b, i, next - i); hasContent = true; i = next; } @@ -98,9 +101,15 @@ public class SimpleBodyCanonicalizer extends FilterOutputStream { public void close() throws IOException { complete(); + // FilterOutputStream#close flushes super.close(); } + public void flush() throws IOException { + flushPending(); + out.flush(); + } + private void complete() throws IOException { if (DEEP_DEBUG) System.out.println("C:(" + lastWasCR + "|" + countCRLF + ")"); @@ -111,12 +120,12 @@ public class SimpleBodyCanonicalizer extends FilterOutputStream { if (emptyBodyIsEmpty && !hasContent) { return; } - out.write("\r\n".getBytes()); + emitCRLF(); } private void writeContent(int b) throws IOException { dumpCRLF(); - out.write(b); + emit(b); hasContent = true; } @@ -124,9 +133,43 @@ public class SimpleBodyCanonicalizer extends FilterOutputStream { if (DEEP_DEBUG) System.out.println("D:(" + lastWasCR + "|" + countCRLF + ")"); while (countCRLF > 0) { - out.write("\r\n".getBytes()); + emitCRLF(); countCRLF--; } } + private void emitCRLF() throws IOException { + if (count + 2 > pending.length) { + flushPending(); + } + pending[count++] = '\r'; + pending[count++] = '\n'; + } + + private void emit(int b) throws IOException { + if (count == pending.length) { + flushPending(); + } + pending[count++] = (byte) b; + } + + private void emit(byte[] b, int off, int len) throws IOException { + if (len > pending.length - count) { + flushPending(); + if (len >= pending.length) { + out.write(b, off, len); + return; + } + } + System.arraycopy(b, off, pending, count, len); + count += len; + } + + private void flushPending() throws IOException { + if (count > 0) { + out.write(pending, 0, count); + count = 0; + } + } + } \ No newline at end of file diff --git a/main/src/test/java/org/apache/james/jdkim/canon/BodyCanonicalizerBlocksTest.java b/main/src/test/java/org/apache/james/jdkim/canon/BodyCanonicalizerBlocksTest.java new file mode 100644 index 0000000..5f06118 --- /dev/null +++ b/main/src/test/java/org/apache/james/jdkim/canon/BodyCanonicalizerBlocksTest.java @@ -0,0 +1,81 @@ +/****************************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ******************************************************************************/ + +package org.apache.james.jdkim.canon; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Arrays; +import java.util.Random; +import java.util.function.Function; + +import org.junit.Test; + +public class BodyCanonicalizerBlocksTest { + + private static final byte[] ALPHABET = {' ', '\t', '\r', '\n', 'a', 'b'}; + + private static byte[] canonicalize(Function<OutputStream, OutputStream> canonicalizer, byte[] data, + Random random) throws IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + OutputStream os = canonicalizer.apply(bos); + int i = 0; + while (i < data.length) { + if (random == null) { + os.write(data[i++]); + continue; + } + int len = Math.min(data.length - i, random.nextInt(9)); + if (len == 1) { + os.write(data[i]); + } else { + os.write(data, i, len); + } + i += len; + } + os.close(); + return bos.toByteArray(); + } + + private static void assertBlocksGiveTheSameOctets(Function<OutputStream, OutputStream> canonicalizer) + throws IOException { + Random random = new Random(6376); + for (int run = 0; run < 50000; run++) { + byte[] data = new byte[run % 1000 == 0 ? 10000 : random.nextInt(30)]; + for (int i = 0; i < data.length; i++) { + data[i] = ALPHABET[random.nextInt(ALPHABET.length)]; + } + assertThat(canonicalize(canonicalizer, data, random)).as("input %s", Arrays.toString(data)) + .isEqualTo(canonicalize(canonicalizer, data, null)); + } + } + + @Test + public void relaxedBlocksShouldGiveTheOctetsOfSingleBytes() throws IOException { + assertBlocksGiveTheSameOctets(RelaxedBodyCanonicalizer::new); + } + + @Test + public void simpleBlocksShouldGiveTheOctetsOfSingleBytes() throws IOException { + assertBlocksGiveTheSameOctets(SimpleBodyCanonicalizer::new); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
