Skip to content

perf: Make Word.MergeAll compaction linear - #7934

Open
nikitabhagat2307-prog wants to merge 2 commits into
dotnet:mainfrom
nikitabhagat2307-prog:codex/word-mergeall-subquadratic
Open

nikitabhagat2307-prog wants to merge 2 commits into
dotnet:mainfrom
nikitabhagat2307-prog:codex/word-mergeall-subquadratic

Conversation

@nikitabhagat2307-prog

Copy link
Copy Markdown

Summary

  • Replace repeated symbol removals in Word.MergeAll with one stable linear compaction pass.
  • Add Vec<T>.RemoveAll to compact in place and clear the unused tail of its buffer.

This removes the quadratic shifting cost from cleanup while preserving the surviving symbol order and leaving merge selection unchanged.

Fixes #7872

Validation

  • dotnet test compiled the Tokenizers library and test assembly for net8.0 with the repository analyzer disabled.
  • Local test discovery reported no tests available with this checkout's preview SDK/test-host setup.
  • git diff --check passed.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Pull requests submitted by community contributors label Nov 20, 2026
@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.21053% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.88%. Comparing base (f8e6d4c) to head (54d540e).
⚠️ Report is 22 commits behind head on main.

Files with missing lines Patch % Lines
src/Microsoft.ML.Tokenizers/Utils/Vec.cs 83.33% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7934      +/-   ##
==========================================
+ Coverage   70.85%   70.88%   +0.03%     
==========================================
  Files        1420     1421       +1     
  Lines      279991   280145     +154     
  Branches    27594    27603       +9     
==========================================
+ Hits       198374   198582     +208     
+ Misses      73467    73421      -46     
+ Partials     8150     8142       -8     
Flag Coverage Δ
Debug 70.88% <84.21%> (+0.03%) ⬆️
production 64.32% <84.21%> (+0.03%) ⬆️
test 90.74% <ø> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/Microsoft.ML.Tokenizers/Model/Word.cs 62.74% <100.00%> (-1.07%) ⬇️
src/Microsoft.ML.Tokenizers/Utils/Vec.cs 60.34% <83.33%> (-7.16%) ⬇️

... and 56 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI 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.

🟢 Approval recommended

There are no active review issues for the tokenizer cleanup optimization.

No open issues 🎉

What changed in this PR

This PR addresses #7872 by removing quadratic cleanup costs from tokenizer Word.MergeAll. It preserves surviving symbol order and leaves merge selection unchanged; only the cleanup step becomes linear.

Changes:

  • Add Vec<T>.RemoveAll for stable, in-place compaction with unused-tail clearing.
  • Replace repeated symbol removals in Word.MergeAll with one compaction pass.
File Description
src/​Microsoft.ML.Tokenizers/​Utils/​Vec.cs Adds predicate-based linear compaction and clears removed elements from the buffer tail.
src/​Microsoft.ML.Tokenizers/​Model/​Word.cs Uses RemoveAll to discard zero-length symbols without repeated shifting.

Review effort: Balanced

Comment on lines +79 to +83
if (match is null)
{
throw new ArgumentNullException(nameof(match));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Coverlet complains about the the match is null check. It seems like we can't ever reach this line. match is non-nullable and the only caller passes a static lambda, so it can go like the _buffer check did or replace it with Debug.Assert

Suggested change
if (match is null)
{
throw new ArgumentNullException(nameof(match));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Pull requests submitted by community contributors

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make Word.MergeAll sub-quadratic

3 participants