Skip to content

Commit c784128

Browse files
committed
perf(ltm): use len(s) instead of len(s.encode()) in trim loop
Avoid allocating a new bytes object for every string when calculating buffer size in _trim_raw_records. Character count is sufficient for the approximate memory cap.
1 parent 4f927cd commit c784128

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

astrbot/builtin_stars/astrbot/long_term_memory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,10 +558,10 @@ def _trim_raw_records(self, umo: str, max_bytes: int = MAX_RAW_BYTES) -> None:
558558
self._raw_cursor[umo] = cursor
559559

560560
# 2. 按大小继续从前面淘汰(限制极端情况的总内存)
561-
total = sum(len(s.encode()) for s in dq)
561+
total = sum(len(s) for s in dq)
562562
while total > max_bytes and dq:
563563
removed = dq.popleft()
564-
total -= len(removed.encode())
564+
total -= len(removed)
565565
if cursor > 0:
566566
cursor -= 1
567567
self._raw_cursor[umo] = max(0, cursor)

0 commit comments

Comments
 (0)