Skip to content

perf(session): reduce allocations in InMemoryService.AppendEvent#1147

Open
huntiezz wants to merge 1 commit into
google:mainfrom
huntiezz:perf/session-append-event-clone
Open

perf(session): reduce allocations in InMemoryService.AppendEvent#1147
huntiezz wants to merge 1 commit into
google:mainfrom
huntiezz:perf/session-append-event-clone

Conversation

@huntiezz

Copy link
Copy Markdown

Summary

  • Add cloneEventForStorage() to deep-clone event maps and slices only when non-empty
  • Trim temporary state keys before persisting events to the canonical in-memory session store

Motivation

InMemoryService.AppendEvent runs on every non-partial LLM/tool event in the default in-memory runner path. It previously called maps.Clone / slices.Clone on every action field even when empty, causing unnecessary heap allocations on the hot path.

This also ensures events retrieved via Get have temporary state keys stripped (matching the runner-held session copy).

Testing Plan

  • go test -race -count=1 ./session/...
  • Extended TestInMemoryService_AppendEvent_PreservesInputEventTempState to verify Get returns stripped temp keys
  • Added BenchmarkInMemoryService_AppendEvent

@google-cla

google-cla Bot commented Jul 10, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@huntiezz
huntiezz force-pushed the perf/session-append-event-clone branch 2 times, most recently from 8678e57 to 56fdcd6 Compare July 10, 2026 18:34
Deep-clone event action maps and slices only when non-empty, and trim
temporary state keys before persisting to the canonical session store.
Reuse the trimmed event from appendEvent to avoid duplicate work.
@huntiezz
huntiezz force-pushed the perf/session-append-event-clone branch from 56fdcd6 to 582c5e0 Compare July 10, 2026 18:34
@huntiezz

Copy link
Copy Markdown
Author

Can someone review this?

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