Skip to content

improved BigDecimal parsing performance#3992

Open
wenshao wants to merge 1 commit into
mainfrom
dec_optim_20260216
Open

improved BigDecimal parsing performance#3992
wenshao wants to merge 1 commit into
mainfrom
dec_optim_20260216

Conversation

@wenshao

@wenshao wenshao commented Feb 16, 2026

Copy link
Copy Markdown
Member
  • Use bit-shift operations for faster digit parsing
  • Simplify overflow detection to single check after digit loop
  • Handle exponent with combined scale to avoid string-based parsing

What this PR does / why we need it?

Summary of your change

Please indicate you've done the following:

  • Made sure tests are passing and test coverage is added if needed.
  • Made sure commit message follow the rule of Conventional Commits specification.
  • Considered the docs impact and opened a new docs issue or PR with docs changes if needed.

- Use bit-shift operations for faster digit parsing
- Simplify overflow detection to single check after digit loop
- Handle exponent with combined scale to avoid string-based parsing

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

@wenshao wenshao left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The same overflow check is duplicated in core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java and should be fixed there as well.

— gpt-5.5 via Qwen Code /review

}
}

// Check overflow: more than 18 digits, or 19 digits with negative value (sign bit set)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

[Critical] This post-loop overflow check can accept corrupted 19-digit values. For exactly 19 digits, values greater than Long.MAX_VALUE may overflow during longValue = longValue * 10 + digit and still finish with a non-negative wrapped longValue, so they take the BigDecimal.valueOf(longValue, combinedScale) fast path instead of falling back to TypeUtils.parseBigDecimal(...). That can silently return the wrong BigDecimal for valid JSON numbers outside signed long range.

A safer fix is to restore per-digit overflow detection, or add an exact 19-digit range check before using the accumulated longValue; otherwise force the existing string-based fallback.

— gpt-5.5 via Qwen Code /review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant