Skip to content
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

Interpret byte array as primitive using VarHandles #11362

Merged

Conversation

ketanv3
Copy link
Contributor

@ketanv3 ketanv3 commented Nov 28, 2023

Description

This PR replaces manual conversion of byte[] to long|int|short with VarHandles. This is more readable and performant, since it would ultimately call Unsafe.unalignedAccess() to read contiguous memory.

Performance was already fast, so I don't expect much reduction in latency. But no harm in making it 2.7x faster.
Benchmark code: https://gist.github.com/ketanv3/22fb565dfea619b2307f25b161d2c522

Benchmark                           Mode  Cnt           Score          Error  Units
InterpretBitsBenchmark.manual      thrpt    5   543160590.013 ± 17214484.939  ops/s
InterpretBitsBenchmark.varHandle   thrpt    5  1448471902.400 ± 61356063.495  ops/s

Check List

  • New functionality includes testing.
    • All tests pass
  • New functionality has been documented.
    • New functionality has javadoc added
  • Failing checks are inspected and point to the corresponding known issue(s) (See: Troubleshooting Failing Builds)
  • Commits are signed per the DCO using --signoff
  • Commit changes are listed out in CHANGELOG.md file (See: Changelog)
  • Public documentation issue/PR created

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.

Copy link
Contributor

❌ Gradle check result for 26797f8: 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?

Copy link
Contributor

github-actions bot commented Nov 28, 2023

Compatibility status:

Checks if related components are compatible with change 4f9d188

Incompatible components

Skipped components

Compatible components

Compatible components: [https://github.com/opensearch-project/asynchronous-search.git, https://github.com/opensearch-project/security-analytics.git, https://github.com/opensearch-project/observability.git, https://github.com/opensearch-project/reporting.git, https://github.com/opensearch-project/job-scheduler.git, https://github.com/opensearch-project/opensearch-oci-object-storage.git, https://github.com/opensearch-project/sql.git, https://github.com/opensearch-project/common-utils.git, https://github.com/opensearch-project/custom-codecs.git, https://github.com/opensearch-project/security.git, https://github.com/opensearch-project/ml-commons.git, https://github.com/opensearch-project/anomaly-detection.git, https://github.com/opensearch-project/notifications.git, https://github.com/opensearch-project/index-management.git, https://github.com/opensearch-project/cross-cluster-replication.git, https://github.com/opensearch-project/neural-search.git, https://github.com/opensearch-project/alerting.git, https://github.com/opensearch-project/geospatial.git, https://github.com/opensearch-project/k-nn.git, https://github.com/opensearch-project/performance-analyzer-rca.git, https://github.com/opensearch-project/performance-analyzer.git]

@ketanv3 ketanv3 force-pushed the refactor/interpret-bits-with-vh branch from 26797f8 to 20411ce Compare November 28, 2023 16:15
@ketanv3 ketanv3 changed the title Interpret bits using VarHandles Interpret byte array as primitive using VarHandles Nov 28, 2023
Copy link
Contributor

❌ Gradle check result for 20411ce: 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: Ketan Verma <ketan9495@gmail.com>
@ketanv3 ketanv3 force-pushed the refactor/interpret-bits-with-vh branch from 20411ce to 3487bb6 Compare November 28, 2023 17:00
Copy link
Contributor

✅ Gradle check result for 3487bb6: SUCCESS

Copy link

codecov bot commented Nov 28, 2023

Codecov Report

Attention: 10 lines in your changes are missing coverage. Please review.

Comparison is base (f7f3500) 71.37% compared to head (4f9d188) 71.39%.

Files Patch % Lines
...search/core/common/io/stream/BytesStreamInput.java 40.00% 7 Missing and 2 partials ⚠️
...ch/index/translog/BufferedChecksumStreamInput.java 66.66% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main   #11362      +/-   ##
============================================
+ Coverage     71.37%   71.39%   +0.02%     
- Complexity    59113    59139      +26     
============================================
  Files          4893     4893              
  Lines        277831   277835       +4     
  Branches      40367    40367              
============================================
+ Hits         198288   198374      +86     
+ Misses        63042    63018      -24     
+ Partials      16501    16443      -58     

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

@andrross
Copy link
Member

andrross commented Dec 1, 2023

Any reason not to add InterpretBitsBenchmark to our microbenchmarks in this PR as well?

@ketanv3
Copy link
Contributor Author

ketanv3 commented Dec 2, 2023

Any reason not to add InterpretBitsBenchmark to our microbenchmarks in this PR as well?

@andrross Mainly because the implementation comes straight from JDK and has known performance benefits. Since it's unrelated to OpenSearch anyways and is faster in all situations, adding these benchmarks felt excessive. For analogy, this is like comparing System.arraycopy(...) with naive implementation of copying an array element-wise.

Copy link
Contributor

@backslasht backslasht left a comment

Choose a reason for hiding this comment

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

LGTM!

@andrross
Copy link
Member

andrross commented Dec 5, 2023

Any reason not to add InterpretBitsBenchmark to our microbenchmarks in this PR as well?

@andrross Mainly because the implementation comes straight from JDK and has known performance benefits. Since it's unrelated to OpenSearch anyways and is faster in all situations, adding these benchmarks felt excessive. For analogy, this is like comparing System.arraycopy(...) with naive implementation of copying an array element-wise.

Thanks @ketanv3, that makes sense. Can you rebase this PR then we can get it merged?

Signed-off-by: Ketan Verma <ketan9495@gmail.com>
Copy link
Contributor

github-actions bot commented Dec 5, 2023

❕ Gradle check result for 4f9d188: UNSTABLE

  • TEST FAILURES:
      1 org.opensearch.remotestore.RemoteIndexPrimaryRelocationIT.testPrimaryRelocationWhileIndexing

Please review all flaky tests that succeeded after retry and create an issue if one does not already exist to track the flaky failure.

@reta reta merged commit d875558 into opensearch-project:main Dec 5, 2023
28 of 29 checks passed
@reta reta added the backport 2.x Backport to 2.x branch label Dec 5, 2023
@opensearch-trigger-bot
Copy link
Contributor

The backport to 2.x failed:

The process '/usr/bin/git' failed with exit code 128

To backport manually, run these commands in your terminal:

# Navigate to the root of your repository
cd $(git rev-parse --show-toplevel)
# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add ../.worktrees/OpenSearch/backport-2.x 2.x
# Navigate to the new working tree
pushd ../.worktrees/OpenSearch/backport-2.x
# Create a new branch
git switch --create backport/backport-11362-to-2.x
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 d8755582c76e1b2e297e38db0b2b3f4a7299e3c6
# Push it to GitHub
git push --set-upstream origin backport/backport-11362-to-2.x
# Go back to the original working tree
popd
# Delete the working tree
git worktree remove ../.worktrees/OpenSearch/backport-2.x

Then, create a pull request where the base branch is 2.x and the compare/head branch is backport/backport-11362-to-2.x.

@reta
Copy link
Collaborator

reta commented Dec 5, 2023

@ketanv3 could you please backport to 2.x manually? thank you

ketanv3 added a commit to ketanv3/OpenSearch that referenced this pull request Dec 5, 2023
…t#11362)

* Interpret byte array as primitive using VarHandles

Signed-off-by: Ketan Verma <ketan9495@gmail.com>

* Fixed offset bug

Signed-off-by: Ketan Verma <ketan9495@gmail.com>

---------

Signed-off-by: Ketan Verma <ketan9495@gmail.com>
ketanv3 added a commit to ketanv3/OpenSearch that referenced this pull request Dec 5, 2023
…t#11362)

* Interpret byte array as primitive using VarHandles

Signed-off-by: Ketan Verma <ketan9495@gmail.com>

* Fixed offset bug

Signed-off-by: Ketan Verma <ketan9495@gmail.com>

---------

Signed-off-by: Ketan Verma <ketan9495@gmail.com>
reta pushed a commit that referenced this pull request Dec 5, 2023
* Interpret byte array as primitive using VarHandles



* Fixed offset bug



---------

Signed-off-by: Ketan Verma <ketan9495@gmail.com>
gaobinlong pushed a commit to gaobinlong/OpenSearch that referenced this pull request Dec 7, 2023
…t#11362)

* Interpret byte array as primitive using VarHandles

Signed-off-by: Ketan Verma <ketan9495@gmail.com>

* Fixed offset bug

Signed-off-by: Ketan Verma <ketan9495@gmail.com>

---------

Signed-off-by: Ketan Verma <ketan9495@gmail.com>
deshsidd pushed a commit to deshsidd/OpenSearch that referenced this pull request Dec 11, 2023
…t#11362)

* Interpret byte array as primitive using VarHandles

Signed-off-by: Ketan Verma <ketan9495@gmail.com>

* Fixed offset bug

Signed-off-by: Ketan Verma <ketan9495@gmail.com>

---------

Signed-off-by: Ketan Verma <ketan9495@gmail.com>
rayshrey pushed a commit to rayshrey/OpenSearch that referenced this pull request Mar 18, 2024
…t#11362)

* Interpret byte array as primitive using VarHandles

Signed-off-by: Ketan Verma <ketan9495@gmail.com>

* Fixed offset bug

Signed-off-by: Ketan Verma <ketan9495@gmail.com>

---------

Signed-off-by: Ketan Verma <ketan9495@gmail.com>
shiv0408 pushed a commit to Gaurav614/OpenSearch that referenced this pull request Apr 25, 2024
…t#11362)

* Interpret byte array as primitive using VarHandles

Signed-off-by: Ketan Verma <ketan9495@gmail.com>

* Fixed offset bug

Signed-off-by: Ketan Verma <ketan9495@gmail.com>

---------

Signed-off-by: Ketan Verma <ketan9495@gmail.com>
Signed-off-by: Shivansh Arora <hishiv@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 2.x Backport to 2.x branch backport-failed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants