From e18827580fa865b12f06ae0170d68389fc94ec66 Mon Sep 17 00:00:00 2001 From: dsh Date: Fri, 6 Nov 2026 19:59:12 +0800 Subject: [PATCH] Cache key: fix a collision and pin the model version Asked whether the cache survives a change of CELL_SIZE, and looking at the key turned up a real problem. The key used to be params | mode | metric | the stat line's values | layout. Every block in asm/ uses a stat line, which is why nothing had gone wrong yet, but a block written with map leaves stat empty -- so two map blocks with the same params/mode/metric hashed to the same entry, and the second one would have been served the first one's program. The key is now the resolved target table itself (every entry, per plane), which is exactly what the search is given: * params / CELL_SIZE is part of it, and the table covers all 2^params entries, so blocks of different sizes cannot collide, and changing params in a block simply misses and recomputes * two blocks that describe the same table (map or stat, either spelling) now share one entry Also added kModelVersion to the key. Without it, changing the instruction model later (say adding conjunctions) would keep serving programs that were optimal for the old model. Bump it whenever the block set changes. Cache files now start with a readable header, e.g. # params 3 mode 0 metric 1 0 2 0 1 4 0 Re-warmed after the change: 7 entries, 885s (bit_raw_add again, 990M states). asm/test.r8asm still 1666 characters, all seven derived tables still match the old implementations bit for bit, and a second assembly hits the cache for all seven and produces an identical tape. --- .r8forge_cache/2848dd58d63b3dd2.txt | 3 ++ .r8forge_cache/2c0e4b4ec0113a05.txt | 4 --- .r8forge_cache/349167b2c24afc1e.txt | 4 +++ .r8forge_cache/46f10fe79b67ccdc.txt | 5 +++ .r8forge_cache/4a3cdd9890e2c185.txt | 3 -- .r8forge_cache/635f3aabeb0db198.txt | 2 -- .r8forge_cache/75883d0d632b1705.txt | 4 --- .r8forge_cache/8e8fe7cc111be258.txt | 2 -- .r8forge_cache/ace576fd09aaca0a.txt | 3 ++ ...6ce570b1bb727.txt => c3e01554593c2da5.txt} | 1 + ...9a544df4fd342.txt => c6172a918afc9275.txt} | 1 + .r8forge_cache/f2bb80857c9a12a5.txt | 5 +++ src/synth.cc | 34 +++++++++++++++---- 13 files changed, 49 insertions(+), 22 deletions(-) create mode 100644 .r8forge_cache/2848dd58d63b3dd2.txt delete mode 100644 .r8forge_cache/2c0e4b4ec0113a05.txt create mode 100644 .r8forge_cache/349167b2c24afc1e.txt create mode 100644 .r8forge_cache/46f10fe79b67ccdc.txt delete mode 100644 .r8forge_cache/4a3cdd9890e2c185.txt delete mode 100644 .r8forge_cache/635f3aabeb0db198.txt delete mode 100644 .r8forge_cache/75883d0d632b1705.txt delete mode 100644 .r8forge_cache/8e8fe7cc111be258.txt create mode 100644 .r8forge_cache/ace576fd09aaca0a.txt rename .r8forge_cache/{1906ce570b1bb727.txt => c3e01554593c2da5.txt} (56%) rename .r8forge_cache/{3d99a544df4fd342.txt => c6172a918afc9275.txt} (50%) create mode 100644 .r8forge_cache/f2bb80857c9a12a5.txt diff --git a/.r8forge_cache/2848dd58d63b3dd2.txt b/.r8forge_cache/2848dd58d63b3dd2.txt new file mode 100644 index 0000000..d5a9145 --- /dev/null +++ b/.r8forge_cache/2848dd58d63b3dd2.txt @@ -0,0 +1,3 @@ +# params 3 mode 0 metric 1 +0 2 0 +1 4 0 diff --git a/.r8forge_cache/2c0e4b4ec0113a05.txt b/.r8forge_cache/2c0e4b4ec0113a05.txt deleted file mode 100644 index d56dc1e..0000000 --- a/.r8forge_cache/2c0e4b4ec0113a05.txt +++ /dev/null @@ -1,4 +0,0 @@ -1 12 0 -0 6 1 -1 5 0 -3 6 0 diff --git a/.r8forge_cache/349167b2c24afc1e.txt b/.r8forge_cache/349167b2c24afc1e.txt new file mode 100644 index 0000000..8f9b008 --- /dev/null +++ b/.r8forge_cache/349167b2c24afc1e.txt @@ -0,0 +1,4 @@ +# params 4 mode 0 metric 1 +0 10 0 +1 13 0 +3 3 0 diff --git a/.r8forge_cache/46f10fe79b67ccdc.txt b/.r8forge_cache/46f10fe79b67ccdc.txt new file mode 100644 index 0000000..3afbd6f --- /dev/null +++ b/.r8forge_cache/46f10fe79b67ccdc.txt @@ -0,0 +1,5 @@ +# params 4 mode 0 metric 1 +1 12 0 +0 6 1 +1 5 0 +3 6 0 diff --git a/.r8forge_cache/4a3cdd9890e2c185.txt b/.r8forge_cache/4a3cdd9890e2c185.txt deleted file mode 100644 index 1c8dbea..0000000 --- a/.r8forge_cache/4a3cdd9890e2c185.txt +++ /dev/null @@ -1,3 +0,0 @@ -0 10 0 -1 13 0 -3 3 0 diff --git a/.r8forge_cache/635f3aabeb0db198.txt b/.r8forge_cache/635f3aabeb0db198.txt deleted file mode 100644 index aa9f006..0000000 --- a/.r8forge_cache/635f3aabeb0db198.txt +++ /dev/null @@ -1,2 +0,0 @@ -0 4 0 -1 4 1 diff --git a/.r8forge_cache/75883d0d632b1705.txt b/.r8forge_cache/75883d0d632b1705.txt deleted file mode 100644 index 75eb0ab..0000000 --- a/.r8forge_cache/75883d0d632b1705.txt +++ /dev/null @@ -1,4 +0,0 @@ -0 8 0 -1 1 0 -0 6 0 -3 5 1 diff --git a/.r8forge_cache/8e8fe7cc111be258.txt b/.r8forge_cache/8e8fe7cc111be258.txt deleted file mode 100644 index eef48f4..0000000 --- a/.r8forge_cache/8e8fe7cc111be258.txt +++ /dev/null @@ -1,2 +0,0 @@ -0 2 0 -1 4 0 diff --git a/.r8forge_cache/ace576fd09aaca0a.txt b/.r8forge_cache/ace576fd09aaca0a.txt new file mode 100644 index 0000000..1623e3b --- /dev/null +++ b/.r8forge_cache/ace576fd09aaca0a.txt @@ -0,0 +1,3 @@ +# params 3 mode 0 metric 1 +0 4 0 +1 4 1 diff --git a/.r8forge_cache/1906ce570b1bb727.txt b/.r8forge_cache/c3e01554593c2da5.txt similarity index 56% rename from .r8forge_cache/1906ce570b1bb727.txt rename to .r8forge_cache/c3e01554593c2da5.txt index 456f8a2..c93bbd8 100644 --- a/.r8forge_cache/1906ce570b1bb727.txt +++ b/.r8forge_cache/c3e01554593c2da5.txt @@ -1,3 +1,4 @@ +# params 6 mode 0 metric 1 0 38 0 1 41 0 2 51 0 diff --git a/.r8forge_cache/3d99a544df4fd342.txt b/.r8forge_cache/c6172a918afc9275.txt similarity index 50% rename from .r8forge_cache/3d99a544df4fd342.txt rename to .r8forge_cache/c6172a918afc9275.txt index c8a18ff..153a9e1 100644 --- a/.r8forge_cache/3d99a544df4fd342.txt +++ b/.r8forge_cache/c6172a918afc9275.txt @@ -1,3 +1,4 @@ +# params 5 mode 0 metric 1 0 24 0 1 21 0 2 10 1 diff --git a/.r8forge_cache/f2bb80857c9a12a5.txt b/.r8forge_cache/f2bb80857c9a12a5.txt new file mode 100644 index 0000000..9bde83b --- /dev/null +++ b/.r8forge_cache/f2bb80857c9a12a5.txt @@ -0,0 +1,5 @@ +# params 4 mode 0 metric 1 +0 8 0 +1 1 0 +0 6 0 +3 5 1 diff --git a/src/synth.cc b/src/synth.cc index 39f1183..20040b1 100644 --- a/src/synth.cc +++ b/src/synth.cc @@ -2,6 +2,7 @@ #include "r8forge/r8veritas_phase_tt.hh" #include #include +#include #include // ---------------------------------------------------------------- %synth @@ -53,12 +54,24 @@ r8asm_synth_run(const std::vector &in, const std::vector &out, auto tt = v.make_truthtable(start, target); - // ---- cache: the same table always gives the same program, and the tape - // search is measured in minutes, so keep the answer around - std::string key = std::to_string(P) + "|" + std::to_string(mode) + "|" + + // ---- cache: the target table is what the search is given, so it is the + // identity of the problem. Keying on it (rather than on the map or the stat + // line the author wrote) means two blocks that describe the same table share + // one entry, and a table of a different CELL_SIZE can never collide with it + // because the key covers every entry. + // bump kModelVersion whenever the instruction model changes, otherwise a + // table could be served a program that was optimal for the old model + static constexpr int kModelVersion = 1; // classic blocks: sacrifice + mask + std::string key = "v" + std::to_string(kModelVersion) + "|" + + std::to_string(P) + "|" + std::to_string(mode) + "|" + std::to_string(metric) + "|"; - for (int val : stat) + for (int b = 0; b < P; b++) { + unsigned val = 0; + for (size_t j = 0; j < n; j++) + if ((*tt.second)[j] & (1u << b)) + val |= 1u << j; key += std::to_string(val) + ","; + } key += "#"; if (metric == 0) // only the character metric cares where the cells are for (int val : layout) @@ -77,15 +90,22 @@ r8asm_synth_run(const std::vector &in, const std::vector &out, std::ifstream in(cache_path); if (!in) return false; - int sac, mask, or_mode; - while (in >> sac >> mask >> or_mode) - dst.push_back({sac, mask, or_mode}); + std::string line; + while (std::getline(in, line)) { + if (line.empty() || line[0] == '#') + continue; + std::istringstream is(line); + int sac, mask, or_mode; + if (is >> sac >> mask >> or_mode) + dst.push_back({sac, mask, or_mode}); + } return !dst.empty(); }; auto write_cache = [&](const std::vector> &src) { std::error_code ec; std::filesystem::create_directories(cache_dir, ec); std::ofstream out(cache_path); + out << "# params " << P << " mode " << mode << " metric " << metric << "\n"; for (const auto &i : src) out << i[0] << " " << i[1] << " " << i[2] << "\n"; };