Skip to content

inline scanning code for fast distance computations#4785

Closed
mdouze wants to merge 1 commit intofacebookresearch:mainfrom
mdouze:export-D91830227
Closed

inline scanning code for fast distance computations#4785
mdouze wants to merge 1 commit intofacebookresearch:mainfrom
mdouze:export-D91830227

Conversation

@mdouze
Copy link
Contributor

@mdouze mdouze commented Jan 29, 2026

Summary:
After the ResultHandler diff landed there was a 1% performance regression on some critical codepath.

It seems plausible that this regression was caused because the inner loop of the scanning code calls 2 virtual functions:

  • distance_to_code to compute a code distance
  • add_result to update the result structure whenever a better restlt is found

It is probably the distance_to_code function that is the culprit because it is called for all the codes, when distance_to_code is called only if a better rresult is found.

What this diff changes is that the scanning code is made available to the InvertedListScanner implementation so that the distance computation inlining can happen.

This is applied to IndexIVFScalarQuantizer, IndexIVFFlat and IndexIVFRabitQ (it turns out that this one has a virtual function call inside its ditance_to_code so this update is unlikely to be significant).

Reviewed By: junjieqi

Differential Revision: D91830227

Summary:
After the ResultHandler diff landed there was a 1% performance regression on some critical codepath.

It seems plausible that this regression was caused because the inner loop of the scanning code calls 2 virtual functions:
- distance_to_code to compute a code distance
- add_result to update the result structure whenever a better restlt is found

It is probably the distance_to_code function that is the culprit because it is called for *all* the codes, when distance_to_code is called only if a better rresult is found.

What this diff changes is that the scanning code is made available to the InvertedListScanner implementation so that the distance computation inlining can happen.

This is applied to IndexIVFScalarQuantizer, IndexIVFFlat and IndexIVFRabitQ (it turns out that this one has a virtual function call inside its ditance_to_code so this update is unlikely to be significant).

Reviewed By: junjieqi

Differential Revision: D91830227
@meta-cla meta-cla bot added the CLA Signed label Jan 29, 2026
@meta-codesync
Copy link
Contributor

meta-codesync bot commented Jan 29, 2026

@mdouze has exported this pull request. If you are a Meta employee, you can view the originating Diff in D91830227.

@meta-codesync
Copy link
Contributor

meta-codesync bot commented Jan 30, 2026

This pull request has been merged in 851ce24.

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.

2 participants