perf: replace stats::stepfun with stepfun2 in expected_event#642
Open
yihui wants to merge 4 commits into
Open
Conversation
…_event stats::stepfun has high creation overhead (~57x slower than stepfun2). In expected_event, step functions are created per call and evaluated on small vectors (3-5 elements), making creation cost dominant. Additionally, improve stepfun2 eval speed for small breakpoint vectors (n <= 10) by replacing findInterval with a for-loop of vectorized comparisons (x >= b). This avoids findInterval's dispatch overhead and binary search, which are overkill for 3-4 breakpoints. Net speedup for the step-function portion of expected_event: ~21x vs stats::stepfun, ~2x vs the previous findInterval-based stepfun2. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
jdblischak
approved these changes
Jun 23, 2026
yihui
commented
Jun 25, 2026
yihui
left a comment
Collaborator
Author
There was a problem hiding this comment.
@LittleBeannie I'm very confident that the new stepfun2() can safely replace stats::stepfun(), but please feel free to close the PR if you are not comfortable with changes in expected_event().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
stats::stepfun()calls withstepfun2()inexpected_event()stepfun2()itself: use a for-loop of vectorized comparisons (x >= b) for small breakpoint vectors (n <= 10), falling back tofindIntervalfor larger onesWhy
stats::stepfun()is 57x slower to create thanstepfun2()due to S3 class setup, environment construction, and method registration. Inexpected_event, step functions are created fresh on every call and evaluated once on small vectors (3-5 elements) — creation cost dominates.The loop approach avoids
findInterval's dispatch overhead and binary search, which are overkill for 3-4 breakpoints. Each iteration is a single vectorized primitive (x >= b), with no allocation.Benchmark (the 4-stepfun create+eval pattern in expected_event, 50k calls)
stats::stepfunstats::stepfunstepfun2(old,findInterval)stepfun2(new, loop for n<=10)Test plan
test-developer-expected_event.Rpassestest-independent-expected_event.Rpassesstepfun2produces identical results tostats::stepfunfor bothright = FALSEandright = TRUE🤖 Generated with Claude Code