From fc2adc3a11a585f8075824e0e1207d39c59e8420 Mon Sep 17 00:00:00 2001 From: Baoyi Chen Date: Fri, 24 Dec 2021 11:56:58 +0800 Subject: [PATCH 1/2] BigDecimal.toPlainString no need to check decimal exponent. so we could replace StringUtils.fixDecimalExponent(BigDecimal.toPlainString) directly to BigDecimal.toPlainString Signed-off-by: Baoyi Chen --- .../core-impl/java/com/mysql/cj/AbstractQueryBindings.java | 4 ++-- .../java/com/mysql/cj/ClientPreparedQueryBindings.java | 2 +- .../java/com/mysql/cj/ServerPreparedQueryBindings.java | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/core-impl/java/com/mysql/cj/AbstractQueryBindings.java b/src/main/core-impl/java/com/mysql/cj/AbstractQueryBindings.java index 6da3d890b..9d4fb81ec 100644 --- a/src/main/core-impl/java/com/mysql/cj/AbstractQueryBindings.java +++ b/src/main/core-impl/java/com/mysql/cj/AbstractQueryBindings.java @@ -962,7 +962,7 @@ public void setObject(int parameterIndex, Object parameterObj, MysqlType targetM case LONGTEXT: case JSON: if (parameterObj instanceof BigDecimal) { - setString(parameterIndex, (StringUtils.fixDecimalExponent(((BigDecimal) parameterObj).toPlainString()))); + setString(parameterIndex, ((BigDecimal) parameterObj).toPlainString()); } else { setString(parameterIndex, parameterObj.toString()); } @@ -1000,7 +1000,7 @@ public void setObject(int parameterIndex, Object parameterObj, MysqlType targetM case LONGTEXT: case JSON: if (parameterObj instanceof BigDecimal) { - setString(parameterIndex, (StringUtils.fixDecimalExponent(((BigDecimal) parameterObj).toPlainString()))); + setString(parameterIndex, ((BigDecimal) parameterObj).toPlainString()); } else if (parameterObj instanceof java.sql.Clob) { setClob(parameterIndex, (java.sql.Clob) parameterObj); } else { diff --git a/src/main/core-impl/java/com/mysql/cj/ClientPreparedQueryBindings.java b/src/main/core-impl/java/com/mysql/cj/ClientPreparedQueryBindings.java index d4cd71036..72791ace4 100644 --- a/src/main/core-impl/java/com/mysql/cj/ClientPreparedQueryBindings.java +++ b/src/main/core-impl/java/com/mysql/cj/ClientPreparedQueryBindings.java @@ -135,7 +135,7 @@ public void setBigDecimal(int parameterIndex, BigDecimal x) { if (x == null) { setNull(parameterIndex); } else { - setValue(parameterIndex, StringUtils.fixDecimalExponent(x.toPlainString()), MysqlType.DECIMAL); + setValue(parameterIndex, x.toPlainString(), MysqlType.DECIMAL); } } diff --git a/src/main/core-impl/java/com/mysql/cj/ServerPreparedQueryBindings.java b/src/main/core-impl/java/com/mysql/cj/ServerPreparedQueryBindings.java index d239b0247..632fa5745 100644 --- a/src/main/core-impl/java/com/mysql/cj/ServerPreparedQueryBindings.java +++ b/src/main/core-impl/java/com/mysql/cj/ServerPreparedQueryBindings.java @@ -166,7 +166,7 @@ public void setBigDecimal(int parameterIndex, BigDecimal x) { } else { ServerPreparedQueryBindValue binding = getBinding(parameterIndex, false); this.sendTypesToServer.compareAndSet(false, binding.resetToType(MysqlType.FIELD_TYPE_NEWDECIMAL, this.numberOfExecutions)); - binding.value = StringUtils.fixDecimalExponent(x.toPlainString()); + binding.value = x.toPlainString(); binding.parameterType = MysqlType.DECIMAL; } } From d8a5ea63e8a1b10a3b3b4ab74fba1ebbf7403243 Mon Sep 17 00:00:00 2001 From: Baoyi Chen Date: Fri, 24 Dec 2021 12:12:03 +0800 Subject: [PATCH 2/2] Replace BigDecimal(String) to BigDecimal(char[]) to reduce memory allocation Signed-off-by: Baoyi Chen --- .../java/com/mysql/cj/util/StringUtils.java | 18 +++++++++++++++++- .../cj/protocol/a/MysqlBinaryValueDecoder.java | 2 +- .../cj/protocol/a/MysqlTextValueDecoder.java | 2 +- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/core-api/java/com/mysql/cj/util/StringUtils.java b/src/main/core-api/java/com/mysql/cj/util/StringUtils.java index f3120976c..19ef8bd88 100644 --- a/src/main/core-api/java/com/mysql/cj/util/StringUtils.java +++ b/src/main/core-api/java/com/mysql/cj/util/StringUtils.java @@ -832,6 +832,22 @@ public static String toAsciiString(byte[] buffer) { * @return the ASCII string */ public static String toAsciiString(byte[] buffer, int startPos, int length) { + return new String(toAsciiCharArray(buffer, startPos, length)); + } + + /** + * Returns the bytes as an ASCII String. + * + * @param buffer + * the bytes to convert + * @param startPos + * the position to start converting + * @param length + * the length of the string to convert + * + * @return the ASCII char array + */ + public static char[] toAsciiCharArray(byte[] buffer, int startPos, int length) { char[] charArray = new char[length]; int readpoint = startPos; @@ -840,7 +856,7 @@ public static String toAsciiString(byte[] buffer, int startPos, int length) { readpoint++; } - return new String(charArray); + return charArray; } /** diff --git a/src/main/protocol-impl/java/com/mysql/cj/protocol/a/MysqlBinaryValueDecoder.java b/src/main/protocol-impl/java/com/mysql/cj/protocol/a/MysqlBinaryValueDecoder.java index fb5dc8a7c..994e1a02d 100644 --- a/src/main/protocol-impl/java/com/mysql/cj/protocol/a/MysqlBinaryValueDecoder.java +++ b/src/main/protocol-impl/java/com/mysql/cj/protocol/a/MysqlBinaryValueDecoder.java @@ -261,7 +261,7 @@ } public T decodeDecimal(byte[] bytes, int offset, int length, ValueFactory vf) { - BigDecimal d = new BigDecimal(StringUtils.toAsciiString(bytes, offset, length)); + BigDecimal d = new BigDecimal(StringUtils.toAsciiCharArray(bytes, offset, length)); return vf.createFromBigDecimal(d); } diff --git a/src/main/protocol-impl/java/com/mysql/cj/protocol/a/MysqlTextValueDecoder.java b/src/main/protocol-impl/java/com/mysql/cj/protocol/a/MysqlTextValueDecoder.java index f6f496dde..c4f6bcd69 100644 --- a/src/main/protocol-impl/java/com/mysql/cj/protocol/a/MysqlTextValueDecoder.java +++ b/src/main/protocol-impl/java/com/mysql/cj/protocol/a/MysqlTextValueDecoder.java @@ -135,7 +135,7 @@ } public T decodeDecimal(byte[] bytes, int offset, int length, ValueFactory vf) { - BigDecimal d = new BigDecimal(StringUtils.toAsciiString(bytes, offset, length)); + BigDecimal d = new BigDecimal(StringUtils.toAsciiCharArray(bytes, offset, length)); return vf.createFromBigDecimal(d); }