Skip to content

Commit 4983e03

Browse files
authored
avoid instance creations in fast parser code (#886)
1 parent 63925aa commit 4983e03

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/main/java/com/fasterxml/jackson/core/io/doubleparser/FastDoubleParser.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
public class FastDoubleParser {
1717

18+
private static final DoubleBitsFromCharArray CHAR_ARRAY_PARSER = new DoubleBitsFromCharArray();
19+
private static final DoubleBitsFromCharSequence CHAR_SEQ_PARSER = new DoubleBitsFromCharSequence();
1820

1921
/**
2022
* Don't let anyone instantiate this class.
@@ -47,7 +49,7 @@ public static double parseDouble(CharSequence str) throws NumberFormatException
4749
* @throws NumberFormatException if the string can not be parsed
4850
*/
4951
public static double parseDouble(CharSequence str, int offset, int length) throws NumberFormatException {
50-
long bitPattern = new DoubleBitsFromCharSequence().parseFloatingPointLiteral(str, offset, length);
52+
long bitPattern = CHAR_SEQ_PARSER.parseFloatingPointLiteral(str, offset, length);
5153
if (bitPattern == AbstractFloatValueParser.PARSE_ERROR) {
5254
throw new NumberFormatException("Illegal input");
5355
}
@@ -79,7 +81,7 @@ public static double parseDouble(char[] str) throws NumberFormatException {
7981
* @throws NumberFormatException if the string can not be parsed
8082
*/
8183
public static double parseDouble(char[] str, int offset, int length) throws NumberFormatException {
82-
long bitPattern = new DoubleBitsFromCharArray().parseFloatingPointLiteral(str, offset, length);
84+
long bitPattern = CHAR_ARRAY_PARSER.parseFloatingPointLiteral(str, offset, length);
8385
if (bitPattern == AbstractFloatValueParser.PARSE_ERROR) {
8486
throw new NumberFormatException("Illegal input");
8587
}
@@ -109,7 +111,7 @@ public static double parseDouble(char[] str, int offset, int length) throws Numb
109111
* otherwise, {@code -1L}.
110112
*/
111113
public static long parseDoubleBits(CharSequence str, int offset, int length) {
112-
return new DoubleBitsFromCharSequence().parseFloatingPointLiteral(str, offset, length);
114+
return CHAR_SEQ_PARSER.parseFloatingPointLiteral(str, offset, length);
113115
}
114116

115117
/**
@@ -128,6 +130,6 @@ public static long parseDoubleBits(CharSequence str, int offset, int length) {
128130
* otherwise, {@code -1L}.
129131
*/
130132
public static long parseDoubleBits(char[] str, int offset, int length) {
131-
return new DoubleBitsFromCharArray().parseFloatingPointLiteral(str, offset, length);
133+
return CHAR_ARRAY_PARSER.parseFloatingPointLiteral(str, offset, length);
132134
}
133135
}

src/main/java/com/fasterxml/jackson/core/io/doubleparser/FastFloatParser.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
public class FastFloatParser {
1717

18+
private static final FloatBitsFromCharArray CHAR_ARRAY_PARSER = new FloatBitsFromCharArray();
19+
private static final FloatBitsFromCharSequence CHAR_SEQ_PARSER = new FloatBitsFromCharSequence();
20+
1821
/**
1922
* Don't let anyone instantiate this class.
2023
*/
@@ -46,7 +49,7 @@ public static float parseFloat(CharSequence str) throws NumberFormatException {
4649
* @throws NumberFormatException if the string can not be parsed
4750
*/
4851
public static float parseFloat(CharSequence str, int offset, int length) throws NumberFormatException {
49-
long bitPattern = new FloatBitsFromCharSequence().parseFloatingPointLiteral(str, offset, length);
52+
long bitPattern = CHAR_SEQ_PARSER.parseFloatingPointLiteral(str, offset, length);
5053
if (bitPattern == AbstractFloatValueParser.PARSE_ERROR) {
5154
throw new NumberFormatException("Illegal input");
5255
}
@@ -78,7 +81,7 @@ public static float parseFloat(char[] str) throws NumberFormatException {
7881
* @throws NumberFormatException if the string can not be parsed
7982
*/
8083
public static float parseFloat(char[] str, int offset, int length) throws NumberFormatException {
81-
long bitPattern = new FloatBitsFromCharArray().parseFloatingPointLiteral(str, offset, length);
84+
long bitPattern = CHAR_ARRAY_PARSER.parseFloatingPointLiteral(str, offset, length);
8285
if (bitPattern == AbstractFloatValueParser.PARSE_ERROR) {
8386
throw new NumberFormatException("Illegal input");
8487
}
@@ -108,7 +111,7 @@ public static float parseFloat(char[] str, int offset, int length) throws Number
108111
* otherwise, {@code -1L}.
109112
*/
110113
public static long parseFloatBits(CharSequence str, int offset, int length) {
111-
return new FloatBitsFromCharSequence().parseFloatingPointLiteral(str, offset, length);
114+
return CHAR_SEQ_PARSER.parseFloatingPointLiteral(str, offset, length);
112115
}
113116

114117
/**
@@ -127,6 +130,6 @@ public static long parseFloatBits(CharSequence str, int offset, int length) {
127130
* otherwise, {@code -1L}.
128131
*/
129132
public static long parseFloatBits(char[] str, int offset, int length) {
130-
return new FloatBitsFromCharArray().parseFloatingPointLiteral(str, offset, length);
133+
return CHAR_ARRAY_PARSER.parseFloatingPointLiteral(str, offset, length);
131134
}
132135
}

0 commit comments

Comments
 (0)