Skip to content

fix(#412): the native bytes cache keeps no write parameters and no large text - #413

Merged
tae898 merged 1 commit into
mainfrom
fix/412-bytes-cache
Nov 17, 2026
Merged

tae898 merged 1 commit into
mainfrom
fix/412-bytes-cache

Conversation

@tae898

@tae898 tae898 commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Closes #412

Thank you for reading the report so closely. This keeps the speed idea of lib.bc() for the texts that repeat and stops it from keeping the value of a write.

What changed:

  • Database._exec (lazy command path) passes the JSON text of the bound parameters through lib.b(), as the other paths already do, instead of lib.bc(). That text is different on almost every write, so remembering it never helped.
  • lib.bc() stores a text only when it has at most 1024 characters and the cache has fewer than 4096 entries. A longer text is encoded and returned without being hashed or stored. A statement text with a large literal in it is covered by the same bound.
  • The cache can now hold at most 4096 texts of 1024 characters whatever a program writes, a few MiB in the worst case.

How I tested it: the new tests/test_native_bytes_cache.py (native build only, listed in tests/conftest.py) reads the cache content, not the resident set and not a time. It checks that a small text is cached and the hit returns the same bytes object, that the bound is 1024 characters (1025 is not stored), that the entry limit still holds, and that 100 writes of 256 KiB with new content each time (positional and named parameters, in a scratch database) leave only the statement texts in lib._bytes_cache, under 64 KiB in all. On the unfixed code 4 of the 6 tests fail (the database test finds 103 extra keys of about 256 KiB each, the parameter texts), with the fix all 6 pass. I ran them against the 26.12.1.dev0 native wheel (its _native files are the ones on main), unfixed and with the two changed files swapped in, on Python 3.14.

Numbers from the repro of the issue (300 writes of 1 MiB, new content each time, same host and wheel, run once each):

RSS growth lib._bytes_cache
before 631 MiB 307 entries, 600 MiB
after 30 MiB 6 entries, 0 MiB

I also checked the other module-level caches in arcadedb/_native (records._EDGES_SQL, Database._type_kinds, the callback table): they are keyed by labels, type names or ids, not by values, and _EDGES_SQL is already capped at 512 entries, so I left them alone.

The 1024 characters and the 4096 entries are my guess at "small", so please say if you would like a different bound.

🤖 Generated with Claude Code

…rge text

lib.bc() remembered the UTF-8 of every text it saw for the first 4096 distinct texts, and the lazy command path of Database._exec passed the JSON text of the bound parameters through it, so a write of a large property kept the str and the bytes of its value for the life of the process (300 writes of 1 MiB: 696 MiB of growth, 600 MiB of it in the cache).
The parameter text now goes through lib.b() as on the other paths, and bc() stores a text only when it has at most 1024 characters and the cache has fewer than 4096 entries; a longer text is encoded and returned without being hashed or stored.
@tae898 tae898 self-assigned this Nov 17, 2026
@tae898
tae898 merged commit 097436a into main Nov 17, 2026
44 checks passed
@tae898
tae898 deleted the fix/412-bytes-cache branch November 17, 2026 18:44
@github-actions github-actions Bot added this to the 26.12.1 milestone Nov 17, 2026
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.

Native wheel: lib.bc() caches the parameter text of every write, so large property writes keep twice their size in memory

1 participant