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 copy when parsing BigDecimal #798

Merged
merged 1 commit into from
Jul 25, 2022
Merged
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
21 changes: 9 additions & 12 deletions src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,11 @@ public static BigDecimal parse(String valueStr) {
}

public static BigDecimal parse(char[] chars, int off, int len) {
if (off > 0 || len != chars.length) {
chars = Arrays.copyOfRange(chars, off, off+len);
}
return parse(chars);
}

public static BigDecimal parse(char[] chars) {
final int len = chars.length;
try {
if (len < 500) {
return new BigDecimal(chars);
return new BigDecimal(chars, off, len);
}
chars = Arrays.copyOfRange(chars, off, off+len);
Copy link
Member

@pjfanning pjfanning Jul 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make more sense to avoid this Arrays.copyOfRange and change the BigDecimalParser constructor to take (chars, off, len)? It looks like the parseBigDecimal can be readily changed to support an offset and explicit len.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a PR for this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That could make sense if easy enough to do.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return new BigDecimalParser(chars).parseBigDecimal(len / 10);
} catch (NumberFormatException e) {
String desc = e.getMessage();
Expand All @@ -53,17 +46,21 @@ public static BigDecimal parse(char[] chars) {
desc = "Not a valid number representation";
}
String stringToReport;
if (chars.length <= MAX_CHARS_TO_REPORT) {
stringToReport = new String(chars);
if (len <= MAX_CHARS_TO_REPORT) {
stringToReport = new String(chars, off, len);
} else {
stringToReport = new String(Arrays.copyOfRange(chars, 0, MAX_CHARS_TO_REPORT))
stringToReport = new String(Arrays.copyOfRange(chars, off, MAX_CHARS_TO_REPORT))
+ "(truncated, full length is " + chars.length + " chars)";
}
throw new NumberFormatException("Value \"" + stringToReport
+ "\" can not be represented as `java.math.BigDecimal`, reason: " + desc);
}
}

public static BigDecimal parse(char[] chars) {
return parse(chars, 0, chars.length);
}

private BigDecimal parseBigDecimal(final int splitLen) {
boolean numHasSign = false;
boolean expHasSign = false;
Expand Down