Skip to content

perf: rewrite expected_event internals using vectors#641

Closed
yihui wants to merge 1 commit into
mainfrom
perf/expected-event-vectors
Closed

perf: rewrite expected_event internals using vectors#641
yihui wants to merge 1 commit into
mainfrom
perf/expected-event-vectors

Conversation

@yihui

@yihui yihui commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Rewrite expected_event() internals using pure vector arithmetic instead of data.frame/merge/order operations
  • Replace stats::stepfun() with stepfun2() (already on main) for rate lookups
  • 8x speedup for this function (3.6ms → 0.43ms per call), ~2.5x for pw_info

Split from #623 to keep the expected_event.R optimization reviewable independently.

Key changes

  • Compute union of enrollment/failure breakpoints directly as vectors
  • All intermediate arithmetic on plain numeric vectors (no data.frame allocation)
  • Only construct data.frame for final output when simple = FALSE

Test plan

  • test-developer-expected_event.R passes
  • test-independent-expected_event.R passes
  • Numerical output identical to baseline

🤖 Generated with Claude Code

…frames

The original expected_event used data.frame(), merge(), and multiple
order() calls for computation on small interval tables. Profiling
showed expected_event accounted for 65% of pw_info time, and
data.frame overhead was 35% of expected_event time.

Rewrite using pure vector operations: compute the union of enrollment
and failure breakpoints directly, use stepfun2 for rate lookups, and
perform all arithmetic on plain numeric vectors. Only construct a
data.frame for the final output when simple=FALSE.

This yields an 8x speedup for expected_event (3.6ms -> 0.43ms per
call) and ~2.5x speedup for pw_info.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@yihui

yihui commented Jun 25, 2026

Copy link
Copy Markdown
Collaborator Author

Closing due to concerns about potential breakage (a huge amount of effort has been made on this expected_evant() function and we'd avoid touching it if possible).

@yihui yihui closed this Jun 25, 2026
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