[PR #2376] d16_format_probe: compute the rule models once per input (TEST-D16-MODELS-CACHE) #2379

Open
opened 2026-11-26 21:21:03 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2376
Author: @enlight3d
Created: 11/26/2026
Status: 🔄 Open

Base: main ← Head: test-d16-models-cache


📝 Commits (1)

  • e6b97d1 d16_format_probe: compute the rule models once per input

📊 Changes

2 files changed (+33 additions, -10 deletions)

View changed files

📝 tests/test_d16_format_probe.py (+8 -0)
📝 tools/d16_format_probe.py (+25 -10)

📄 Description

Claimed in the coordination issue as: TEST-D16-MODELS-CACHE (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7197567323), picking up the item @amine23012004 claimed and then released as free for anyone (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7191191563).

What and why

models16() and models32() in tools/d16_format_probe.py are pure functions of (kind, bits, value), but a sweep, classify() and the tests call them about 955,000 times for about 131,000 distinct inputs, each time redoing the exact Fraction arithmetic. The rule bits are now computed once per input (functools.lru_cache on a tuple), and each call still returns a fresh dict, so a caller that edits it cannot change what the next call gets. Every caller looks the rules up by name (classify() iterates its own rules tuple), so the dict's key order does not reach any output. No result changes.

The commit is amine23012004's, kept as written and with them as its author (their branch test-d16-models-cache, 9a7128947), rebased on current main. It adds a test that the cache hands each call its own dict.

tests/semantic/test_buffer_load_format.py and tools/rdna2spirv.py only name the probe in comments; nothing else imports it except tools/d16_image_probe.py and the two d16 test files.

This PR touches the same two files as #1664; the two merge without conflict (git merge-tree of both heads).

Tested

On main 2e2c51c09 + this commit, Windows 11 (26300), Python 3.12.10, i9-13900K, timed while a full make_game.py build was using the CPU, so the absolute times are noisy:

  • python -I tests/test_d16_format_probe.py: 26.6 s before, 12.0 s after (12.4 s after the rebase); 24 tests OK (2 skipped, GPU).
  • python -I tests/test_d16_image_probe.py: 22.6 s before, 17.0 s after; OK (2 skipped).
  • python -I tools/d16_format_probe.py --self-test: self-test ok.
  • python -I tests/test_image_d16_probe.py: 22 tests OK (2 skipped).
  • python -m unittest discover -s tests/semantic -p test_buffer_load_format.py: 34 tests OK.
  • python -m compileall -q make_game.py tools tests, python -I make_game.py --help, python -I tests/test_text_write_line_endings.py (3 OK), python -I scripts/check_no_game_data.py on both changed files, git diff --check: clean.

amine23012004 measured 26.7 s -> 7.7 s and 21.8 s -> 13.1 s on an idle machine.

GPUs tested: n/a (CPU-only change; the probe's GPU kernels are untouched)

AI-assisted: yes (Claude Code)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (No translator rule here.)

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2376 **Author:** [@enlight3d](https://github.com/enlight3d) **Created:** 11/26/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `test-d16-models-cache` --- ### 📝 Commits (1) - [`e6b97d1`](https://github.com/ubiquitarianism/leonida/commit/e6b97d1e436d0102aa512ac67f43db44c2fb75ef) d16_format_probe: compute the rule models once per input ### 📊 Changes **2 files changed** (+33 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_d16_format_probe.py` (+8 -0) 📝 `tools/d16_format_probe.py` (+25 -10) </details> ### 📄 Description Claimed in the coordination issue as: TEST-D16-MODELS-CACHE (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7197567323), picking up the item @amine23012004 claimed and then released as free for anyone (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7191191563). ### What and why `models16()` and `models32()` in `tools/d16_format_probe.py` are pure functions of (kind, bits, value), but a sweep, `classify()` and the tests call them about 955,000 times for about 131,000 distinct inputs, each time redoing the exact `Fraction` arithmetic. The rule bits are now computed once per input (`functools.lru_cache` on a tuple), and each call still returns a fresh dict, so a caller that edits it cannot change what the next call gets. Every caller looks the rules up by name (`classify()` iterates its own `rules` tuple), so the dict's key order does not reach any output. No result changes. The commit is amine23012004's, kept as written and with them as its author (their branch `test-d16-models-cache`, 9a7128947), rebased on current `main`. It adds a test that the cache hands each call its own dict. `tests/semantic/test_buffer_load_format.py` and `tools/rdna2spirv.py` only name the probe in comments; nothing else imports it except `tools/d16_image_probe.py` and the two d16 test files. This PR touches the same two files as #1664; the two merge without conflict (`git merge-tree` of both heads). ### Tested On `main` 2e2c51c09 + this commit, Windows 11 (26300), Python 3.12.10, i9-13900K, timed while a full `make_game.py` build was using the CPU, so the absolute times are noisy: - `python -I tests/test_d16_format_probe.py`: 26.6 s before, 12.0 s after (12.4 s after the rebase); 24 tests OK (2 skipped, GPU). - `python -I tests/test_d16_image_probe.py`: 22.6 s before, 17.0 s after; OK (2 skipped). - `python -I tools/d16_format_probe.py --self-test`: self-test ok. - `python -I tests/test_image_d16_probe.py`: 22 tests OK (2 skipped). - `python -m unittest discover -s tests/semantic -p test_buffer_load_format.py`: 34 tests OK. - `python -m compileall -q make_game.py tools tests`, `python -I make_game.py --help`, `python -I tests/test_text_write_line_endings.py` (3 OK), `python -I scripts/check_no_game_data.py` on both changed files, `git diff --check`: clean. amine23012004 measured 26.7 s -> 7.7 s and 21.8 s -> 13.1 s on an idle machine. GPUs tested: n/a (CPU-only change; the probe's GPU kernels are untouched) AI-assisted: yes (Claude Code) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (No translator rule here.) 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#2379
No description provided.