-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Self-organizing hash table to improve the performance of bucket aggregations #7652
Self-organizing hash table to improve the performance of bucket aggregations #7652
Conversation
Gradle Check (Jenkins) Run Completed with:
|
a222e8b
to
f59172f
Compare
Gradle Check (Jenkins) Run Completed with:
|
cfb9ac8
to
b18dfcb
Compare
Gradle Check (Jenkins) Run Completed with:
|
Codecov Report
@@ Coverage Diff @@
## main #7652 +/- ##
==========================================
Coverage 70.96% 70.96%
- Complexity 56780 56790 +10
==========================================
Files 4733 4735 +2
Lines 268140 268281 +141
Branches 39287 39308 +21
==========================================
+ Hits 190285 190387 +102
- Misses 61816 61853 +37
- Partials 16039 16041 +2
|
Gradle Check (Jenkins) Run Completed with:
|
Gradle Check (Jenkins) Run Completed with:
|
Unrelated to this PR. This flaky-test is being fixed in #8063. Codecov/patch failed as it's expecting test coverage in the benchmark file. This should probably be excluded. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me! Thanks for introducing ReorganizingLongHash
.
Hi @dblock, can you check the latest revision and comments? |
I'm good with this. There's a weird changelog conflict, care to resolve? I tried to use the GitHub UI and I didn't like its suggestion that had nothing to do with this PR :) |
…ggregations Signed-off-by: Ketan Verma <[email protected]>
…ed in the hash table itself Signed-off-by: Ketan Verma <[email protected]>
Signed-off-by: Ketan Verma <[email protected]>
…initial capacity Signed-off-by: Ketan Verma <[email protected]>
bc6f294
to
e3d3915
Compare
Gradle Check (Jenkins) Run Completed with:
|
@dblock Updated! |
Good work. Watch the backport to 2.x, might need some manual work. |
The backport to
To backport manually, run these commands in your terminal: # Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add ../.worktrees/backport-2.x 2.x
# Navigate to the new working tree
pushd ../.worktrees/backport-2.x
# Create a new branch
git switch --create backport/backport-7652-to-2.x
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 b7cace575c84d94b49c02cd8328835d3f8b1a0d0
# Push it to GitHub
git push --set-upstream origin backport/backport-7652-to-2.x
# Go back to the original working tree
popd
# Delete the working tree
git worktree remove ../.worktrees/backport-2.x Then, create a pull request where the |
@ketanv3 want to backport manually to 2.x? |
@dblock Sure! Working on it. |
…gations (opensearch-project#7652) * Add self-organizing hash table to improve the performance of bucket aggregations Signed-off-by: Ketan Verma <[email protected]> * Updated approach: PSL, fingerprint and recency information are embedded in the hash table itself Signed-off-by: Ketan Verma <[email protected]> * Updated tests and added microbenchmarks Signed-off-by: Ketan Verma <[email protected]> * Renamed FastLongHash to ReorganizingLongHash and updated the default initial capacity Signed-off-by: Ketan Verma <[email protected]> --------- Signed-off-by: Ketan Verma <[email protected]>
…gations (opensearch-project#7652) * Add self-organizing hash table to improve the performance of bucket aggregations Signed-off-by: Ketan Verma <[email protected]> * Updated approach: PSL, fingerprint and recency information are embedded in the hash table itself Signed-off-by: Ketan Verma <[email protected]> * Updated tests and added microbenchmarks Signed-off-by: Ketan Verma <[email protected]> * Renamed FastLongHash to ReorganizingLongHash and updated the default initial capacity Signed-off-by: Ketan Verma <[email protected]> --------- Signed-off-by: Ketan Verma <[email protected]>
…gations (opensearch-project#7652) * Add self-organizing hash table to improve the performance of bucket aggregations Signed-off-by: Ketan Verma <[email protected]> * Updated approach: PSL, fingerprint and recency information are embedded in the hash table itself Signed-off-by: Ketan Verma <[email protected]> * Updated tests and added microbenchmarks Signed-off-by: Ketan Verma <[email protected]> * Renamed FastLongHash to ReorganizingLongHash and updated the default initial capacity Signed-off-by: Ketan Verma <[email protected]> --------- Signed-off-by: Ketan Verma <[email protected]>
…gations (opensearch-project#7652) * Add self-organizing hash table to improve the performance of bucket aggregations Signed-off-by: Ketan Verma <[email protected]> * Updated approach: PSL, fingerprint and recency information are embedded in the hash table itself Signed-off-by: Ketan Verma <[email protected]> * Updated tests and added microbenchmarks Signed-off-by: Ketan Verma <[email protected]> * Renamed FastLongHash to ReorganizingLongHash and updated the default initial capacity Signed-off-by: Ketan Verma <[email protected]> --------- Signed-off-by: Ketan Verma <[email protected]>
…gations (opensearch-project#7652) * Add self-organizing hash table to improve the performance of bucket aggregations Signed-off-by: Ketan Verma <[email protected]> * Updated approach: PSL, fingerprint and recency information are embedded in the hash table itself Signed-off-by: Ketan Verma <[email protected]> * Updated tests and added microbenchmarks Signed-off-by: Ketan Verma <[email protected]> * Renamed FastLongHash to ReorganizingLongHash and updated the default initial capacity Signed-off-by: Ketan Verma <[email protected]> --------- Signed-off-by: Ketan Verma <[email protected]>
…gations (#7652) (#8337) * Add self-organizing hash table to improve the performance of bucket aggregations * Updated approach: PSL, fingerprint and recency information are embedded in the hash table itself * Updated tests and added microbenchmarks * Renamed FastLongHash to ReorganizingLongHash and updated the default initial capacity --------- Signed-off-by: Ketan Verma <[email protected]>
…gations (opensearch-project#7652) * Add self-organizing hash table to improve the performance of bucket aggregations Signed-off-by: Ketan Verma <[email protected]> * Updated approach: PSL, fingerprint and recency information are embedded in the hash table itself Signed-off-by: Ketan Verma <[email protected]> * Updated tests and added microbenchmarks Signed-off-by: Ketan Verma <[email protected]> * Renamed FastLongHash to ReorganizingLongHash and updated the default initial capacity Signed-off-by: Ketan Verma <[email protected]> --------- Signed-off-by: Ketan Verma <[email protected]>
…gations (opensearch-project#7652) * Add self-organizing hash table to improve the performance of bucket aggregations Signed-off-by: Ketan Verma <[email protected]> * Updated approach: PSL, fingerprint and recency information are embedded in the hash table itself Signed-off-by: Ketan Verma <[email protected]> * Updated tests and added microbenchmarks Signed-off-by: Ketan Verma <[email protected]> * Renamed FastLongHash to ReorganizingLongHash and updated the default initial capacity Signed-off-by: Ketan Verma <[email protected]> --------- Signed-off-by: Ketan Verma <[email protected]> Signed-off-by: Shivansh Arora <[email protected]>
Description
Some aggregations know ahead of time about the number of buckets and owning ordinals (eg. range aggregations), thus, are able to map densely and directly. Most other aggregations cannot predict this, thus, rely on a hash table lookup to identify the owning bucket ordinal.
Aggregations in the "nyc_taxis" and the "http_logs" workloads revealed that a major share of the CPU time is spent in these lookups. As these lookups are performed for each document matched by the query, even a small improvement in the hash table can have a large overall improvement.
Latest Approach:
ReorganizingLongHash
This PR introduces
ReorganizingLongHash
which is a drop-in replacement forLongHash
. It organizes itself by moving keys around dynamically in order to reduce the longest probe sequence length (PSL), which makes lookups faster as keys are likely to be found in the same CPU cache line. It also optimizes lookups for recently added keys, making it useful for aggregations where keys are correlated across consecutive hits.Probe Sequence Length
It is the number of probes needed to find the desired key in the hash table.
The average PSL is nearly identical. This is expected as the sum of PSLs is preserved after keys are rearranged.
The longest PSL is reduced significantly, thus improving the worst-case performance of certain keys.
Memory Usage
The memory usage is identical. This is expected as the initial capacity, growth rate, and the underlying table implementation remains the same. Only the encoding and placement strategy has changed.
JMH Benchmarks
Uncorrelated Adds
This represents workloads where keys arrive in random order. Performance is similar on small number of keys, but gets better with large number of keys, mainly due to the reduced number of memory lookups.
Correlated Adds
This represents workloads where adjacent keys are likely to be similar (eg. bucketing on timestamp where two consecutive hits are likely to share the same "hour"). Performance is significantly better as keys are likely to be found at their ideal home slot.
Distinct Adds
This represents workloads with high cardinality, i.e., each hit maps to a different key. Performance gets better due to the reduced number of memory lookups.
OSB Benchmarks
nyc_taxis
There is around 15-23% improvement on Intel and 8-12% on Graviton.
c6i.2xlarge
c6g.2xlarge
http_logs
There is around 23% improvement on Intel and 11% on Graviton.
c6i.2xlarge
c6g.2xlarge
Previous Approach:
LongRHHash
This PR introduces
LongRHHash
which is a drop-in replacement forLongHash
. It combines ideas from Robin Hood hashing and Cuckoo hashing to improve performance. Here's a summary:Probe Sequence Length
It is the number of probes needed to find the desired key in the hash table.
The average PSL is nearly identical. This is expected as the sum of PSLs is preserved after keys are rearranged.
The longest PSL is reduced significantly, thus improving the worst-case performance of certain keys.
JMH Benchmarks
These benchmarks were performed on c6i.2xlarge (Intel) / c6g.2xlarge (Graviton) instances running OpenJDK 17.0.6. These results represent the time to perform 16.7M "adds" with varying number of distinct buckets.
Uncorrelated Adds
This represents workloads where keys arrive in random order. "Recent" lookups are ineffective for large hash tables, so the improvement solely comes from the rearrangement of keys.
(A) is the baseline, (B) is the contender without "recent" lookups, and (C) is the contender with "recent" lookups.
Correlated Adds
This represents workloads where adjacent keys are likely to be similar (eg. bucketing on timestamp where two consecutive hits are likely to share the same "hour").
(A) is the baseline, (B) is the contender with "recent" lookups. Performance is consistent (no sawtooth pattern) and keeps improving as the number of buckets grow. This is because, with two independent hash functions, it is unlikely that both collide.
OSB Benchmarks
nyc_taxis
There is 14-17% improvement on Intel and 8-12% improvement on Graviton.
c6i.2xlarge details
c6g.2xlarge details
http_logs
There is 25% improvement on Intel and 12% improvement on Graviton.
c6i.2xlarge details
c6g.2xlarge details
Check List
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.