1 Commits

Author SHA1 Message Date
Google Team Member 400f512d81 perf: avoid deepcopy of session contents when building LLM requests
`_get_contents` deep-copied `event.content` for every event on every LLM
request, just to strip ADK-generated (`adk-` prefixed) function call/response
ids and to isolate the contents from downstream request processors that mutate
parts in place (e.g. nl_planning clearing `part.thought`, code_execution
rewriting parts). The deepcopy recursed into large
`function_call.args`/`inline_data` payloads, and cost grew with conversation
length (a dominant non-LLM CPU sink in profiling, ~4-7s of a ~30s run).

Replace it with a shallow copy: the `Content` and every `Part` are
`model_copy`-d (so downstream in-place mutations stay isolated from session
events), but the payloads (`args`/`response`/`inline_data`/...) are shared by
reference instead of deep-copied.

Adds regression tests (id stripping and downstream-mutation isolation) and a
google_benchmark perf script.

Benchmark (_get_contents over a 500-turn history, ~23x):

Before (copy.deepcopy):
  -------------------------------------------------------
  Benchmark             Time             CPU   Iterations
  -------------------------------------------------------
  get_contents  781559706 ns    781443557 ns            1

After (per-part shallow copy):
  -------------------------------------------------------
  Benchmark             Time             CPU   Iterations
  -------------------------------------------------------
  get_contents   33996069 ns     33987197 ns           20

PiperOrigin-RevId: 940664677
2026-06-30 14:36:32 -07:00