Skip to content

Can ReadOnlySpan.Contains with ReferenceEqualityComparer be vectorized? #117178

Closed Answered by EgorBo
verdie-g asked this question in General
Discussion options

You must be logged in to vote

If technically possible, would you say the ratio (extra LOC) / benefits is too low?

it's not possible. You basically want to take some string "A" (needle), populate a vector with "A"'s address e.g. { 0xAAAABBBB, 0xAAAABBBB ... 0xAAAABBBB }. and then run search. What if GC relocates strings at any point of time or two equal strings have different addresses? It won't update values in SIMD registers today (well, even if it could, it wouldn't be able to figure out that it needs to update the broadcasted values as well).

So your search becomes opportunistic, it might help if you want to run some quick vectorized O(N) search and if it finds nothing - run slow O(N). But it doesn't sound great …

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@verdie-g
Comment options

@EgorBo
Comment options

EgorBo Jun 30, 2025
Collaborator

Answer selected by verdie-g
@verdie-g
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants