Skip to content

user: compute the ids of touched objects once per query - #42

Open
cldellow wants to merge 2 commits into
masterfrom
user-touched-once
Open

cldellow wants to merge 2 commits into
masterfrom
user-touched-once

Conversation

@cldellow

@cldellow cldellow commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Fixes #32.

Base: this PR was stacked on #41 (the relations fix for the touched filters). Upstream has since merged #41, and this fork's master now includes it, so this PR targets master and its diff shows only the cache. The "Base" column below is that relations fix.

Change

For uid_touched and user_touched, User_Constraint scanned all of the users' current and attic metadata (touched_ids_by_users) in get_node_ids, get_way_ids and get_relation_ids, and again in filter. When the query engine uses both, as it does when these filters choose the candidates, every metadata block was decompressed twice. Inside a loop, both scans repeated in every iteration.

The touched ids depend only on the users, so they are the same for the whole query:

  • Kept after first use: the constraint keeps each type's list, and both callers share it.
  • Cleanup: calc_ranges_both, which computed the same ranges as calc_ranges_32 and calc_ranges_31 together, is removed.

One file. No change to the database format or the query API.

Update: an earlier version didn't keep lists longer than 2M ids, which excluded the most prolific users. That bound is gone (second commit), for the reasons under Memory.

Impact

CPU time (user + system) on the Nova Scotia history database (#2), warm cache, 4-core VM, against the base branch (the relations fix). Median of 5 alternating runs for the first query, one run per build for the loop. Output is identical for every row. The users are identified by their rank in edit count in the extract, since their ids come from licensed data.

Query User Base This PR
[out:csv(::count)]; nwr(uid_touched:N)(44.60,-63.66,44.70,-63.52); out count; #1 1.99 s 1.22 s (−39%)
the same #10 1.43 s 0.79 s (−45%)
the same #20 0.15 s 0.12 s (−20%)
[out:csv(::count)]; node[amenity=cafe](44.64,-63.60,44.66,-63.56)->.c; foreach.c->.it { nwr(around.it:100)(uid_touched:N); out count; } (76 iterations) #1 124.7 s 6.1 s (20×)
the same #10 95.8 s 3.0 s (32×)
the same #20 7.6 s 0.5 s (15×)

N is the user's id.

Testing

  • Differential fuzzer against the base branch, history database: 1,500 queries with users,history,complete, 0 differences (3 timeouts skipped). The earlier version also passed 2,500 such queries, 1,000 on the plain database, and 300 against itself.
  • Targeted check: 400 queries applying the four user filters (uid, user, uid_touched, user_touched) to nodes, ways, relations and nwr. Each is written one of four ways: directly, with the box first, on an input set, or inside a foreach. 53 returned data. 0 differences from the base branch.
  • Mutation test (earlier version, same relation code): a build where relations reuse the cached way ids gave wrong counts (0 instead of 1,089 relations for one user). The targeted check caught it on 20 of the 400 queries.

Memory

Each list costs 8 bytes per touched node and 4 bytes per touched way or relation, and lives until the query ends. Like other auxiliary structures, it isn't counted toward [maxsize:].

Upstream already holds a full node list while it builds a second one: the list from get_node_ids lives in Query_Statement::execute through apply_all_filters, which calls filter. Here the second list is the kept one, so the extra peak over upstream is the way and relation lists. In a loop, upstream frees its lists between iterations, while this PR keeps them, but it no longer recomputes them.

Peak RSS, first query above: user #1 75.9 → 85.1 MB; user #10 24.0 → 21.0 MB; user #20 10.7 → 11.8 MB. Loop: user #1 67.5 → 69.8 MB.

Limitations and open questions

  • Planet scale: per-user counts on the planet are unknown here. A user who touched 100M nodes needs 800 MB per copy of the node list, both here and upstream, which also holds two copies at once.
  • Narrow test setup: one regional extract, warm cache, one VM.

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

Copy link
Copy Markdown
Owner Author

Colin thoughts: haven't reviewed this yet. If memory is the concern, check with upstream to see what would be reasonable. Also considering using something like roaring bitmaps or streamvbyte, depending on how exactly the data is used.

@cldellow

Copy link
Copy Markdown
Owner Author

Colin's thoughts: low risk, high confidence, medium benefit.

This is a only-do-things-once optimization, reuse work to avoid decompressing multiple times.

The question is whether we should have a cap above which we fall back to the old behaviour.

IMO, we should just be uncapped, I believe the previous code already required loading all the IDs into memory at once. gall/lambert aren't constrained on RAM. Also removing the cap for the biggest user in Nova Scotia was "at a cost of 9.4 MB more peak memory", which, lol. I think we can spare that. (Yes, yes, Nova Scotia is a podunk province. Still.)

Needs followup: remove the cap, measure impact, re-assess PR changes.

@cldellow cldellow added human-reviewed Colin has reviewed this. needs-followup Colin reviewed and wants Claude to iterate further, but didn't have spare agents available. labels Nov 21, 2026

Copy link
Copy Markdown
Owner Author

Follow-up done: the cap is removed (second commit). The user with the most edits in the extract now gets the full speedup: −39% on the box query and 124.7 s → 6.1 s on the 76-iteration loop, with identical output. Peak RSS for the box query goes from 75.9 to 85.1 MB. Upstream already holds two copies of the node list during filter, so the extra peak over upstream is just the way and relation lists. Re-fuzzed on the history database (1,500 queries) and re-ran the targeted check (400 queries): 0 differences. The description is updated. I left the needs-followup label for you to clear.


Generated by Claude Code

@cldellow

Copy link
Copy Markdown
Owner Author

Colin's revised thoughts after followup: low risk, high confidence, likely high benefit for the specific case of user queries.

I think the code may even read more clearly now.

@cldellow cldellow removed the needs-followup Colin reviewed and wants Claude to iterate further, but didn't have spare agents available. label Nov 21, 2026
For uid_touched and user_touched, User_Constraint scanned all of the users'
current and attic metadata (touched_ids_by_users) in get_node_ids,
get_way_ids and get_relation_ids, and again in filter. When the query engine
used both, as it does when these filters choose the candidates, every metadata
block was decompressed twice. Inside a loop, both scans repeated in every
iteration.

The touched ids depend only on the users, so they are the same for the whole
query. The constraint now keeps them after first use, and both callers share
them. A list longer than 2M ids (16 MB) is not kept: it is handed over or
dropped after use, as before, so a prolific user's ids never stay in memory
longer than they did. calc_ranges_both, which computed the same ranges as
calc_ranges_32 and calc_ranges_31 together, is no longer needed.

On the Nova Scotia history database, with N a user id:

  [out:csv(::count)]; nwr(uid_touched:N)(44.60,-63.66,44.70,-63.52); out count;

drops from 2.02 s to 1.20 s, and a foreach over 76 cafes running
nwr(around.it:100)(uid_touched:N) from 126 s to 6 s, with identical output.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT
The previous commit kept the touched ids only for lists of up to 2M ids, so
the most prolific users, likely the ones queried most, still had their
metadata read twice. Upstream already holds a full id list from get_node_ids
while filter builds a second one, so keeping one list costs no more than that
for nodes. The way and relation lists are smaller, and the bound is removed.

On the Nova Scotia history database, for the user with the most edits in the
extract (N their user id):

  [out:csv(::count)]; nwr(uid_touched:N)(44.60,-63.66,44.70,-63.52); out count;

drops from 1.99 s to 1.22 s (it was 2.20 s with the bound). Peak RSS goes from
75.9 to 85.1 MB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT
@cldellow
cldellow force-pushed the user-touched-relations branch from 0430ee9 to 4a9c1b5 Compare November 23, 2026 02:49
@cldellow
cldellow changed the base branch from user-touched-relations to master November 23, 2026 22:35
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.

uid_touched queries read every metadata block twice

2 participants