Skip to content

fix(rlm): honest retained-bytes accounting for eager documents - #111

Merged
mahmoudimus merged 1 commit into
mainfrom
fix/rlm-honest-accounting
Jul 20, 2026
Merged

mahmoudimus merged 1 commit into
mainfrom
fix/rlm-honest-accounting

Conversation

@mahmoudimus

Copy link
Copy Markdown
Owner

What

The eager _Document mode (files ≤ rlm.max_document_mb) retains text, lines = text.split("\n"), and line_starts — but the DocumentStore LRU accounted its retained bytes as sys.getsizeof(text) * 2, which ignores per-line string object overhead, the list objects, and (as it turns out) line_starts being a plain list[int] with per-int object overhead. On short-lined content the real retention is several times the accounted value (11.7× on the test input; a live capture showed ~4.7GB of split-line strings on a store that believed it was within budget), so rlm.store_budget_mb under-evicted and the budget lied.

Changes

  • src/simba/rlm/context.py — eager docs compute an honest retained-bytes sum once at ingest (O(n_lines)): getsizeof(text) + getsizeof(lines) + Σ getsizeof(line) + getsizeof(line_starts) + Σ getsizeof(n). Lazy-mode accounting (packed array('Q'), no text) was verified already honest and left untouched. Docstrings updated from the old "~2× text size" description.
  • tests/rlm/test_context.py — red-first: an accounting-honesty test (50k×3-char lines; old estimate 400,080 B vs true 4,689,592 B) and an eviction test where the old estimate admitted two docs but honest accounting forces the first to demote to lazy.

Notes

No new config; this makes the existing rlm.store_budget_mb knob enforce what it claims.

The eager _Document.retained_bytes() estimated retention as
sys.getsizeof(text) * 2, which ignores the per-line string object
overhead in .lines and the per-int object overhead in .line_starts.
Both scale with line count, not text size, so many-short-line
documents under-counted real retention 3-5x in production (a live
daemon capture showed ~4.7GB of split-line strings on a store that
believed it was within budget). Result: the DocumentStore LRU
under-evicted and the store_budget_mb cap lied.

Replace the flat estimate with an honest sum computed once at ingest:
text + (.lines list + every line's sizeof) + (.line_starts list +
every int's sizeof). Lazy-mode accounting (packed offset-index
arrays, no text) was already honest and is unchanged.
@mahmoudimus
mahmoudimus merged commit a911095 into main Jul 20, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant