← Back to Pull requests

chore: Write JSON doubles without std::to_chars on iOS below 16.3 #463

Closed
unknown closed this pull request without merging on Nov 23, 2026 17:34 UTC
View on GitHub
unknown unknown commented on Nov 20, 2026 18:31 UTC
Author

refs: RUM-20303

## Context

We want the code in types to be portable to other platforms and build toolchains, even those where dd-sdk-cpp is not supported in full. This is one in a series of PRs addressing minor portability issues.

This one deals with how we serialize floating-point values to JSON.

## Problem

This PR is very similar to the previous one, which fixed the equivalent problem for parsing, but with a few key differences:

  • That PR (#462):

  • - Problem: We use std::from_chars to parse JSON floats, but some platforms don't fully support floating-point from_chars
    - Fallback: On those platforms, we fall back to strtod_l
    - Impact: The fallback applies to libc++ with LLVM < 20, NDK < r29, macOS/iOS < 26.0
  • This PR:

  • - Problem: We use std::to_chars to serialize JSON floats, but some platforms don't fully support floating-point to_chars
    - Fallback: On those platforms, _there is no C-standard-library equivalent that exactly matches the to_chars behavior_
    - Impact: The fallback only applies to Apple deployment targets below iOS 16.3 or macOS 13.3

    Widespread support for floating-point to_chars predates widespread support for floating-point from_chars by a few years, so iOS targets below 16.3 are the only case in our range of supported toolchains where we need to worry about the lack of floating-point to_chars. dd-sdk-cpp's own macOS builds target macOS 14.0.

    As currently written, our JSON-serialization code would fail to compile in builds targeting iOS below 16.3. Certain cross-platform SDKs that rely on dd-sdk-cpp's types code enforce a minimum iOS target version of 15.0, so we need to address this issue.

    ## Solution

    We could close this gap in one of two ways:

    1. Fall back to a simple approximation, e.g. snprintf with %g format
    2. Implement a drop-in replacement for std::to_chars that produces identical results

    We take the latter approach here. It's more code to write and maintain, but it avoids having subtle platform-specific differences in how we serialize numeric JSON values. With this fix, we can continue to guarantee that our JSON code can serialize any finite double value to JSON (formatted exactly the same on all platforms), parse it back, and end up with the exact same double.

    The key constraint is that snprintf can only serialize floating-point values with a fixed precision, whereas to_chars works out exactly how many digits are needed to identify that double exactly, and writes it with the fewest digits. If we took Approach 1, we'd have to use %.17g to guarantee lossless serialization, but values would end up needlessly long and noisy, e.g. 0.10000000000000001 instead of 0.1, or 10000000000000000 instead of 1e+16.

    Therefore, we reimplement the to_chars logic in
    WriteDoubleWithoutToChars. This function requires a bunch of hand-rolled code for ascertaining the shortest digits, deciding on the appropriate notation, and writing the result. It reads the double's exact decimal expansion with snprintf("%.766e"), tries each length from one digit up, and keeps the first candidate that strtod parses back to the same double, so its correctness rests on snprintf writing exact expansions and strtod rounding correctly. It switches the calling thread to the "C" locale with uselocale() while it does this, so the decimal separator is always ..

    (A fully optimized to_chars implementation would use a modern algorithm like Ryu to do this more efficiently, but we accept the performance tradeoff for the narrow case that this fallback applies to. The fallback takes several microseconds per double on a desktop CPU, from one long snprintf and up to 34 strtod calls.)

    float.hpp selects the fallback by comparing the Clang-defined deployment target with the OS versions that first shipped floating-point to_chars in Apple's libc++: macOS 13.3, iOS and tvOS 16.3, and watchOS 9.3.

    libc++ also reports this through _LIBCPP_AVAILABILITY_HAS_TO_CHARS_FLOATING_POINT, but only from libc++ 18 (Xcode 16), and Xcode 15 has the same restriction. So checking the deployment target covers both. As a side effect, macOS builds targeting versions below 13.3 now compile too.

    The fallback is compiled on every platform except Windows, which has no uselocale(), so that unit tests can compare it with std::to_chars on hosts that have both. Linux and macOS builds therefore carry it as unused code.

    ## Testing

    New unit tests compare WriteDoubleWithoutToChars with std::to_chars across hand-picked values in both notations and at the boundaries between them, every power of 10 and of 2 with their neighbors, and 40,000 seeded random values, half of them arbitrary bit patterns and half short decimals. Another test checks that a comma-decimal locale on the calling thread doesn't change the output.

    check-all passes on macOS. The full unit test suite also passes in a build targeting macOS 13.0, where every WriteJson(double) call uses the fallback.

    With both Xcode 26.4 and Xcode 15.2, compiling float.cpp for iOS 15.0 and 16.2 and for macOS 11.0 and 13.0 selects the fallback, and iOS 16.3 and later and macOS 13.3 and later use to_chars. NDK r27c, r28 and r29 at API level 26 use to_chars. Across 43 Apple targets in Xcode 26.4, covering macOS, iOS, tvOS, watchOS, Mac Catalyst and visionOS, the deployment-target check agrees with libc++'s
    _LIBCPP_AVAILABILITY_HAS_TO_CHARS_FLOATING_POINT.

    The types module also compiles for Android (arm64-v8a and x86_64) and for iOS 15.0, and on-device checks pass on both platforms.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: fuzzybinary Review duration: 4262m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #463"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%