chore: Write JSON doubles without std::to_chars on iOS below 16.3 #463
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
fuzzybinary
refs: RUM-20303
## Context
We want the code in
typesto be portable to other platforms and build toolchains, even those wheredd-sdk-cppis 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:
- Problem: We use
std::from_charsto parse JSON floats, but some platforms don't fully support floating-pointfrom_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
- Problem: We use
std::to_charsto serialize JSON floats, but some platforms don't fully support floating-pointto_chars- Fallback: On those platforms, _there is no C-standard-library equivalent that exactly matches the
to_charsbehavior_- Impact: The fallback only applies to Apple deployment targets below iOS 16.3 or macOS 13.3
Widespread support for floating-point
to_charspredates widespread support for floating-pointfrom_charsby 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-pointto_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
typescode 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.
snprintfwith%gformat2. Implement a drop-in replacement for
std::to_charsthat produces identical resultsWe 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
doublevalue to JSON (formatted exactly the same on all platforms), parse it back, and end up with the exact samedouble.The key constraint is that
snprintfcan only serialize floating-point values with a fixed precision, whereasto_charsworks 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%.17gto guarantee lossless serialization, but values would end up needlessly long and noisy, e.g.0.10000000000000001instead of0.1, or10000000000000000instead of1e+16.Therefore, we reimplement the
to_charslogic inWriteDoubleWithoutToChars. 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 withsnprintf("%.766e"), tries each length from one digit up, and keeps the first candidate thatstrtodparses back to the same double, so its correctness rests onsnprintfwriting exact expansions andstrtodrounding correctly. It switches the calling thread to the "C" locale withuselocale()while it does this, so the decimal separator is always..(A fully optimized
to_charsimplementation 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 longsnprintfand up to 34strtodcalls.)float.hppselects the fallback by comparing the Clang-defined deployment target with the OS versions that first shipped floating-pointto_charsin 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 withstd::to_charson hosts that have both. Linux and macOS builds therefore carry it as unused code.## Testing
New unit tests compare
WriteDoubleWithoutToCharswithstd::to_charsacross 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-allpasses on macOS. The full unit test suite also passes in a build targeting macOS 13.0, where everyWriteJson(double)call uses the fallback.With both Xcode 26.4 and Xcode 15.2, compiling
float.cppfor 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 useto_chars. NDK r27c, r28 and r29 at API level 26 useto_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
typesmodule also compiles for Android (arm64-v8a and x86_64) and for iOS 15.0, and on-device checks pass on both platforms.