From 6fde65ad0c96f446c4abc25f88bb0aa5e705d692 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Nov 2026 00:28:22 +0000 Subject: [PATCH] block cache: don't keep global tag index blocks While a union whose statements will re-read the same blocks runs, the transaction's Block_Cache keeps the decompressed blocks of every data file it reads. Such statements share every filter apart from their tag filters, so they read the same skeleton and local tag blocks, but look up different tags in the global tag index: its blocks are not read again. They only filled the cache, up to its limit, and cost a copy each. Nonsynced_Transaction::data_index no longer attaches the block cache to the *_tags_global files. On a US northeast full-history extract (Geofabrik), warm cache, 4-core VM, peak RSS, CPU as the median of 3 alternating runs: [out:json][timeout:90]; (way["maxheight"]["highway"](39.0,-70.0,40.0,-69.0); way["bridge"]["maxheight"](39.0,-70.0,40.0,-69.0); way["tunnel"]["maxheight"](39.0,-70.0,40.0,-69.0); node["maxheight"]["highway"](39.0,-70.0,40.0,-69.0); ); out center; 74 MB -> 13 MB, CPU 0.36 s -> 0.33 s Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01736kJWpJaGbt8ML6jz9N35 --- src/template_db/transaction.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/template_db/transaction.h b/src/template_db/transaction.h index 101092362..01c5b280a 100644 --- a/src/template_db/transaction.h +++ b/src/template_db/transaction.h @@ -97,6 +97,12 @@ inline void Nonsynced_Transaction::flush() } +inline bool ends_with(const std::string& s, const std::string& suffix) +{ + return s.size() >= suffix.size() && s.compare(s.size() - suffix.size(), suffix.size(), suffix) == 0; +} + + inline File_Blocks_Index_Base* Nonsynced_Transaction::data_index (const File_Properties* fp) { @@ -108,7 +114,9 @@ inline File_Blocks_Index_Base* Nonsynced_Transaction::data_index File_Blocks_Index_Base* data_index = fp->new_data_index(access_mode, use_shadow, db_dir, file_name_extension); if (data_index != 0) { - if (access_mode == Access_Mode::readonly) + // Statements of a union read the same skeleton and local tag blocks, but look up different + // tags in the global tag index, so its blocks are rarely read again: don't cache them. + if (access_mode == Access_Mode::readonly && !ends_with(fp->get_file_name_trunk(), "_tags_global")) data_index->block_cache = &block_cache_; data_files[fp] = data_index; }