Skip to content

around: use fixed-size arrays for 3-D vectors - #7

Open
cldellow wants to merge 1 commit into
masterfrom
around-fixed-vectors
Open

cldellow wants to merge 1 commit into
masterfrom
around-fixed-vectors

Conversation

@cldellow

@cldellow cldellow commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Fixes #4.

Change

statements/around.cc stored every 3-element vector in a heap-allocated std::vector<double>. That covers the helpers cartesian, sum, cross_prod and rescale, and the Prepared_Segment/Prepared_Point members. Every intersect() call therefore allocated and freed three small buffers, and large around queries make hundreds of millions of these calls.

This switches them to std::array<double, 3> (Vec3). The arithmetic is unchanged, so results are identical.

Two files changed, all within around. No change to the database format or the query API.

Impact

Wall time per query (single-threaded, so close to CPU time), Nova Scotia test database (see #2), compared with master:

Query master This PR
A: around 282 ways (defined below) 24.1 s 9.9 s
B: around nodes and ways within 1 km (defined below) 33.8 s 11.6 s
around against small sets (benchmark q06: node[amenity=cafe](bbox); node(around:300)[shop], and other spot checks) unchanged unchanged
Full text of the timed queries

A (282 ways around a point in Dartmouth, then everything within 100 m of them):

[out:csv(::count)]; way(around:250,44.679085,-63.511161)->.s0; way(around.s0:100); out count;

B (nodes and ways within 1 km, then ways within 100 m of them):

[out:csv(::count)]; nw(around:1000,44.790135,-65.396762)->.s0; way(around.s0:100)["width"!="1"]; out count;

The profile share of malloc/free/operator new was about 30% on the first query; the rest of the gain likely comes from cheaper arithmetic without the indirection.

The quadratic cost itself remains; #3 covers that, with a follow-up PR stacked on this one.

Memory

This reduces memory. Each Prepared_Segment shrinks from 4 doubles plus 3 heap-allocated vectors (about 220 bytes including allocator overhead) to 13 doubles (104 bytes), and Prepared_Point shrinks similarly. Nothing new is allocated.

Testing

  • Benchmark: identical output on all benchmark and around spot-check queries.
  • Differential fuzzer, seed 2: 3,000 queries against master. 2,997 identical, 3 skipped (timeouts on both builds), 0 differences.
  • 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).

Limitations

  • Narrow test setup: one extract, warm cache, one 4-core VM.
  • Production query mix unknown: we don't know how often production queries hit around with way or relation sets, so the effect on server load is unknown.

Generated by Claude Code

The geometry helpers in around.cc (cartesian, sum, cross_prod, rescale)
and the Prepared_Segment/Prepared_Point members used std::vector<double>
for 3-element vectors, so every intersection test allocated and freed
several small heap buffers. On larger inputs around makes hundreds of
millions of these tests.

Use std::array<double, 3>. The arithmetic is unchanged, so results are
identical.

way(around.s0:100) with 282 ways in .s0 (Nova Scotia extract): 24.1 s ->
10.1 s CPU. around queries against small sets are unchanged.

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 20, 2026 — with Claude
@cldellow cldellow added the human-reviewed Colin has reviewed this. label Nov 21, 2026
@cldellow

Copy link
Copy Markdown
Owner Author

Colin's thoughts: Low risk, high confidence, high benefit. Move heap allocations to the stack by eliminating a vector that is constant size.

cldellow pushed a commit that referenced this pull request Nov 23, 2026
…ions (#7)

When this function was first created, it applied the filter
unconditionally:
https://github.com/fossgis/Overpass-API/blame/46abfbeda4d774db76f502156aa0416b9742f517/src/overpass_api/statements/user.cc#L203-L208

A subsequent commit introduced a guard. The guards for nodes/ways are
correct, the guard for relations is not:
https://github.com/fossgis/Overpass-API/blame/8edf11b0419c39df4cd074bb83b6854392e57b13/src/overpass_api/statements/user.cc#L252-L256

The result is that the filter is not correctly applied in all cases.

For example, querying for a relation touched by a non-existent user will
always find the relation:

```
rel(1);
nwr._(uid_touched:999999999);
out ids;
```

If you broaden the input to include a node so that the relation filter
code path is exercised, the output is correct:

```
(rel(1); node(1););
nwr._(uid_touched:999999999);
out ids;
```

With the fix, the two queries return the same result - the empty set.
cldellow added a commit that referenced this pull request Nov 27, 2026
Add run_testsuite_regressions.sh, which runs every test in
osm-3s_testing/input/regression_*/ and compares its output with
expected/regression_*/, and call it from run_testsuite.sh. Each test is
one directory, so a fix can add its test without changing a script:

  stdin.log    the query (for a CGI test, the request body, if any)
  init.osm     optional: data imported into a database of the test's own
  import.args  optional: arguments for update_database (default --meta)
  env          optional: run cgi-bin/interpreter with these environment
               variables instead of osm3s_query, with a dispatcher over
               the test's database

Each test runs for at most 60 seconds and writes at most 10 MB per file,
so a test of a hang fails rather than blocks the suite. The database is
created with mktemp, because the dispatcher's socket path in a directory
under osm-3s_testing/run can exceed the length limit for socket paths.
osm-3s_testing/README.regressions describes how to add a test.

The tests:

- regression_ql_nesting_limit (04c045b, "Stop the OverpassQL parser
  looping past the nesting limit"): node(1); inside 1024 levels of
  parentheses. The parser reports that nesting is limited to 1023 levels
  and stops. Without the fix, it reports parse errors without end.

- regression_user_touched_relations (#7): on one relation, way and two
  nodes by uid 1,

    rel(1); nwr._(uid_touched:999999999); out count;
    rel(1); nwr._(uid_touched:1); out count;

  count 0 and 1 relations. Without the fix, the first counts 1 relation.

- regression_cgi_custom_url_line_break (#4): a GET request for
  data=[out:custom];node(1);out; with url=http://example.com/%0d%0aX-Injected:%201
  is rejected with 400 Bad Request. Without the fix, the response is a
  302 redirect with an extra header line "X-Injected: 1".

- regression_cgi_bbox_not_numeric (#4): a GET request for
  data=node(bbox);out; with bbox=7,51,8);node(id:1,52 fails to parse.
  Without the fix, the value is substituted as is, the query becomes
  node(51,7,52,8);node(id:1);out; and returns node 1.

Reverting one fix on this branch makes its tests fail and no others:

  git revert -n 04c045b         # regression_ql_nesting_limit
  git revert -n 4a9c1b5         # regression_user_touched_relations
  git revert -n -m 1 8edf11b    # the two regression_cgi_* tests

Not covered: the parts of #4 that leave the output of these requests
unchanged (reading the POST body in chunks, a "%" at the end of the
input, the scope of the "=" search, the operates_* checks in
pt_diagrams), the cgi-bin shell scripts of #3, and #8 and #11, which
change only performance.

Co-Authored-By: Colin Dellow <cldellow@gmail.com>
cldellow added a commit that referenced this pull request Nov 27, 2026
Test #7: on one relation, way and two nodes by uid 1,

  rel(1); nwr._(uid_touched:999999999); out count;
  rel(1); nwr._(uid_touched:1); out count;

count 0 and 1 relations. With the fix reverted (git revert -n 4a9c1b5),
the first counts 1 relation.

Co-Authored-By: Colin Dellow <cldellow@gmail.com>
cldellow added a commit that referenced this pull request Nov 27, 2026
Test #7: on one relation, way and two nodes by uid 1,

  rel(1); nwr._(uid_touched:999999999); out count;
  rel(1); nwr._(uid_touched:1); out count;

count 0 and 1 relations. With the fix reverted (git revert -n 4a9c1b5),
the first counts 1 relation.

Co-Authored-By: Colin Dellow <cldellow@gmail.com>
cldellow added a commit that referenced this pull request Nov 27, 2026
Test #7: on one relation, way and two nodes by uid 1,

  rel(1); nwr._(uid_touched:999999999); out count;
  rel(1); nwr._(uid_touched:1); out count;

count 0 and 1 relations. With the fix reverted (git revert -n 4a9c1b5),
the first counts 1 relation.

Co-Authored-By: Colin Dellow <cldellow@gmail.com>
cldellow added a commit that referenced this pull request Nov 27, 2026
Test #7: on one relation, way and two nodes by uid 1,

  rel(1); nwr._(uid_touched:999999999); out count;
  rel(1); nwr._(uid_touched:1); out count;

count 0 and 1 relations. With the fix reverted (git revert -n 4a9c1b5),
the first counts 1 relation.

Co-Authored-By: Colin Dellow <cldellow@gmail.com>
cldellow added a commit that referenced this pull request Nov 27, 2026
Test #7: on one relation, way and two nodes by uid 1,

  rel(1); nwr._(uid_touched:999999999); out count;
  rel(1); nwr._(uid_touched:1); out count;

count 0 and 1 relations. With the fix reverted (git revert -n 4a9c1b5),
the first counts 1 relation.

Co-Authored-By: Colin Dellow <cldellow@gmail.com>
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. query-perf Query performance change; collected on all-changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

around: heap allocations in 3-D vector maths cost about 30% of CPU

2 participants