instance_lists: count grass and prop instances in bulk (9x / 3.6x, same output) #315

Closed
Andretuta wants to merge 1 commit from upstream-pr/315 into main
Andretuta commented 2026-11-24 03:07:36 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Andretuta on 2026-11-24T03:07:36Z.


Carried over from old PR 10049 by tedin7 in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch old-pr/10049, exactly that PR's last commit. All carried-over PRs are listed in #17.
Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it.
tedin7: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one.


Original description (mentions and #N references are disabled; the numbers refer to the old repository):

@ItsNotRexed: an exe PR, as you asked.

What

tools/instance_lists.py counts prop and grass instances (hundreds of millions over the full corpus) one record at a time in Python. This reads them in bulk instead:

  • grass: each byte field is one strided slice; (NormalX, NormalY) pairs are counted once per distinct pair, ScaleZ == Scale is one XOR of two columns, Scale == 0 and the rotation max are bytes.count / max.
  • props: each 3x4 matrix comes out of one Struct.iter_unpack pass, with local counters instead of eight dict updates per instance.
  • Truncation (record past the end of the buffer, member offset below 0) is computed up front with the same prefix semantics as the old break.

Output is identical. tools/world_build.py and tools/sandbox_scene.py do not read these lists (they take entity placements), so I left them alone; docs/world.md says so and gives the numbers.

Why

A list holds at most 65,535 instances (u16 count), so memory was never the problem: one resource plus its joined streams. Time was: the full corpus is about 225 M grass and 26 M prop instances at roughly 1.4 us and 12 us each in Python.

Tested

Synthetic bytes only, no game file.

  • Synthetic 65,535-instance list, best of 3: grass 0.091 s to 0.010 s (9x), props 0.815 s to 0.224 s (3.6x); the full counts dict is equal to the old module's for both.
  • New tests in tests/test_instance_lists.py: grass and prop counts equal a record-by-record oracle on seeded random data, including NaN/zero matrices and buffers cut mid-list (5 + 3 cases); a speed test needs bulk at least 4x faster than the oracle on near-upright grass normals.
  • Negative control: with the old instance_lists.py the speed test fails (bulk 0.0915 s vs 0.0719 s); with the change all 19 tests pass, and tests/test_instance_lists_schema_gap.py still passes.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5465327499 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/315). Originally posted by [Andretuta](https://github.com/Andretuta) on 2026-11-24T03:07:36Z. --- > Carried over from **old PR 10049** by `tedin7` in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch `old-pr/10049`, exactly that PR's last commit. All carried-over PRs are listed in #17. > Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it. > `tedin7`: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one. --- Original description (mentions and `#N` references are disabled; the numbers refer to the old repository): `@ItsNotRexed`: an exe PR, as you asked. ## What `tools/instance_lists.py` counts prop and grass instances (hundreds of millions over the full corpus) one record at a time in Python. This reads them in bulk instead: - grass: each byte field is one strided slice; (NormalX, NormalY) pairs are counted once per distinct pair, ScaleZ == Scale is one XOR of two columns, Scale == 0 and the rotation max are `bytes.count` / `max`. - props: each 3x4 matrix comes out of one `Struct.iter_unpack` pass, with local counters instead of eight dict updates per instance. - Truncation (record past the end of the buffer, member offset below 0) is computed up front with the same prefix semantics as the old `break`. Output is identical. `tools/world_build.py` and `tools/sandbox_scene.py` do not read these lists (they take entity placements), so I left them alone; `docs/world.md` says so and gives the numbers. ## Why A list holds at most 65,535 instances (u16 count), so memory was never the problem: one resource plus its joined streams. Time was: the full corpus is about 225 M grass and 26 M prop instances at roughly 1.4 us and 12 us each in Python. ## Tested Synthetic bytes only, no game file. - Synthetic 65,535-instance list, best of 3: grass 0.091 s to 0.010 s (9x), props 0.815 s to 0.224 s (3.6x); the full counts dict is equal to the old module's for both. - New tests in `tests/test_instance_lists.py`: grass and prop counts equal a record-by-record oracle on seeded random data, including NaN/zero matrices and buffers cut mid-list (5 + 3 cases); a speed test needs bulk at least 4x faster than the oracle on near-upright grass normals. - Negative control: with the old `instance_lists.py` the speed test fails (bulk 0.0915 s vs 0.0719 s); with the change all 19 tests pass, and `tests/test_instance_lists_schema_gap.py` still passes.
MoonEyes commented 2026-11-28 07:08:18 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T07:08:18Z.


Thanks a lot for this, @Andretuta. We checked and this pull request is a draft for the same change as #1161 (same title subject, same three files), which is claimed and ready; the kept one is #1161. To keep the queue clear and avoid reviewing the same change twice, I am closing this one in favour of #1161. If you think this is a mistake, please reopen it and let us know.

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7235740281 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/315#issuecomment-7235740281). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T07:08:18Z. --- Thanks a lot for this, @Andretuta. We checked and this pull request is a draft for the same change as #1161 (same title subject, same three files), which is claimed and ready; the kept one is #1161. To keep the queue clear and avoid reviewing the same change twice, I am closing this one in favour of #1161. If you think this is a mistake, please reopen it and let us know. AI-assisted (Claude Code, Claude Opus 5.5).

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!315
No description provided.