Skip to content

Optimize matrix_stats agg by using primitive arrays instead of maps#19989

Merged
jainankitk merged 12 commits intoopensearch-project:mainfrom
peteralfonsi:matrix-stats-clean
Nov 19, 2025
Merged

Optimize matrix_stats agg by using primitive arrays instead of maps#19989
jainankitk merged 12 commits intoopensearch-project:mainfrom
peteralfonsi:matrix-stats-clean

Conversation

@peteralfonsi
Copy link
Copy Markdown
Contributor

Description

As described in the initial issue, the use of Map<String, Double/Long> in the hot loop of matrix_stats is slow mostly because of un/boxing. Replacing it with double[] / long[] speeds up the aggregation by 80%, from 15.639 seconds to 3.093 seconds on nyc_taxis.

As discussed with @jainankitk , we unfortunately do have to keep the older map-based logic around for BWC/serialization reasons. I wasn't able to think of a good way to remove it - while it is true that we'll never add RunningStats that are for different aggregation requests, and therefore the fieldNames keys will actually always be the same in practice, we still do need a RunningStats instance constructed with the StreamInput ctor to be functional, even before we add it to the RunningStats from another segment/shard. So we have to keep the old code around as a fallback. Sad since it adds a lot of bloat + logic around switching between the two. However I still think the performance improvement makes the overall change worthwhile.

Note we still use map-based logic in the other objects like InternalMatrixStats/MatrixStatsResults. This should have ~no perf impact since it only happens at the end, not in the per-doc hot loop. Doing it this way reduces the complexity of having multiple cases for those objects too.

Related Issues

Resolves #19741

Check List

  • Functionality includes testing.
  • [N/A] API changes companion pull request created, if applicable.
  • [N/A] Public documentation issue/PR created, if applicable.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Peter Alfonsi added 4 commits November 12, 2025 16:06
Signed-off-by: Peter Alfonsi <petealft@amazon.com>
Signed-off-by: Peter Alfonsi <petealft@amazon.com>
UT
Signed-off-by: Peter Alfonsi <petealft@amazon.com>
Signed-off-by: Peter Alfonsi <petealft@amazon.com>
@peteralfonsi peteralfonsi requested a review from a team as a code owner November 13, 2025 22:34
@github-actions github-actions bot added enhancement Enhancement or improvement to existing feature or request Search:Performance labels Nov 13, 2025
Peter Alfonsi and others added 2 commits November 13, 2025 14:35
Signed-off-by: Peter Alfonsi <petealft@amazon.com>
Signed-off-by: Peter Alfonsi <peter.alfonsi@gmail.com>
@github-actions
Copy link
Copy Markdown
Contributor

❌ Gradle check result for 7cd07d7: FAILURE

Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change?

Signed-off-by: Peter Alfonsi <petealft@amazon.com>
@github-actions
Copy link
Copy Markdown
Contributor

❌ Gradle check result for 3dcc988: FAILURE

Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change?

@peteralfonsi
Copy link
Copy Markdown
Contributor Author

distribution:bwc:maintenance:buildBwcLinuxTar is failing for 2.19.4 but not 2.19.3. I think this isn't related to serialization BWC test failures or anything like that since I think those show up as test failures. Not sure why this is happening.

@github-actions
Copy link
Copy Markdown
Contributor

❌ Gradle check result for cded794: FAILURE

Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change?

@peteralfonsi peteralfonsi marked this pull request as draft November 14, 2025 20:32
Signed-off-by: Peter Alfonsi <petealft@amazon.com>
@github-actions
Copy link
Copy Markdown
Contributor

❌ Gradle check result for 0fba995: null

Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change?

Signed-off-by: Peter Alfonsi <petealft@amazon.com>
@github-actions
Copy link
Copy Markdown
Contributor

❌ Gradle check result for 9593a6c: FAILURE

Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change?

Signed-off-by: Peter Alfonsi <petealft@amazon.com>
@github-actions
Copy link
Copy Markdown
Contributor

✅ Gradle check result for 5a1a4f6: SUCCESS

@codecov
Copy link
Copy Markdown

codecov bot commented Nov 18, 2025

Codecov Report

❌ Patch coverage is 83.91608% with 46 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.27%. Comparing base (2f930dc) to head (7810559).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
...search/aggregations/matrix/stats/RunningStats.java 84.25% 5 Missing and 32 partials ⚠️
.../aggregations/matrix/stats/MatrixStatsResults.java 86.48% 2 Missing and 3 partials ⚠️
...aggregations/matrix/stats/InternalMatrixStats.java 60.00% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main   #19989      +/-   ##
============================================
+ Coverage     73.24%   73.27%   +0.03%     
- Complexity    71519    71561      +42     
============================================
  Files          5789     5789              
  Lines        327121   327333     +212     
  Branches      47117    47159      +42     
============================================
+ Hits         239591   239849     +258     
+ Misses        68269    68216      -53     
- Partials      19261    19268       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Signed-off-by: Peter Alfonsi <petealft@amazon.com>
@github-actions
Copy link
Copy Markdown
Contributor

✅ Gradle check result for 7810559: SUCCESS

@jainankitk jainankitk marked this pull request as ready for review November 19, 2025 01:02
Copy link
Copy Markdown
Contributor

@jainankitk jainankitk left a comment

Choose a reason for hiding this comment

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

Change looks good to me. And results in significant speedup!!

@jainankitk jainankitk merged commit 9e04e6c into opensearch-project:main Nov 19, 2025
35 checks passed
rgsriram pushed a commit to rgsriram/OpenSearch that referenced this pull request Dec 5, 2025
…pensearch-project#19989)

Signed-off-by: Peter Alfonsi <petealft@amazon.com>
Signed-off-by: Peter Alfonsi <peter.alfonsi@gmail.com>
Co-authored-by: Peter Alfonsi <petealft@amazon.com>
liuguoqingfz pushed a commit to liuguoqingfz/OpenSearch that referenced this pull request Dec 15, 2025
…pensearch-project#19989)

Signed-off-by: Peter Alfonsi <petealft@amazon.com>
Signed-off-by: Peter Alfonsi <peter.alfonsi@gmail.com>
Co-authored-by: Peter Alfonsi <petealft@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Enhancement or improvement to existing feature or request Search:Performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Remove maps from hot loop in matrix_stats agg for performance

2 participants