Skip to content

Conversation

kaivalnp
Copy link
Contributor

@kaivalnp kaivalnp commented Oct 6, 2025

Description

Intermediate cast from int -> float -> int in the byte vector specific cosineBody function(s) loses precision.

This happens for integers >= 2^24, because IEEE 754 single-precision floats use 24 bits to represent the integer part.

For example, when 16777217 is cast to float and back to int, it yields 16777216.

This also avoids CPU cycles related to casting. VectorUtilBenchmark shows minor but consistent speedup:

main

Benchmark                               (size)   Mode  Cnt  Score   Error   Units
VectorUtilBenchmark.binaryCosineVector    1024  thrpt   15  4.782 ± 0.007  ops/us

This PR

Benchmark                               (size)   Mode  Cnt  Score   Error   Units
VectorUtilBenchmark.binaryCosineVector    1024  thrpt   15  4.885 ± 0.002  ops/us

Copy link
Contributor

github-actions bot commented Oct 6, 2025

This PR does not have an entry in lucene/CHANGES.txt. Consider adding one. If the PR doesn't need a changelog entry, then add the skip-changelog label to it and you will stop receiving this reminder on future updates to the PR.

Copy link
Member

@benwtrent benwtrent left a comment

Choose a reason for hiding this comment

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

simplifies things, fine with this :)

@msokolov
Copy link
Contributor

msokolov commented Oct 7, 2025

Let's merge then? and maybe backport?

@msokolov msokolov merged commit 5249ab9 into apache:main Oct 7, 2025
12 checks passed
@kaivalnp kaivalnp deleted the cosine-sig branch October 7, 2025 19:11
kaivalnp pushed a commit to kaivalnp/lucene that referenced this pull request Oct 7, 2025
…tions (backport apache#15293)

Co-authored-by: Kaival Parikh <[email protected]>

(cherry picked from commit 5249ab9)
@kaivalnp
Copy link
Contributor Author

kaivalnp commented Oct 7, 2025

Thanks @msokolov, I opened #15298 to backport

msokolov pushed a commit that referenced this pull request Oct 7, 2025
…tions (backport #15293) (#15298)

(cherry picked from commit 5249ab9)

Co-authored-by: Kaival Parikh <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants