Skip to content

Format output coordinates without general float formatting - #17

Open
cldellow wants to merge 1 commit into
masterfrom
fast-coordinate-output
Open

cldellow wants to merge 1 commit into
masterfrom
fast-coordinate-output

Conversation

@cldellow

@cldellow cldellow commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Fixes #15.

Change

Every coordinate in the JSON, XML, CSV and custom output was written as out<<std::fixed<<std::setprecision(7)<<value, which goes through glibc's general floating-point formatting. For large geometry outputs that was a large share of the CPU time.

This adds Fixed7 (output_formats/fixed7.h) and uses it at all 72 call sites. It writes exactly the same characters:

  • Fast path: scale by 1e7 and round. For |value| ≤ 1e9 the scaling error is far below 1e-6, so the rounding direction is certain.
  • Fallback: within 1e-6 of a rounding tie, and for large or non-finite values, it calls snprintf("%.7f"), the same conversion as before.
  • Negative zero: keeps printf's sign on negative values that round to zero (-0.0000000).
  • Stream state: leaves the stream in fixed notation with precision 7, as the manipulators did, so later output that relies on that state is unaffected.

std::to_chars would be simpler, but it needs C++17, and the build pins -std=c++11.

Six files (one new header, four output formats, src/Makefile.am). Based on fossgis master. No change to the database format or the query API, and output is byte-identical.

Impact

CPU time (user + system), median of 5 alternating runs per build, Nova Scotia test database (Geofabrik extract of 2026-11-08, imported with --meta), warm cache, 4-core VM. Output is byte-identical for every query.

Query master This PR Change
[out:json]; way[highway](44.60,-63.66,44.70,-63.52); out geom; (benchmark q10, 14.5 MB) 536 ms 377 ms −30%
[out:csv(::id,::lat,::lon)]; node(44.60,-63.66,44.70,-63.52); out; (17 MB) 767 ms 468 ms −39%
[out:json]; node(44.60,-63.66,44.70,-63.52); out body; (63 MB) 1,087 ms 771 ms −29%
[out:xml]; way[highway](44.60,-63.66,44.70,-63.52); out geom; (13 MB) 526 ms 418 ms −21%
[out:json]; way[building](44.640,-63.585,44.655,-63.565); out geom; (benchmark q09) 81 ms 63 ms −22%
[out:xml]; nwr[amenity](44.60,-63.66,44.70,-63.52); out meta; (benchmark q08) 371 ms 351 ms −5%
[out:json]; way[building](44.60,-63.66,44.70,-63.52); out center; (22 MB) 671 ms 695 ms noise ¹

¹ This query prints only one coordinate pair per building, so formatting is a small share of its cost. Splitting it by output mode on the all-changes branch (#12): finding the 12,420 buildings takes about 100 ms (out ids), reading and printing their tags adds about 225 ms (out tags), and loading their node geometry for the centres plus printing adds about 195 ms. Computing the centres themselves doesn't show up in the profile.

Queries with small outputs are unaffected.

Testing

  • Formatter against printf: 92 million values were checked against both snprintf("%.7f") and the stream with std::fixed<<std::setprecision(7), with 0 mismatches. The values were:
    • random fixed-point coordinates as Overpass stores them, and their midpoints (as used for centres)
    • random doubles in [−180, 180]
    • 4 million exact rounding ties, each with its two neighbouring doubles
    • signed zeros, tiny negatives, large values, infinities and NaN
  • Differential fuzzer, seeds 2 and 3: 6,000 queries against master (bench/fuzz.py, see Benchmark queries and differential fuzzer #2), mixing JSON, XML and CSV output with geom, bb, center and body. 0 differences; 12 skipped, all of which time out on master.
  • Update, re-fuzzed with the corrected fuzzer: this change has since been re-fuzzed as part of the all-changes branch (All changes combined (tracking PR) #12), with the corrected fuzzer and a bigger vocabulary (areas, history, users and complete). That covered 10,000 queries across the plain, area and history databases (6,000 / 2,000 / 2,000), with 0 differences. The per-PR fuzz run above predates a fuzzer fix: about 8% of its queries were invalid on both builds, so it rests on roughly 5,500 queries that ran (see Benchmark queries and differential fuzzer #2).

Memory

No new allocations: a 64-byte stack buffer per coordinate.

Limitations

  • The fallback relies on snprintf producing the same characters as the stream, which is how libstdc++ implements std::fixed output. The 92-million-value test confirms this for glibc; other C libraries are untested.
  • Narrow test setup: one regional extract, warm cache, one VM.

Generated by Claude Code

Every coordinate in the JSON, XML, CSV and custom output was written
with out<<std::fixed<<std::setprecision(7)<<value, which goes through
glibc's general floating-point formatting. For large geometry outputs
that formatting is a large share of the CPU time.

Add Fixed7 (output_formats/fixed7.h), which writes exactly the same
characters: scale by 1e7 and round, falling back to snprintf("%.7f")
within 1e-6 of a rounding tie and for large or non-finite values. It
leaves the stream in fixed notation with precision 7, as the
manipulators did. Replace the 72 call sites.

Checked against snprintf and the stream for 92 million values: random
fixed-point coordinates, their midpoints, arbitrary doubles, exact ties
and their neighbours, signed zeros and non-finite values. No mismatches.

Nova Scotia extract, CPU time, alternating runs:
  [out:json]; way[highway](44.60,-63.66,44.70,-63.52); out geom;
    536 ms -> 377 ms
  [out:csv(::id,::lat,::lon)]; node(44.60,-63.66,44.70,-63.52); out;
    767 ms -> 468 ms

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT
@cldellow cldellow added the query-perf Query performance change; collected on all-changes label Nov 21, 2026 — with Claude
@cldellow cldellow added the human-reviewed Colin has reviewed this. label Nov 21, 2026
@cldellow

cldellow commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner Author

Colin's thoughts: Low Medium risk [revised due to feedback from @b1tw153), high confidence, high benefit. This is applying a well-known optimization to a common task (converting a double to string).

Caveat: I have no idea where Claude got this function from. I'm satisfied that the function likely works, given its 90 million test cases tested against the old behaviour.

Followup: the project currently targets C++11, but it does also compile cleanly against C++ 17. If the project is willing to move to C++17, the custom function can be replaced with std::to_chars and still retain 85% of the speed improvement. This is a question for upstream.

@cldellow

cldellow commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner Author

@b1tw153 notes elsewhere:

one that caught my eye was Format output coordinates without general float formatting - #17. Node coordinates have to make the round trip from OSM planet data to Overpass XML with no changes to 7 decimal places so the XML output can be used for editing without node version churn. Maybe the change is fine, but that constraint makes it high risk.

This makes a stronger argument for moving to C++17, where we can be confident that std::to_chars is correct. We lose a bit of the speedup of the Fixed7 version but gain peace of mind.

@cldellow cldellow added the needs-followup Colin reviewed and wants Claude to iterate further, but didn't have spare agents available. label Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

human-reviewed Colin has reviewed this. needs-followup Colin reviewed and wants Claude to iterate further, but didn't have spare agents available. query-perf Query performance change; collected on all-changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Coordinate formatting dominates large geometry outputs

2 participants