Skip to content

optimize single-pass f32 parsing#227

Merged
liuq19 merged 3 commits intocloudwego:mainfrom
liuq19:optimize-single-pass-f32
Apr 15, 2026
Merged

optimize single-pass f32 parsing#227
liuq19 merged 3 commits intocloudwego:mainfrom
liuq19:optimize-single-pass-f32

Conversation

@liuq19
Copy link
Copy Markdown
Collaborator

@liuq19 liuq19 commented Apr 15, 2026

Summary

  • add a dedicated single-pass f32 parser in sonic-number
  • use that parser from Deserializer::deserialize_f32
  • lock in overflow handling with explicit negative tests returning FloatMustBeFinite
  • strengthen fuzz coverage with a dedicated f32 literal target, curated seeds, and CI fuzz-time normalization

Background

The previous f32 deserialization path first parsed the number as f64 and then converted it back to f32. For tie-boundary inputs such as 17005001.000000000000130, that can produce an off-by-one ULP result.

Changes

This updates f32 parsing to stay on a single pass, avoids the extra string reparse fallback, and preserves the expected f32 rounding behavior for affected cases.

On top of the parser change, this PR also strengthens regression coverage around f32 parsing:

  • add direct regression tests for precision-sensitive literals and overflow-to-infinity cases such as 3.4028236e38 and 1e39
  • add a dedicated fuzz_f32_literal target with curated corpus seeds for rounding and overflow boundaries
  • expand fuzz seed coverage for JSON value, string, path, deep nesting, and serde roundtrip paths
  • normalize FUZZ_TIME in CI so values like 3m are passed to libFuzzer as seconds instead of effectively running for only a few seconds

Validation

  • cargo test test_deserialize_f32_single_pass_precision
  • cargo test --manifest-path sonic-number/Cargo.toml test_parse_float32
  • cargo test --manifest-path fuzz/Cargo.toml

Copilot AI review requested due to automatic review settings April 15, 2026 07:15
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 15, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 71.16%. Comparing base (96daa50) to head (890e228).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #227      +/-   ##
==========================================
+ Coverage   71.08%   71.16%   +0.07%     
==========================================
  Files          42       42              
  Lines        9744     9762      +18     
==========================================
+ Hits         6927     6947      +20     
+ Misses       2817     2815       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates f32 deserialization to avoid the previous f64-first parsing path (which could mis-round tie-boundary decimals), by introducing and wiring in a dedicated single-pass f32 parser in sonic-number.

Changes:

  • Add sonic_number::parse_float32 and shared generic float-parsing helpers for f32.
  • Switch Deserializer::deserialize_f32 to use the new single-pass f32 parser (removing the raw-string fallback parse).
  • Add regression tests for the tie-boundary precision case and direct parse_float32 unit tests.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/serde/mod.rs Adds a regression test covering the precision edge case and -0 handling for f32 deserialization.
src/serde/de.rs Replaces the prior f64f32 + fallback parse approach with a dedicated single-pass f32 parse call.
src/parser.rs Adds a Parser::parse_float32 wrapper that advances the reader index appropriately and maps number errors into parser errors.
sonic-number/src/lib.rs Implements parse_float32, introduces generic float helpers, and adds unit tests for parse_float32.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread sonic-number/src/lib.rs
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 253acbad4f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread sonic-number/src/lib.rs Outdated
@liuq19 liuq19 merged commit 03545a9 into cloudwego:main Apr 15, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants