Skip to content

Conversation

@iverase
Copy link
Contributor

@iverase iverase commented Aug 14, 2025

After #132820, the method BQVectorUtils#packAsBinary can be easily vectorize by using a helper array that defines the shifts we need to apply to the vector elements.

For 128 bits in my mac, the improvements are almost 2x:

Benchmark                                 (dims)   Mode  Cnt   Score   Error   Units
PackAsBinaryBenchmark.packAsBinary           384  thrpt    5  16.493 ± 0.253  ops/ms
PackAsBinaryBenchmark.packAsBinary           782  thrpt    5   8.193 ± 0.043  ops/ms
PackAsBinaryBenchmark.packAsBinary          1024  thrpt    5   6.461 ± 0.155  ops/ms
PackAsBinaryBenchmark.packAsBinaryLegacy     384  thrpt    5   6.897 ± 0.018  ops/ms
PackAsBinaryBenchmark.packAsBinaryLegacy     782  thrpt    5   1.321 ± 0.007  ops/ms
PackAsBinaryBenchmark.packAsBinaryLegacy    1024  thrpt    5   2.546 ± 0.043  ops/ms
PackAsBinaryBenchmark.packAsBinaryPanama     384  thrpt    5  30.626 ± 0.179  ops/ms
PackAsBinaryBenchmark.packAsBinaryPanama     782  thrpt    5  14.035 ± 0.096  ops/ms
PackAsBinaryBenchmark.packAsBinaryPanama    1024  thrpt    5  11.421 ± 0.977  ops/ms

For 256 bits on a GCP instancem looks similar:

Benchmark                                 (dims)   Mode  Cnt   Score   Error   Units
PackAsBinaryBenchmark.packAsBinary           384  thrpt    5   7.620 ± 0.238  ops/ms
PackAsBinaryBenchmark.packAsBinary           782  thrpt    5   3.818 ± 0.306  ops/ms
PackAsBinaryBenchmark.packAsBinary          1024  thrpt    5   3.070 ± 0.025  ops/ms
PackAsBinaryBenchmark.packAsBinaryLegacy     384  thrpt    5   4.321 ± 0.048  ops/ms
PackAsBinaryBenchmark.packAsBinaryLegacy     782  thrpt    5   1.000 ± 0.099  ops/ms
PackAsBinaryBenchmark.packAsBinaryLegacy    1024  thrpt    5   1.686 ± 0.016  ops/ms
PackAsBinaryBenchmark.packAsBinaryPanama     384  thrpt    5  13.296 ± 0.270  ops/ms
PackAsBinaryBenchmark.packAsBinaryPanama     782  thrpt    5   7.064 ± 0.257  ops/ms
PackAsBinaryBenchmark.packAsBinaryPanama    1024  thrpt    5   5.941 ± 0.077  ops/ms

For 512 bits on a GCP instance does look so great, still an improvement though. I tried to optimize this case by doing two bytes at a time and shifting the second to build a short but it did not help.

Benchmark                                 (dims)   Mode  Cnt  Score   Error   Units
PackAsBinaryBenchmark.packAsBinary           384  thrpt    5  7.254 ± 0.146  ops/ms
PackAsBinaryBenchmark.packAsBinary           782  thrpt    5  4.057 ± 0.026  ops/ms
PackAsBinaryBenchmark.packAsBinary          1024  thrpt    5  3.213 ± 0.036  ops/ms
PackAsBinaryBenchmark.packAsBinaryLegacy     384  thrpt    5  4.216 ± 0.457  ops/ms
PackAsBinaryBenchmark.packAsBinaryLegacy     782  thrpt    5  0.892 ± 0.030  ops/ms
PackAsBinaryBenchmark.packAsBinaryLegacy    1024  thrpt    5  1.665 ± 0.107  ops/ms
PackAsBinaryBenchmark.packAsBinaryPanama     384  thrpt    5  8.862 ± 0.248  ops/ms
PackAsBinaryBenchmark.packAsBinaryPanama     782  thrpt    5  4.861 ± 0.268  ops/ms
PackAsBinaryBenchmark.packAsBinaryPanama    1024  thrpt    5  4.037 ± 0.242  ops/ms

relates #132761

@elasticsearchmachine elasticsearchmachine added the Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch label Aug 14, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-search-relevance (Team:Search Relevance)

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.

I like it!

I think the bit shifts values should be a private static final value so we don't need to create a new intvector every time the shift is called. But, it looks good to me :)

Copy link
Contributor

@ChrisHegarty ChrisHegarty left a comment

Choose a reason for hiding this comment

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

LGTM

@iverase iverase merged commit d68a17a into elastic:main Aug 14, 2025
32 of 33 checks passed
@iverase iverase deleted the packAsbinary-vector2 branch August 14, 2025 14:26
joshua-adams-1 pushed a commit to joshua-adams-1/elasticsearch that referenced this pull request Aug 15, 2025
Vectorize by using a helper array that defines the shifts we need to apply to the vector elements.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

>non-issue :Search Relevance/Vectors Vector search Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch v9.2.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants