From 60a4b95c88503eea6dd4b7b2f6ebcf5808118504 Mon Sep 17 00:00:00 2001 From: g4lb Date: Fri, 27 Nov 2026 13:37:34 +0200 Subject: [PATCH] perf: divide without allocation when parsing doubles --- .../fastjson2/util/MutableBigInteger.java | 53 +++++++++++++++++++ .../com/alibaba/fastjson2/util/TypeUtils.java | 2 +- .../fastjson2/util/MutableBigIntegerTest.java | 30 +++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 core/src/test/java/com/alibaba/fastjson2/util/MutableBigIntegerTest.java diff --git a/core/src/main/java/com/alibaba/fastjson2/util/MutableBigInteger.java b/core/src/main/java/com/alibaba/fastjson2/util/MutableBigInteger.java index 46c99054a6..7d5fa3dab6 100644 --- a/core/src/main/java/com/alibaba/fastjson2/util/MutableBigInteger.java +++ b/core/src/main/java/com/alibaba/fastjson2/util/MutableBigInteger.java @@ -74,6 +74,59 @@ final class MutableBigInteger { static final int KNUTH_POW2_THRESH_LEN = 6; static final int KNUTH_POW2_THRESH_ZEROS = 3; + static long divideKnuthLongFast(long intCompact, int ql, int scale) { + if (ql <= 0 && ql > -64 && scale > 0 && scale <= 18 && intCompact > 0) { + // (intCompact << -ql) / 10^scale without allocation, when the quotient fits in [2^32, 2^63) + int n = -ql; + long d = NumberUtils.POW10_LONG_VALUES[scale - 1]; + long hi = n == 0 ? 0 : intCompact >>> (64 - n); + if (hi < (d >>> 1)) { + long q = divide128(hi, intCompact << n, d); + if ((q >>> 32) != 0) { + return q; + } + } + } + return divideKnuthLong(intCompact, ql, scale); + } + + /** + * Unsigned (u1 * 2^64 + u0) / v, for 0 < v < 2^63 and u1 < v (Hacker's Delight, divlu). + */ + private static long divide128(long u1, long u0, long v) { + final long b = 1L << 32; + int s = Long.numberOfLeadingZeros(v); + v <<= s; + long vn1 = v >>> 32; + long vn0 = v & LONG_MASK; + long un32 = (u1 << s) | (u0 >>> (64 - s)); + long un10 = u0 << s; + long un1 = un10 >>> 32; + long un0 = un10 & LONG_MASK; + + long q1 = Long.divideUnsigned(un32, vn1); + long rhat = un32 - q1 * vn1; + while (q1 >= b || Long.compareUnsigned(q1 * vn0, (rhat << 32) | un1) > 0) { + q1--; + rhat += vn1; + if (rhat >= b) { + break; + } + } + + long un21 = (un32 << 32) + un1 - q1 * v; + long q0 = Long.divideUnsigned(un21, vn1); + rhat = un21 - q0 * vn1; + while (q0 >= b || Long.compareUnsigned(q0 * vn0, (rhat << 32) | un0) > 0) { + q0--; + rhat += vn1; + if (rhat >= b) { + break; + } + } + return (q1 << 32) | q0; + } + static long divideKnuthLong(long intCompact, int ql, int scale) { int[] pow10 = MutableBigInteger.BIG_TEN_POWERS_MAGIC_TABLE[scale]; int[] magic_a, magic_b; diff --git a/core/src/main/java/com/alibaba/fastjson2/util/TypeUtils.java b/core/src/main/java/com/alibaba/fastjson2/util/TypeUtils.java index 01e43a9132..14acaa8b6c 100644 --- a/core/src/main/java/com/alibaba/fastjson2/util/TypeUtils.java +++ b/core/src/main/java/com/alibaba/fastjson2/util/TypeUtils.java @@ -3817,7 +3817,7 @@ public static double doubleValue(int signNum, long intCompact, int scale) { } int ql = (int) qb - (P_D + 3); // narrowing qb to an int is safe - long i = MutableBigInteger.divideKnuthLong(intCompact, ql, scale); + long i = MutableBigInteger.divideKnuthLongFast(intCompact, ql, scale); int dq = (Long.SIZE - (P_D + 2)) - Long.numberOfLeadingZeros(i); int eq = (Q_MIN_D - 2) - ql; diff --git a/core/src/test/java/com/alibaba/fastjson2/util/MutableBigIntegerTest.java b/core/src/test/java/com/alibaba/fastjson2/util/MutableBigIntegerTest.java new file mode 100644 index 0000000000..92b126dd8d --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/util/MutableBigIntegerTest.java @@ -0,0 +1,30 @@ +package com.alibaba.fastjson2.util; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +import java.util.Random; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@Tag("util") +public class MutableBigIntegerTest { + @Test + public void divideKnuthLongFast() { + Random random = new Random(1); + for (int i = 0; i < 1_000_000; i++) { + long intCompact = (random.nextLong() >>> 1) >>> random.nextInt(63); + if (intCompact == 0) { + continue; + } + int scale = 1 + random.nextInt(20); + // same ql as TypeUtils.doubleValue + int bitLength = 64 - Long.numberOfLeadingZeros(intCompact); + int ql = bitLength - (int) Math.ceil(scale * 3.321928094887362) - 56; + assertEquals( + MutableBigInteger.divideKnuthLong(intCompact, ql, scale), + MutableBigInteger.divideKnuthLongFast(intCompact, ql, scale), + intCompact + " " + ql + " " + scale); + } + } +}