Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid instance creations in fast parser code #886

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
public class FastDoubleParser {

private static final DoubleBitsFromCharArray CHAR_ARRAY_PARSER = new DoubleBitsFromCharArray();
private static final DoubleBitsFromCharSequence CHAR_SEQ_PARSER = new DoubleBitsFromCharSequence();

/**
* Don't let anyone instantiate this class.
Expand Down Expand Up @@ -47,7 +49,7 @@ public static double parseDouble(CharSequence str) throws NumberFormatException
* @throws NumberFormatException if the string can not be parsed
*/
public static double parseDouble(CharSequence str, int offset, int length) throws NumberFormatException {
long bitPattern = new DoubleBitsFromCharSequence().parseFloatingPointLiteral(str, offset, length);
long bitPattern = CHAR_SEQ_PARSER.parseFloatingPointLiteral(str, offset, length);
if (bitPattern == AbstractFloatValueParser.PARSE_ERROR) {
throw new NumberFormatException("Illegal input");
}
Expand Down Expand Up @@ -79,7 +81,7 @@ public static double parseDouble(char[] str) throws NumberFormatException {
* @throws NumberFormatException if the string can not be parsed
*/
public static double parseDouble(char[] str, int offset, int length) throws NumberFormatException {
long bitPattern = new DoubleBitsFromCharArray().parseFloatingPointLiteral(str, offset, length);
long bitPattern = CHAR_ARRAY_PARSER.parseFloatingPointLiteral(str, offset, length);
if (bitPattern == AbstractFloatValueParser.PARSE_ERROR) {
throw new NumberFormatException("Illegal input");
}
Expand Down Expand Up @@ -109,7 +111,7 @@ public static double parseDouble(char[] str, int offset, int length) throws Numb
* otherwise, {@code -1L}.
*/
public static long parseDoubleBits(CharSequence str, int offset, int length) {
return new DoubleBitsFromCharSequence().parseFloatingPointLiteral(str, offset, length);
return CHAR_SEQ_PARSER.parseFloatingPointLiteral(str, offset, length);
}

/**
Expand All @@ -128,6 +130,6 @@ public static long parseDoubleBits(CharSequence str, int offset, int length) {
* otherwise, {@code -1L}.
*/
public static long parseDoubleBits(char[] str, int offset, int length) {
return new DoubleBitsFromCharArray().parseFloatingPointLiteral(str, offset, length);
return CHAR_ARRAY_PARSER.parseFloatingPointLiteral(str, offset, length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
public class FastFloatParser {

private static final FloatBitsFromCharArray CHAR_ARRAY_PARSER = new FloatBitsFromCharArray();
private static final FloatBitsFromCharSequence CHAR_SEQ_PARSER = new FloatBitsFromCharSequence();

/**
* Don't let anyone instantiate this class.
*/
Expand Down Expand Up @@ -46,7 +49,7 @@ public static float parseFloat(CharSequence str) throws NumberFormatException {
* @throws NumberFormatException if the string can not be parsed
*/
public static float parseFloat(CharSequence str, int offset, int length) throws NumberFormatException {
long bitPattern = new FloatBitsFromCharSequence().parseFloatingPointLiteral(str, offset, length);
long bitPattern = CHAR_SEQ_PARSER.parseFloatingPointLiteral(str, offset, length);
if (bitPattern == AbstractFloatValueParser.PARSE_ERROR) {
throw new NumberFormatException("Illegal input");
}
Expand Down Expand Up @@ -78,7 +81,7 @@ public static float parseFloat(char[] str) throws NumberFormatException {
* @throws NumberFormatException if the string can not be parsed
*/
public static float parseFloat(char[] str, int offset, int length) throws NumberFormatException {
long bitPattern = new FloatBitsFromCharArray().parseFloatingPointLiteral(str, offset, length);
long bitPattern = CHAR_ARRAY_PARSER.parseFloatingPointLiteral(str, offset, length);
if (bitPattern == AbstractFloatValueParser.PARSE_ERROR) {
throw new NumberFormatException("Illegal input");
}
Expand Down Expand Up @@ -108,7 +111,7 @@ public static float parseFloat(char[] str, int offset, int length) throws Number
* otherwise, {@code -1L}.
*/
public static long parseFloatBits(CharSequence str, int offset, int length) {
return new FloatBitsFromCharSequence().parseFloatingPointLiteral(str, offset, length);
return CHAR_SEQ_PARSER.parseFloatingPointLiteral(str, offset, length);
}

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