Commit 9c41215
authored
Optimize Memory.set_messages
The optimization replaces the call to `encoded_tokens_len(message["content"])` with an inlined approximation `len(message["content"]) // 4` in the `get_total_tokens()` method.
**What changed:**
- Instead of calling `encoded_tokens_len()` for each message (which multiplies `len(s)` by 0.25 and converts to int), the code now directly computes `len(message["content"]) // 4` inline.
**Why it's faster:**
1. **Eliminates function call overhead**: Each call to `encoded_tokens_len()` incurs Python function call overhead (argument passing, stack frame creation, return). With hundreds of messages, these microseconds accumulate significantly. The line profiler shows `get_total_tokens()` dropping from 5.06ms to 2.32ms—a 54% reduction.
2. **Avoids floating-point arithmetic**: The original code multiplies by 0.25 (float) then converts to int. The optimized version uses integer division (`// 4`), which is faster as it stays in integer arithmetic throughout.
3. **Better CPU cache locality**: Inlining keeps the hot loop tighter, improving instruction cache utilization during the list comprehension.
**Impact on workloads:**
The annotated tests show consistent speedups across all scenarios:
- Small workloads (single message): ~25-30% faster
- Medium workloads (100 messages): ~85-93% faster
- Large workloads (1000 messages): ~106-110% faster
The speedup scales linearly with message count because the optimization eliminates per-message overhead. Functions that process many messages (batch operations, conversation histories) benefit most. Since `set_messages()` calls `get_total_tokens()` to check against `max_tokens`, any code path that validates message lists sees this improvement.
**Test case performance:**
- Best for large-scale scenarios (500-1000 messages): 85-110% speedup
- Good for typical workloads (10-100 messages): 30-93% speedup
- Minimal but positive impact on edge cases (empty lists, single messages): 2-34% speedup
The optimization maintains identical behavior—both `int(len(s) * 0.25)` and `len(s) // 4` produce the same token approximation for all string lengths.1 parent c18b790 commit 9c41215
1 file changed
Lines changed: 1 addition & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | | - | |
7 | 5 | | |
8 | 6 | | |
9 | 7 | | |
| |||
44 | 42 | | |
45 | 43 | | |
46 | 44 | | |
47 | | - | |
| 45 | + | |
0 commit comments