Skip to content

perf: O(N²) total cost per conversation from full-history re-scan every step #3134

Closed

Description

@csmith49

Problem

Every call to Agent.step() does 4 full O(N) passes over the entire event history, where N is the total number of events ever produced:

  1. ConversationState.get_unmatched_actions() — reverse iteration over all events (state.py:504)
  2. View.from_events() — forward iteration, deserializing every event (view.py:126)
  3. enforce_properties — BatchAtomicityProperty scans all events (batch_atomicity.py:24)
  4. enforce_properties — ToolLoopAtomicityProperty scans all events (tool_loop_atomicity.py:60)

Each pass involves Pydantic model_validate_json deserialization with an uncached class hierarchy walk (get_known_concrete_subclasses). Nothing is cached between steps.

Evidence

2000-step conversation: step time grows from 36ms (step 1) to 331ms (step 2000) — a 9.1× slowdown. Total conversation time: 367 seconds. At 100 steps the early-to-late slowdown is 7.7×. The _validate_subtype + get_known_concrete_subclasses family accounts for ~47% of per-step self-time in the 100-step wall profile.

Severity: Critical

Impact of fix

Per-step cost becomes O(1) amortized instead of O(N). A 2000-step conversation that currently takes 367s would take ~5-10s. Long conversations (thousands of steps) become viable at consistent speed.

Suggested fixes

  • Cache deserialized Event objects in EventLog (keyed by event ID) so re-iteration doesn't re-deserialize. Medium effort, large impact.
  • Make View.from_events() incremental — append new events to the existing view instead of rebuilding from scratch. High effort, eliminates the O(N).
  • Cache get_known_concrete_subclasses() result (the class hierarchy is static after import). Low effort, eliminates ~47% of per-step time.

Related issues and PRs

Discovered during profiling investigation (austin wall-time profiler), May 2026. openhands-sdk v1.19.1.

Activity

  1. added
    bug
    performance
    on May 8, 2026
  2. github-actions commented on Jun 18, 2026

    @github-actions
    Contributor

    This issue is stale because it has been open for 40 days with no activity. Remove the stale label or leave a comment, otherwise it will be closed in 10 days.

  3. VascoSch92 commented on Jun 22, 2026

    @VascoSch92
    Member

    Hey @csmith49

    I'm closing the issue as it seems complete. (both fixes were merged)

    Feel free to re-open if it is the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugperformance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions