Skip to content

Avoid double-counting remainders when resampling sessions - #467

Open
FanWu-ai wants to merge 3 commits into
joerick:mainfrom
FanWu-ai:fix-resample-duration-20261011
Open

FanWu-ai wants to merge 3 commits into
joerick:mainfrom
FanWu-ai:fix-resample-duration-20261011

Conversation

@FanWu-ai

@FanWu-ai FanWu-ai commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Resampling double-counts the carried remainder: four 6 ms records at a 10 ms interval report 26 ms from 24 ms of input. Following maintainer feedback, emit whole-interval durations with one accumulator and carry the remainder forward. The final partial interval is intentionally omitted, so this example emits 10 + 10 ms. This replaces the initial exact-duration-preservation approach. Sample-selection positions, inputs and session metadata remain unchanged.

Local validation on CPython 3.12/Linux: all 159 tests across the two required modes (152 ordinary + 7 IPython), all nine pinned hooks, docs, wheel/sdist, strict Twine and whitespace checks pass. Sixteen regression cases cover quantized outputs, zero/empty/short/oversized samples, 2,500 exact-arithmetic prefixes, adjacent floats and bounded cumulative error. Actual HTML output represents 600.0 s from 600.006 s of input, omitting only the final 6 ms. Existing MyST and setuptools license warnings remain.

Hosted run 38227860619 initially passed 21/23 jobs; two unchanged Ubuntu CLI sampling assertions failed. All new resampling tests and Read the Docs passed. The failure cause is not established; no successful rerun is claimed.

AI assistance: OpenAI Codex was used to investigate, implement and test this revision. No human review of the revision is claimed.

AI assistance: OpenAI Codex was used for investigation, implementation, regression tests, and automated validation.
@FanWu-ai
FanWu-ai marked this pull request as ready for review October 11, 2026 17:43
Comment thread pyinstrument/session.py Outdated
Comment on lines +226 to +240
# Time carried across interval boundaries has already been recorded.
pending_time = 0.0

for frame_info_stack, time in frame_records:
accumulated_time += time
pending_time += time

if accumulated_time >= interval:
result.append((frame_info_stack, accumulated_time))
result.append((frame_info_stack, pending_time))
pending_time = 0.0
accumulated_time = accumulated_time % interval

if pending_time:
result.append((frame_records[-1][0], pending_time))

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I agree that it looks like the remainder is being double-counted here. I think I'd prefer if the time persisted in result was the floor-rounded accumulated time rather than a value that looks precise but is 'lying' about its precision. also it would use one less local var.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated in f3bf2e7 to floor-rounded whole intervals with one accumulator. The remainder is carried forward and the final partial interval is omitted, matching the requested precision. Regression tests now assert quantization and bounded total error. All 159 local tests and nine hooks pass; hosted CI initially passed 21/23 jobs, with two unchanged CLI sampling failures.

Follow maintainer feedback: emit only whole sampling intervals with one accumulator, carry the remainder forward, and omit a final partial interval. Replace exact-preservation assertions with quantization and bounded-error regressions.

Validated on CPython 3.12/Linux: all 159 tests across both required modes; nine pinned hooks; docs; wheel and sdist; strict Twine checks. AI assistance: OpenAI Codex.
@FanWu-ai FanWu-ai changed the title Preserve recorded duration when resampling sessions Avoid double-counting remainders when resampling sessions Oct 12, 2026

@joerick joerick left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

just one small comment

Comment thread pyinstrument/session.py Outdated
Comment on lines +223 to +224
Durations are rounded down to whole intervals, carrying the remainder
forward. A final partial interval is discarded.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

this would be better as a comment rather than a docstring.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved the explanation from the docstring to a comment above the resampling loop in 7fcb9ea. All 159 local tests across the two required test modes and all nine hooks pass.

This branch has not been deployed

No deployments
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.

2 participants