Repository navigation
Conversation
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
|
Colin's thoughts: 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 |
|
@b1tw153 notes elsewhere:
This makes a stronger argument for moving to C++17, where we can be confident that |
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:snprintf("%.7f"), the same conversion as before.printf's sign on negative values that round to zero (-0.0000000).std::to_charswould 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 fossgismaster. 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.master[out:json]; way[highway](44.60,-63.66,44.70,-63.52); out geom;(benchmark q10, 14.5 MB)[out:csv(::id,::lat,::lon)]; node(44.60,-63.66,44.70,-63.52); out;(17 MB)[out:json]; node(44.60,-63.66,44.70,-63.52); out body;(63 MB)[out:xml]; way[highway](44.60,-63.66,44.70,-63.52); out geom;(13 MB)[out:json]; way[building](44.640,-63.585,44.655,-63.565); out geom;(benchmark q09)[out:xml]; nwr[amenity](44.60,-63.66,44.70,-63.52); out meta;(benchmark q08)[out:json]; way[building](44.60,-63.66,44.70,-63.52); out center;(22 MB)¹ 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
printf: 92 million values were checked against bothsnprintf("%.7f")and the stream withstd::fixed<<std::setprecision(7), with 0 mismatches. The values were:master(bench/fuzz.py, see Benchmark queries and differential fuzzer #2), mixing JSON, XML and CSV output withgeom,bb,centerandbody. 0 differences; 12 skipped, all of which time out onmaster.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
snprintfproducing the same characters as the stream, which is how libstdc++ implementsstd::fixedoutput. The 92-million-value test confirms this for glibc; other C libraries are untested.Generated by Claude Code