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.
This commit is contained in:
dsh committed 2026-11-06 19:59:12 +08:00
1 parent 5f92d288c9
commit e18827580f
13 files changed
+49 -22

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
# params 3 mode 0 metric 1
0 2 0
1 4 0
-4
View File
@@ -1,4 +0,0 @@
1 12 0
0 6 1
1 5 0
3 6 0
+4
View File
@@ -0,0 +1,4 @@
# params 4 mode 0 metric 1
0 10 0
1 13 0
3 3 0
+5
View File
@@ -0,0 +1,5 @@
# params 4 mode 0 metric 1
1 12 0
0 6 1
1 5 0
3 6 0
-3
View File
@@ -1,3 +0,0 @@
0 10 0
1 13 0
3 3 0
-2
View File
@@ -1,2 +0,0 @@
0 4 0
1 4 1
-4
View File
@@ -1,4 +0,0 @@
0 8 0
1 1 0
0 6 0
3 5 1
-2
View File
@@ -1,2 +0,0 @@
0 2 0
1 4 0
+3
View File
@@ -0,0 +1,3 @@
# params 3 mode 0 metric 1
0 4 0
1 4 1
@@ -1,3 +1,4 @@
# params 6 mode 0 metric 1
0 38 0
1 41 0
2 51 0
@@ -1,3 +1,4 @@
# params 5 mode 0 metric 1
0 24 0
1 21 0
2 10 1
+5
View File
@@ -0,0 +1,5 @@
# params 4 mode 0 metric 1
0 8 0
1 1 0
0 6 0
3 5 1
+27 -7
View File
@@ -2,6 +2,7 @@
#include "r8forge/r8veritas_phase_tt.hh"
#include <filesystem>
#include <fstream>
#include <sstream>
#include <chrono>
// ---------------------------------------------------------------- %synth
@@ -53,12 +54,24 @@ r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &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<int> &in, const std::vector<int> &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<std::array<int, 3>> &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";
};