Skip to content

perf: avoid draining the event queue when prepending events - #7053

Open
Alek99 wants to merge 2 commits into
mainfrom
codex/non-regressing-event-path
Open

Alek99 wants to merge 2 commits into
mainfrom
codex/non-regressing-event-path

Conversation

@Alek99

@Alek99 Alek99 commented Sep 6, 2026

Copy link
Copy Markdown
Member

Prepending events currently drains the pending queue with repeated shift() calls, builds a combined array, filters it, and pushes it back. Insert the filtered incoming events with unshift() instead, preserving queue identity and event order while removing the extra traversal and copies.

The ordinary append expression and event-processing condition are unchanged. This change adds no Python backend work or WebSocket protocol changes.

Validation

  • Fourteen behavioral tests load the complete frontend module with Node’s native ES-module loader and explicit dependency stubs. They cover ordering, filtering, prepend, reconnect, async/reentrant dispatch, REST, mismatch handling, and rejection/retry. The original implementation fails the prepend-work regression: five shifts for three dispatches; this implementation uses three. Four loader cases cover normal and compact formatting, function declarations, and nested closures. All 18 cases pass on Node 22 and Node 26; no parser or npm dependency is required.
  • uv run --no-sync pytest tests/units --cov --no-cov-on-fail --cov-report=: 8,298 passed, 18 skipped; 75.95% coverage (72% required).
  • All pre-commit hooks passed: Ruff, codespell, stub generation, Pyright, ty, and Biome. Full pyright reflex tests and repository-wide Ruff lint/format checks also passed.
  • Five paired Chrome 152 trials on an Apple M5 Pro, against c49a85d, showed prepend capacity improvements of 19.2% for 32 incoming + 32 pending events and 18.5% for 2,048 + 2,048. A higher-arrival confirmation reduced measured queue work by 16.2% and median completion p99 from 2.865 to 2.659 ms.

The benchmarks exercise queue processing with counted local callbacks/socket emits, excluding real network traffic and React rendering. Light-load timer results were noisy, including in an identical-source A/A control, so these measurements do not establish exact frontend latency equivalence or overall application speedup.

Submission checklist

  • Followed the contributing guidelines and checked for overlapping open PRs.
  • Added a performance changelog fragment for reflex-base.
  • Added behavioral and work-count regression coverage.
  • Reviewed the patch adversarially.

@Alek99
Alek99 requested a review from a team as a code owner September 6, 2026 20:30
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@greptile-apps

greptile-apps Bot commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Replaces the prepend queue drain-and-rebuild operation with an in-place filtered unshift, preserving queue identity and event ordering while reducing work. The follow-up changes replace formatting-sensitive source extraction with complete-module evaluation through Node VM modules.

  • Preserves append and event-processing behavior.
  • Adds queue ordering, filtering, reconnect, reentrancy, failure, and work-count regression coverage.
  • Resolves the previous formatting-sensitive test-harness finding.
  • Renames the performance changelog fragment to the PR-numbered form.

Confidence Score: 5/5

The PR appears safe to merge with no outstanding correctness, security, or repository-rule issues.

The in-place prepend preserves filtering and FIFO order, the complete-module harness removes the previously reported formatting dependency, and that prior thread is manually resolved.

Important Files Changed

Filename Overview
packages/reflex-base/src/reflex_base/.templates/web/utils/state.js Uses filtered in-place prepend or append operations without changing dispatch behavior.
tests/units/reflex_base/client_event_queue_runtime.mjs Evaluates the complete frontend module with explicit synthetic dependency and browser stubs.
tests/units/reflex_base/client_event_queue.mjs Adds broad behavioral coverage and verifies that prepending no longer drains pending events.
tests/units/reflex_base/test_client_event_queue.py Runs the Node regression suite with VM-module support and reports subprocess failures.
packages/reflex-base/news/7053.performance.md Adds a correctly named concise performance changelog fragment.

Reviews (2): Last reviewed commit: "test: load the full client state module ..." | Re-trigger Greptile

Comment thread tests/units/reflex_base/client_event_queue_runtime.mjs Outdated
@codspeed

codspeed Bot commented Sep 6, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 32 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing codex/non-regressing-event-path (f15b0ad) with main (c49a85d)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 5 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread packages/reflex-base/news/7053.performance.md
Comment thread tests/units/reflex_base/client_event_queue_runtime.mjs Outdated
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