mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-22 15:42:25 +02:00
Two optimizations for the hot path in add_messages, which is called on every write to a messages channel: 1. Skip conversion of left: when left is already list[BaseMessage] with IDs assigned (true for every call after the first), skip convert_to_messages + message_chunk_to_message + the ID-None loop. These are O(n) no-ops on already-resolved messages that allocate two intermediate lists. 2. Pure-append short-circuit: when right contains no RemoveMessage and no ID overlaps with left, return left + right directly. Replaces the O(n) copy + dict build + filter with a single set-membership check. Benchmarks (median of 2000 iterations, pure-append scenario): 10-msg thread: 2.9x faster 100-msg thread: 6.6x faster 1000-msg thread: 7.3x faster 200-step simulation (2 msgs/step): 3.4x faster end-to-end Also adds tests/test_add_messages_benchmark.py with correctness tests for all scenarios (append, update, remove) and a runnable benchmark. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>