From 436b6f9153d06fa88ee1f1ce419de432465f2d7c Mon Sep 17 00:00:00 2001 From: "Gerlach, Winfried" Date: Tue, 29 Sep 2026 16:51:13 +0200 Subject: [PATCH 1/3] #51 Add fast path for decoding BigInteger values of up to 18 digits --- release-notes/VERSION | 5 ++ .../stax2/ri/typed/ValueDecoderFactory.java | 73 ++++++++++++++++++ .../stax2/ri/typed/BigIntegerDecoderTest.java | 76 +++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 src/test/java/org/codehaus/stax2/ri/typed/BigIntegerDecoderTest.java diff --git a/release-notes/VERSION b/release-notes/VERSION index 15441a6..4a8a9e8 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION @@ -4,6 +4,11 @@ Project: Stax2 API = Releases ------------------------------------------------------------------------ +(not yet released) + +#51 Fast path for BigInteger decoding + (contributed by @winfriedgerlach) + 4.3.1 (27-Sep-2026) #41: Fix `ValueEncoderFactory.getScalarEncoder()` fails for long values diff --git a/src/main/java/org/codehaus/stax2/ri/typed/ValueDecoderFactory.java b/src/main/java/org/codehaus/stax2/ri/typed/ValueDecoderFactory.java index 7054ae7..46b8a39 100644 --- a/src/main/java/org/codehaus/stax2/ri/typed/ValueDecoderFactory.java +++ b/src/main/java/org/codehaus/stax2/ri/typed/ValueDecoderFactory.java @@ -1016,6 +1016,11 @@ public void decode(char[] lexical, int start, int end) public static final class IntegerDecoder extends DecoderBase { + /** + * Max. number of digits for which values are parsed as a {@code long} first (any 18-digit number fits in one) + */ + private static final int MAX_LONG_DIGITS = 18; + protected BigInteger mValue; public IntegerDecoder() { } @@ -1028,6 +1033,9 @@ public IntegerDecoder() { } @Override public void decode(String lexical) throws IllegalArgumentException { + if (decodeAsLong(lexical)) { + return; + } try { mValue = new BigInteger(lexical); } catch (NumberFormatException nex) { @@ -1038,6 +1046,9 @@ public void decode(String lexical) throws IllegalArgumentException @Override public void decode(char[] lexical, int start, int end) throws IllegalArgumentException { + if (decodeAsLong(lexical, start, end)) { + return; + } String lexicalStr = new String(lexical, start, (end-start)); try { mValue = new BigInteger(lexicalStr); @@ -1045,6 +1056,68 @@ public void decode(char[] lexical, int start, int end) throws IllegalArgumentExc throw constructInvalidValue(lexicalStr); } } + + /** + * Fast path for the common case of an optional sign followed by at + * most {@link #MAX_LONG_DIGITS} (ASCII) digits: such values fit in + * a {@code long}, avoiding construction of a String and the general + * {@link BigInteger} parser. Anything else is left to + * {@link BigInteger}, including reporting of invalid values. + * + * @return True if the value was decoded; false if not handled + */ + private boolean decodeAsLong(String lexical) + { + final int end = lexical.length(); + int ptr = 0; + boolean neg = false; + if (end > 0) { + char ch = lexical.charAt(0); + if (ch == '-' || ch == '+') { + neg = (ch == '-'); + ++ptr; + } + } + if (ptr == end || (end - ptr) > MAX_LONG_DIGITS) { + return false; + } + long value = 0L; + for (; ptr < end; ++ptr) { + int digit = lexical.charAt(ptr) - '0'; + if (digit < 0 || digit > 9) { + return false; + } + value = (value * 10) + digit; + } + mValue = BigInteger.valueOf(neg ? -value : value); + return true; + } + + private boolean decodeAsLong(char[] lexical, int start, final int end) + { + int ptr = start; + boolean neg = false; + if (ptr < end) { + char ch = lexical[ptr]; + if (ch == '-' || ch == '+') { + neg = (ch == '-'); + ++ptr; + } + } + if (ptr == end || (end - ptr) > MAX_LONG_DIGITS) { + return false; + } + long value = 0L; + for (; ptr < end; ++ptr) { + int digit = lexical[ptr] - '0'; + if (digit < 0 || digit > 9) { + return false; + } + value = (value * 10) + digit; + } + mValue = BigInteger.valueOf(neg ? -value : value); + return true; + } } public static final class DecimalDecoder diff --git a/src/test/java/org/codehaus/stax2/ri/typed/BigIntegerDecoderTest.java b/src/test/java/org/codehaus/stax2/ri/typed/BigIntegerDecoderTest.java new file mode 100644 index 0000000..1972d92 --- /dev/null +++ b/src/test/java/org/codehaus/stax2/ri/typed/BigIntegerDecoderTest.java @@ -0,0 +1,76 @@ +package org.codehaus.stax2.ri.typed; + +import java.math.BigInteger; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Tests for {@link ValueDecoderFactory.IntegerDecoder}: values with up to 18 digits take a fast path, which must give + * the same results as {@link BigInteger#BigInteger(String)}, like all other values. + */ +class BigIntegerDecoderTest +{ + + private static final String[] VALID = { + "0", "-0", "+0", "5", "+5", "-5", "007", "-007", "42", + "2147483648", "-2147483649", "9223372036854775807", + // longest values for fast path: 18 digits + "999999999999999999", "-999999999999999999", "+100000000000000000", + // 19 digits and more: BigInteger only + "1000000000000000000", "-9223372036854775808", "9999999999999999999", + "123456789012345678901234567890", "-000000000000000000000000000001", + // non-ASCII digits (accepted by BigInteger): U+0663 ARABIC-INDIC DIGIT THREE + "\u0663", "1\u0663" + }; + + private static final String[] INVALID = { + "", "-", "+", "--1", "+-1", "1a", "a1", "1.5", "1e3", "1 2", " 1", "1 ", "0x10" + }; + + private final ValueDecoderFactory.IntegerDecoder decoder = new ValueDecoderFactory().getIntegerDecoder(); + + @Test + void decodeValidString() + { + for (String value : VALID) { + decoder.decode(value); + assertEquals(new BigInteger(value), decoder.getValue(), value); + } + } + + @Test + void decodeValidCharArray() + { + for (String value : VALID) { + // Decoded range is not at the start or end of the array + char[] buf = ("ab" + value + "cd").toCharArray(); + decoder.decode(buf, 2, buf.length - 2); + assertEquals(new BigInteger(value), decoder.getValue(), value); + } + } + + @Test + void decodeInvalidString() + { + for (String value : INVALID) { + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, + () -> decoder.decode(value), value); + assertTrue(e.getMessage().contains("not a valid lexical representation of integer"), e.getMessage()); + } + } + + @Test + void decodeInvalidCharArray() + { + for (String value : INVALID) { + char[] buf = ("ab" + value + "cd").toCharArray(); + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, + () -> decoder.decode(buf, 2, buf.length - 2), value); + assertTrue(e.getMessage().contains("not a valid lexical representation of integer"), e.getMessage()); + } + } +} From 32f62e1425e35ba1e7c808ad6b6635d2e75a4cc9 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Thu, 8 Oct 2026 18:24:08 -0700 Subject: [PATCH 2/3] Clean up --- release-notes/VERSION | 2 +- .../stax2/ri/typed/ValueDecoderFactory.java | 181 +++++++++++------- .../stax2/ri/typed/BigIntegerDecoderTest.java | 95 ++++++--- 3 files changed, 185 insertions(+), 93 deletions(-) diff --git a/release-notes/VERSION b/release-notes/VERSION index 5cd65f1..1e1f5c2 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION @@ -6,7 +6,7 @@ Project: Stax2 API 4.3.2 (not yet released) -#51 Fast path for BigInteger decoding +#51: Fast path for BigInteger decoding (contributed by @winfriedgerlach) 4.3.1 (27-Sep-2026) diff --git a/src/main/java/org/codehaus/stax2/ri/typed/ValueDecoderFactory.java b/src/main/java/org/codehaus/stax2/ri/typed/ValueDecoderFactory.java index 46b8a39..7003791 100644 --- a/src/main/java/org/codehaus/stax2/ri/typed/ValueDecoderFactory.java +++ b/src/main/java/org/codehaus/stax2/ri/typed/ValueDecoderFactory.java @@ -186,6 +186,22 @@ public abstract static class DecoderBase static final BigInteger BD_MIN_LONG = BigInteger.valueOf(Long.MIN_VALUE); static final BigInteger BD_MAX_LONG = BigInteger.valueOf(Long.MAX_VALUE); + + /** + * Value returned by {@code tryParseLong} for values it does not handle + * (it is never returned for handled values, as {@link Long#MIN_VALUE} + * itself is not handled) + */ + protected static final long NOT_A_LONG = Long.MIN_VALUE; + + /** + * Max. number of digits (after leading zeroes) {@code tryParseLong} + * handles: same as for {@link Long#MAX_VALUE} + */ + static final int MAX_LONG_DIGITS = 19; + + static final long MAX_LONG_DIV_10 = Long.MAX_VALUE / 10; + static final int MAX_LONG_LAST_DIGIT = (int) (Long.MAX_VALUE % 10); /** * Pointer to the next character to check, within lexical value @@ -452,6 +468,94 @@ protected static final long parseLong(String digitChars, int start, int end) return val + parseInt(digitChars, start2, end); } + /** + * Non-throwing variant of {@code long} parsing, for decoders that + * have a more general (and slower) fallback: handles an optional sign + * followed by ASCII digits (leading zeroes are skipped), for any value + * that fits in a {@code long} except for {@link Long#MIN_VALUE}. + * Anything else, including invalid values, is left to the caller. + * + * @return Parsed value, or {@link #NOT_A_LONG} if value was not handled + */ + protected static final long tryParseLong(String lexical, int ptr, final int end) + { + if (ptr >= end) { + return NOT_A_LONG; + } + char ch = lexical.charAt(ptr); + final boolean neg = (ch == '-'); + if (neg || ch == '+') { + if (++ptr == end) { + return NOT_A_LONG; + } + } + // Skip leading zeroes (but not the last digit, for value zero) + final int last = end-1; + while (ptr < last && lexical.charAt(ptr) == '0') { + ++ptr; + } + if ((end - ptr) > MAX_LONG_DIGITS) { + return NOT_A_LONG; + } + long value = 0L; + for (; ptr < last; ++ptr) { + int digit = lexical.charAt(ptr) - '0'; + if (digit < 0 || digit > 9) { + return NOT_A_LONG; + } + value = (value * 10) + digit; + } + return lastLongDigit(value, lexical.charAt(last) - '0', neg); + } + + protected static final long tryParseLong(char[] lexical, int ptr, final int end) + { + if (ptr >= end) { + return NOT_A_LONG; + } + char ch = lexical[ptr]; + final boolean neg = (ch == '-'); + if (neg || ch == '+') { + if (++ptr == end) { + return NOT_A_LONG; + } + } + // Skip leading zeroes (but not the last digit, for value zero) + final int last = end-1; + while (ptr < last && lexical[ptr] == '0') { + ++ptr; + } + if ((end - ptr) > MAX_LONG_DIGITS) { + return NOT_A_LONG; + } + long value = 0L; + for (; ptr < last; ++ptr) { + int digit = lexical[ptr] - '0'; + if (digit < 0 || digit > 9) { + return NOT_A_LONG; + } + value = (value * 10) + digit; + } + return lastLongDigit(value, lexical[last] - '0', neg); + } + + /** + * Helper method for {@code tryParseLong}: appends the last digit, + * which is the only one that can overflow (with up to 19 digits) + */ + private static long lastLongDigit(long value, int digit, boolean neg) + { + if (digit < 0 || digit > 9) { + return NOT_A_LONG; + } + if (value >= MAX_LONG_DIV_10 + && (value > MAX_LONG_DIV_10 || digit > MAX_LONG_LAST_DIGIT)) { + return NOT_A_LONG; + } + value = (value * 10) + digit; + return neg ? -value : value; + } + /* /////////////////////////////////////////////// // Shared methods, error reporting @@ -1016,11 +1120,6 @@ public void decode(char[] lexical, int start, int end) public static final class IntegerDecoder extends DecoderBase { - /** - * Max. number of digits for which values are parsed as a {@code long} first (any 18-digit number fits in one) - */ - private static final int MAX_LONG_DIGITS = 18; - protected BigInteger mValue; public IntegerDecoder() { } @@ -1033,7 +1132,11 @@ public IntegerDecoder() { } @Override public void decode(String lexical) throws IllegalArgumentException { - if (decodeAsLong(lexical)) { + // Fast path for values that fit in a long; anything else (including + // reporting of invalid values) is left to BigInteger + long l = tryParseLong(lexical, 0, lexical.length()); + if (l != NOT_A_LONG) { + mValue = BigInteger.valueOf(l); return; } try { @@ -1046,7 +1149,9 @@ public void decode(String lexical) throws IllegalArgumentException @Override public void decode(char[] lexical, int start, int end) throws IllegalArgumentException { - if (decodeAsLong(lexical, start, end)) { + long l = tryParseLong(lexical, start, end); + if (l != NOT_A_LONG) { + mValue = BigInteger.valueOf(l); return; } String lexicalStr = new String(lexical, start, (end-start)); @@ -1056,68 +1161,6 @@ public void decode(char[] lexical, int start, int end) throws IllegalArgumentExc throw constructInvalidValue(lexicalStr); } } - - /** - * Fast path for the common case of an optional sign followed by at - * most {@link #MAX_LONG_DIGITS} (ASCII) digits: such values fit in - * a {@code long}, avoiding construction of a String and the general - * {@link BigInteger} parser. Anything else is left to - * {@link BigInteger}, including reporting of invalid values. - * - * @return True if the value was decoded; false if not handled - */ - private boolean decodeAsLong(String lexical) - { - final int end = lexical.length(); - int ptr = 0; - boolean neg = false; - if (end > 0) { - char ch = lexical.charAt(0); - if (ch == '-' || ch == '+') { - neg = (ch == '-'); - ++ptr; - } - } - if (ptr == end || (end - ptr) > MAX_LONG_DIGITS) { - return false; - } - long value = 0L; - for (; ptr < end; ++ptr) { - int digit = lexical.charAt(ptr) - '0'; - if (digit < 0 || digit > 9) { - return false; - } - value = (value * 10) + digit; - } - mValue = BigInteger.valueOf(neg ? -value : value); - return true; - } - - private boolean decodeAsLong(char[] lexical, int start, final int end) - { - int ptr = start; - boolean neg = false; - if (ptr < end) { - char ch = lexical[ptr]; - if (ch == '-' || ch == '+') { - neg = (ch == '-'); - ++ptr; - } - } - if (ptr == end || (end - ptr) > MAX_LONG_DIGITS) { - return false; - } - long value = 0L; - for (; ptr < end; ++ptr) { - int digit = lexical[ptr] - '0'; - if (digit < 0 || digit > 9) { - return false; - } - value = (value * 10) + digit; - } - mValue = BigInteger.valueOf(neg ? -value : value); - return true; - } } public static final class DecimalDecoder diff --git a/src/test/java/org/codehaus/stax2/ri/typed/BigIntegerDecoderTest.java b/src/test/java/org/codehaus/stax2/ri/typed/BigIntegerDecoderTest.java index 1972d92..94e5ab3 100644 --- a/src/test/java/org/codehaus/stax2/ri/typed/BigIntegerDecoderTest.java +++ b/src/test/java/org/codehaus/stax2/ri/typed/BigIntegerDecoderTest.java @@ -9,47 +9,78 @@ import static org.junit.jupiter.api.Assertions.assertTrue; /** - * Tests for {@link ValueDecoderFactory.IntegerDecoder}: values with up to 18 digits take a fast path, which must give - * the same results as {@link BigInteger#BigInteger(String)}, like all other values. + * Tests for {@link ValueDecoderFactory.IntegerDecoder}: values that fit in a {@code long} take a fast path + * ({@link ValueDecoderFactory.DecoderBase#tryParseLong}), which must give the same results as + * {@link BigInteger#BigInteger(String)}, like all other values. */ class BigIntegerDecoderTest { + private static final long NOT_A_LONG = ValueDecoderFactory.DecoderBase.NOT_A_LONG; - private static final String[] VALID = { + // Values handled by the fast path + private static final String[] VALID_LONG = { "0", "-0", "+0", "5", "+5", "-5", "007", "-007", "42", - "2147483648", "-2147483649", "9223372036854775807", - // longest values for fast path: 18 digits + "2147483648", "-2147483649", "999999999999999999", "-999999999999999999", "+100000000000000000", - // 19 digits and more: BigInteger only - "1000000000000000000", "-9223372036854775808", "9999999999999999999", - "123456789012345678901234567890", "-000000000000000000000000000001", - // non-ASCII digits (accepted by BigInteger): U+0663 ARABIC-INDIC DIGIT THREE - "\u0663", "1\u0663" + // 19 digits, up to Long.MAX_VALUE + "1000000000000000000", "9223372036854775807", "-9223372036854775807", + // leading zeroes do not count towards max. digits + "0000000000000000000042", "-000000000000000000000000000001", "00000000000000000000000", + "0009223372036854775807" + }; + + // Valid values left to BigInteger + private static final String[] VALID_NOT_LONG = { + // overflow on last digit + "9223372036854775808", "-9223372036854775808", "9223372036854775810", + "9999999999999999999", "10000000000000000000", + "123456789012345678901234567890" }; private static final String[] INVALID = { - "", "-", "+", "--1", "+-1", "1a", "a1", "1.5", "1e3", "1 2", " 1", "1 ", "0x10" + "", "-", "+", "--1", "+-1", "1a", "a1", "1.5", "1e3", "1 2", " 1", "1 ", "0x10", "00-1" }; - private final ValueDecoderFactory.IntegerDecoder decoder = new ValueDecoderFactory().getIntegerDecoder(); + @Test + void tryParseLong() + { + for (String value : VALID_LONG) { + long expected = Long.parseLong(value); + assertEquals(expected, ValueDecoderFactory.DecoderBase.tryParseLong(value, 0, value.length()), value); + char[] buf = pad(value); + assertEquals(expected, ValueDecoderFactory.DecoderBase.tryParseLong(buf, 2, buf.length - 2), value); + } + for (String[] values : new String[][] { VALID_NOT_LONG, INVALID }) { + for (String value : values) { + assertEquals(NOT_A_LONG, ValueDecoderFactory.DecoderBase.tryParseLong(value, 0, value.length()), value); + char[] buf = pad(value); + assertEquals(NOT_A_LONG, ValueDecoderFactory.DecoderBase.tryParseLong(buf, 2, buf.length - 2), value); + } + } + } @Test void decodeValidString() { - for (String value : VALID) { - decoder.decode(value); - assertEquals(new BigInteger(value), decoder.getValue(), value); + for (String[] values : new String[][] { VALID_LONG, VALID_NOT_LONG }) { + for (String value : values) { + ValueDecoderFactory.IntegerDecoder decoder = newDecoder(); + decoder.decode(value); + assertEquals(new BigInteger(value), decoder.getValue(), value); + } } } @Test void decodeValidCharArray() { - for (String value : VALID) { - // Decoded range is not at the start or end of the array - char[] buf = ("ab" + value + "cd").toCharArray(); - decoder.decode(buf, 2, buf.length - 2); - assertEquals(new BigInteger(value), decoder.getValue(), value); + for (String[] values : new String[][] { VALID_LONG, VALID_NOT_LONG }) { + for (String value : values) { + ValueDecoderFactory.IntegerDecoder decoder = newDecoder(); + char[] buf = pad(value); + decoder.decode(buf, 2, buf.length - 2); + assertEquals(new BigInteger(value), decoder.getValue(), value); + } } } @@ -58,7 +89,7 @@ void decodeInvalidString() { for (String value : INVALID) { IllegalArgumentException e = assertThrows(IllegalArgumentException.class, - () -> decoder.decode(value), value); + () -> newDecoder().decode(value), value); assertTrue(e.getMessage().contains("not a valid lexical representation of integer"), e.getMessage()); } } @@ -67,10 +98,28 @@ void decodeInvalidString() void decodeInvalidCharArray() { for (String value : INVALID) { - char[] buf = ("ab" + value + "cd").toCharArray(); + char[] buf = pad(value); IllegalArgumentException e = assertThrows(IllegalArgumentException.class, - () -> decoder.decode(buf, 2, buf.length - 2), value); + () -> newDecoder().decode(buf, 2, buf.length - 2), value); assertTrue(e.getMessage().contains("not a valid lexical representation of integer"), e.getMessage()); } } + + @Test + void decodeInvertedRange() + { + // Must not be decoded as an (empty) zero value + assertThrows(IndexOutOfBoundsException.class, + () -> newDecoder().decode("12345".toCharArray(), 4, 2)); + } + + private static ValueDecoderFactory.IntegerDecoder newDecoder() { + return new ValueDecoderFactory().getIntegerDecoder(); + } + + // Decoded range is not at the start or end of the array; padding with digits so reading outside + // of the range gives a wrong value, instead of falling back to BigInteger + private static char[] pad(String value) { + return ("12" + value + "34").toCharArray(); + } } From af4dfd7fbccb02cda4bf25fafb23f5b0589ffb9d Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Thu, 8 Oct 2026 18:25:45 -0700 Subject: [PATCH 3/3] ... --- .../org/codehaus/stax2/ri/typed/ValueDecoderFactory.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/org/codehaus/stax2/ri/typed/ValueDecoderFactory.java b/src/main/java/org/codehaus/stax2/ri/typed/ValueDecoderFactory.java index 7003791..e9e4cea 100644 --- a/src/main/java/org/codehaus/stax2/ri/typed/ValueDecoderFactory.java +++ b/src/main/java/org/codehaus/stax2/ri/typed/ValueDecoderFactory.java @@ -191,6 +191,8 @@ public abstract static class DecoderBase * Value returned by {@code tryParseLong} for values it does not handle * (it is never returned for handled values, as {@link Long#MIN_VALUE} * itself is not handled) + * + * @since 4.3.2 */ protected static final long NOT_A_LONG = Long.MIN_VALUE; @@ -476,6 +478,8 @@ protected static final long parseLong(String digitChars, int start, int end) * Anything else, including invalid values, is left to the caller. * * @return Parsed value, or {@link #NOT_A_LONG} if value was not handled + * + * @since 4.3.2 */ protected static final long tryParseLong(String lexical, int ptr, final int end) { @@ -508,6 +512,11 @@ protected static final long tryParseLong(String lexical, int ptr, final int end) return lastLongDigit(value, lexical.charAt(last) - '0', neg); } + /** + * @see #tryParseLong(String, int, int) + * + * @since 4.3.2 + */ protected static final long tryParseLong(char[] lexical, int ptr, final int end) { if (ptr >= end) {