Skip to content

Conversation

@trieloff
Copy link
Contributor

No description provided.

Copilot AI review requested due to automatic review settings December 10, 2025 12:25
@github-actions
Copy link

This PR will trigger a patch release when merged.

@codecov
Copy link

codecov bot commented Dec 10, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds performance optimization to the Aggregate class by caching sorted values for percentile and median calculations. Instead of sorting the values array on every percentile call, the array is sorted once in-place and a _sorted flag tracks whether sorting has occurred.

Key changes:

  • Added _sorted flag to track whether values array has been sorted
  • Modified percentile() method to sort values array only once on first call
  • Subsequent percentile/median calls reuse the cached sorted array

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 220 to 224
if (!this._sorted) {
// Sort once in place; all other metrics are order‑insensitive
this.values.sort((left, right) => left - right);
this._sorted = true;
}
Copy link

Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

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

The caching logic is fragile and could lead to incorrect results if values are added after percentile() is called. The _sorted flag is never reset when new values are pushed to this.values (e.g., via aggregateFn at line 92).

Consider either:

  1. Making the values array immutable after first access (document this invariant)
  2. Invalidating _sorted when values are added (requires adding a setter/method for adding values)
  3. Removing the cache and accepting the performance cost of sorting on each call

While the current usage pattern (build once via reduce, then query) appears safe, this implementation is error-prone for future modifications.

Copilot uses AI. Check for mistakes.
Comment on lines 220 to 226
if (!this._sorted) {
// Sort once in place; all other metrics are order‑insensitive
this.values.sort((left, right) => left - right);
this._sorted = true;
}
const index = Math.floor((p / 100) * this.values.length);
return this.values[index];
Copy link

Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

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

The new caching behavior for percentile calculations lacks explicit test coverage. While existing tests call percentile() multiple times, there's no test that verifies:

  1. The cache is properly set after the first call
  2. Subsequent calls use the cached sorted array (performance test)
  3. The invariant that values should not be added after percentile is called

Consider adding a test that verifies multiple percentile calls return consistent results and that the array is only sorted once.

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <[email protected]>
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