perf(context,memory): binary search truncation and batch fidelity tag updates#4624
Merged
Conversation
…delity tag updates truncate_to_tokens now uses binary search (O(log N) token-count calls) instead of sequential halving, preserving up to 2x more content per Compressed message. Uses usize::midpoint and floor_char_boundary for safe UTF-8 handling; stall guard prevents infinite loops on multibyte chars spanning the midpoint. update_fidelity_tags replaces N sequential UPDATE statements with a single batched CASE WHEN expression per chunk, reducing SQLite round-trips from N to ceil(N/333). MAX_FIDELITY_BATCH=333 keeps bind variable count within SQLITE_MAX_VARIABLE_NUMBER=999 (3 variables per row). A single transaction wraps all chunks for atomicity. Empty-slice early return skips the transaction entirely. Closes #4618 Closes #4619
f935735 to
9413b08
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
truncate_to_tokenswith binary search — preserves up to 2× more content per Compressed message at the same token budgetUPDATE messages SET fidelity_tagcalls with a single batchedCASE WHENexpression — reduces SQLite round-trips from N to ⌈N/333⌉ per scoring passPerformance
Binary search (
fidelity.rs):Batch SQL (
messages/mod.rs):executecalls inside a transaction (N=200 → 200 awaits)executecalls (N=200 → 1 await; N=500 → 2 awaits)MAX_FIDELITY_BATCH=333keeps bind variable count withinSQLITE_MAX_VARIABLE_NUMBER=999(3 vars/row: 2 for CASE arm + 1 for IN list)Test plan
truncate_to_tokens: no-op, exact limit, minimal truncation, 90%-preservation, empty string, max_tokens=0, multibyte UTF-8 boundaryupdate_fidelity_tags: single-item and multi-item batchMAX_FIDELITY_BATCHconstantCloses #4618
Closes #4619