Skip to content

Add panama simd implementation of contains function for BinaryDocValuesContainsTermQuery - #143922

Merged
parkertimmins merged 11 commits into
elastic:mainfrom
parkertimmins:parker/simd-contains
Mar 30, 2026
Merged

parkertimmins merged 11 commits into
elastic:mainfrom
parkertimmins:parker/simd-contains

Conversation

@parkertimmins

@parkertimmins parkertimmins commented Mar 10, 2026 •

Copy link
Copy Markdown
Contributor

The contains() function matches terms that contain a given substring in BinaryDocValuesContainsTermQuery. The existing scalar implementation is based on the StringUTF16.indexOfLatin1Unsafe function. This change adds a SIMD implementation.

Add a vectorized substring search using the Panama Vector API.
The SIMD implementation broadcasts both the first and last bytes
of the search term, compares them against vector-sized chunks in
parallel, and only performs full verification at candidate
positions where both bytes match.

Made-with: Cursor
Replace Arrays.mismatch with a direct byte loop for candidate
verification. Fall back to scalar for values shorter than 24
bytes where SIMD overhead exceeds its benefit. Remove redundant
benchmark method that always used the scalar fallback.

Made-with: Cursor
@elasticsearchmachine

Copy link
Copy Markdown
Collaborator

Hi @parkertimmins, I've created a changelog YAML for you.

@parkertimmins

Copy link
Copy Markdown
Contributor Author

Buildkite benchmark this with clickbench-columnar-mode please

Use Panama ByteVector to compare middle bytes in chunks
instead of a scalar byte-by-byte loop. Also add termLen=50
to the benchmark to exercise the SIMD path.

Results show no measurable difference because middleBytesMatch
is called too infrequently (first+last byte filter eliminates
~99.99% of candidates) and on AVX2 (32-byte vectors) the SIMD
loop only engages for termLength >= 34.

Benchmark results (ops/us, higher is better):

termLen  valueLen  panamaSimd  scalar
      5        20       76.6    89.8
      5        35       82.6    52.3
      5       100       55.7    22.8
      5       500       35.5     5.2
      5      1000       20.9     2.8
     10       100       55.2    18.9
     10       500       27.7     4.8
     10      1000       18.0     2.6
     20       100       53.0    20.2
     20       500       23.4     4.5
     20      1000       15.2     2.6
     50       500       23.9     4.2
     50      1000       14.0     2.3

Made-with: Cursor
@parkertimmins

Copy link
Copy Markdown
Contributor Author

Results of a micro benchmark:

Benchmark                     (avgTermLength)  (avgValueLength)   Mode  Cnt   Score   Error   Units
ContainsBenchmark.panamaSimd                5                20  thrpt    5  81.084 ± 2.850  ops/us
ContainsBenchmark.panamaSimd                5                35  thrpt    5  66.992 ± 4.816  ops/us
ContainsBenchmark.panamaSimd                5                50  thrpt    5  68.806 ± 1.582  ops/us
ContainsBenchmark.panamaSimd                5               100  thrpt    5  59.828 ± 0.568  ops/us
ContainsBenchmark.panamaSimd                5               500  thrpt    5  36.105 ± 0.911  ops/us
ContainsBenchmark.panamaSimd                5              1000  thrpt    5  20.255 ± 0.694  ops/us
ContainsBenchmark.panamaSimd               10                20  thrpt    5  79.220 ± 0.687  ops/us
ContainsBenchmark.panamaSimd               10                35  thrpt    5  67.255 ± 2.742  ops/us
ContainsBenchmark.panamaSimd               10                50  thrpt    5  77.728 ± 6.298  ops/us
ContainsBenchmark.panamaSimd               10               100  thrpt    5  56.166 ± 1.345  ops/us
ContainsBenchmark.panamaSimd               10               500  thrpt    5  27.334 ± 0.515  ops/us
ContainsBenchmark.panamaSimd               10              1000  thrpt    5  18.232 ± 0.341  ops/us
ContainsBenchmark.panamaSimd               20                20  thrpt    5  86.063 ± 0.591  ops/us
ContainsBenchmark.panamaSimd               20                35  thrpt    5  68.353 ± 4.981  ops/us
ContainsBenchmark.panamaSimd               20                50  thrpt    5  67.157 ± 0.262  ops/us
ContainsBenchmark.panamaSimd               20               100  thrpt    5  51.859 ± 0.176  ops/us
ContainsBenchmark.panamaSimd               20               500  thrpt    5  25.214 ± 0.654  ops/us
ContainsBenchmark.panamaSimd               20              1000  thrpt    5  16.616 ± 0.339  ops/us
ContainsBenchmark.scalar                    5                20  thrpt    5  90.001 ± 6.914  ops/us
ContainsBenchmark.scalar                    5                35  thrpt    5  51.426 ± 4.790  ops/us
ContainsBenchmark.scalar                    5                50  thrpt    5  32.619 ± 2.491  ops/us
ContainsBenchmark.scalar                    5               100  thrpt    5  21.811 ± 0.948  ops/us
ContainsBenchmark.scalar                    5               500  thrpt    5   5.223 ± 0.021  ops/us
ContainsBenchmark.scalar                    5              1000  thrpt    5   2.749 ± 0.027  ops/us
ContainsBenchmark.scalar                   10                20  thrpt    5  79.635 ± 3.778  ops/us
ContainsBenchmark.scalar                   10                35  thrpt    5  57.813 ± 0.157  ops/us
ContainsBenchmark.scalar                   10                50  thrpt    5  40.579 ± 3.380  ops/us
ContainsBenchmark.scalar                   10               100  thrpt    5  18.439 ± 0.225  ops/us
ContainsBenchmark.scalar                   10               500  thrpt    5   4.829 ± 0.046  ops/us
ContainsBenchmark.scalar                   10              1000  thrpt    5   2.514 ± 0.175  ops/us
ContainsBenchmark.scalar                   20                20  thrpt    5  94.250 ± 2.051  ops/us
ContainsBenchmark.scalar                   20                35  thrpt    5  57.838 ± 0.468  ops/us
ContainsBenchmark.scalar                   20                50  thrpt    5  36.748 ± 1.756  ops/us
ContainsBenchmark.scalar                   20               100  thrpt    5  19.826 ± 0.250  ops/us
ContainsBenchmark.scalar                   20               500  thrpt    5   4.710 ± 0.125  ops/us
ContainsBenchmark.scalar                   20              1000  thrpt    5   2.585 ± 0.010  ops/us

@parkertimmins

Copy link
Copy Markdown
Contributor Author

Buildkite benchmark this with clickbench-columnar-mode please

1 similar comment
@parkertimmins

Copy link
Copy Markdown
Contributor Author

Buildkite benchmark this with clickbench-columnar-mode please

@parkertimmins

Copy link
Copy Markdown
Contributor Author

Buildkite benchmark this with clickbench-columnar-mode please

@parkertimmins

Copy link
Copy Markdown
Contributor Author

Buildkite benchmark this with clickbench-columnar-mode please

@elasticmachine

elasticmachine commented Mar 29, 2026 •

Copy link
Copy Markdown
Collaborator

💚 Build Succeeded

This build ran two clickbench-columnar-mode benchmarks to evaluate performance impact of this PR.

History

cc @parkertimmins

@parkertimmins

Copy link
Copy Markdown
Contributor Author

P100 looks very good for the *google* queries:
image

@parkertimmins
parkertimmins marked this pull request as ready for review March 30, 2026 14:12
@elasticsearchmachine

Copy link
Copy Markdown
Collaborator

Pinging @elastic/es-storage-engine (Team:StorageEngine)

@martijnvg

Copy link
Copy Markdown
Member

🚀 nice results!

@ChrisHegarty ChrisHegarty left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Very nice! The code looks clean and well tested. LGTM

@parkertimmins
parkertimmins merged commit 770b77c into elastic:main Mar 30, 2026
32 of 36 checks passed
@parkertimmins
parkertimmins deleted the parker/simd-contains branch March 30, 2026 20:17
mouhc1ne pushed a commit to shmuelhanoch/elasticsearch that referenced this pull request Mar 31, 2026
…luesContainsTermQuery (elastic#143922)

Add a vectorized substring search using the Panama Vector API. The SIMD implementation broadcasts both the first and last bytes of the search term, compares them against vector-sized chunks in parallel, and only performs full verification at candidate positions where both bytes match.
ncordon pushed a commit to ncordon/elasticsearch that referenced this pull request Apr 1, 2026
…luesContainsTermQuery (elastic#143922)

Add a vectorized substring search using the Panama Vector API. The SIMD implementation broadcasts both the first and last bytes of the search term, compares them against vector-sized chunks in parallel, and only performs full verification at candidate positions where both bytes match.
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.

5 participants