From 186d2e4114a2d15f89bc033261216f2e64b92322 Mon Sep 17 00:00:00 2001 From: huangcj <1411187249@qq.com> Date: Sat, 31 May 2025 17:19:55 +0800 Subject: [PATCH] Optimize BigDecimal zero value handling by reusing instances to reduce memory footprint --- .../java/com/mysql/cj/result/BigDecimalValueFactory.java | 3 +++ .../java/com/mysql/cj/result/BigDecimalValueFactoryTest.java | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/main/core-impl/java/com/mysql/cj/result/BigDecimalValueFactory.java b/src/main/core-impl/java/com/mysql/cj/result/BigDecimalValueFactory.java index ad51e8862..d8ba6ee5e 100644 --- a/src/main/core-impl/java/com/mysql/cj/result/BigDecimalValueFactory.java +++ b/src/main/core-impl/java/com/mysql/cj/result/BigDecimalValueFactory.java @@ -52,6 +52,9 @@ public BigDecimalValueFactory(PropertySet pset, int scale) { * @return result */ private BigDecimal adjustResult(BigDecimal d) { + if (d.signum() == 0) { + d = BigDecimal.ZERO.setScale(d.scale()); + } if (this.hasScale) { try { return d.setScale(this.scale); diff --git a/src/test/java/com/mysql/cj/result/BigDecimalValueFactoryTest.java b/src/test/java/com/mysql/cj/result/BigDecimalValueFactoryTest.java index f421f4bea..25a80ce47 100644 --- a/src/test/java/com/mysql/cj/result/BigDecimalValueFactoryTest.java +++ b/src/test/java/com/mysql/cj/result/BigDecimalValueFactoryTest.java @@ -22,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; import java.math.BigDecimal; @@ -103,6 +104,7 @@ public void testCreateFromBigDecimal() { assertEquals(Constants.BIG_DECIMAL_MAX_INTEGER_VALUE, this.vf.createFromBigDecimal(Constants.BIG_DECIMAL_MAX_INTEGER_VALUE)); assertEquals(Constants.BIG_DECIMAL_NEGATIVE_ONE, this.vf.createFromBigDecimal(Constants.BIG_DECIMAL_NEGATIVE_ONE)); assertEquals(Constants.BIG_DECIMAL_MIN_INTEGER_VALUE, this.vf.createFromBigDecimal(Constants.BIG_DECIMAL_MIN_INTEGER_VALUE)); + assertSame(this.vf.createFromBigDecimal(Constants.BIG_DECIMAL_ZERO), this.vf.createFromBigDecimal(Constants.BIG_DECIMAL_ZERO)); } @Test