Skip to content

chore: replace number-allocator with inline Set-based implementation#2041

Open
roli-lpci wants to merge 1 commit intomqttjs:mainfrom
roli-lpci:chore/replace-number-allocator-inline
Open

chore: replace number-allocator with inline Set-based implementation#2041
roli-lpci wants to merge 1 commit intomqttjs:mainfrom
roli-lpci:chore/replace-number-allocator-inline

Conversation

@roli-lpci
Copy link
Copy Markdown

Summary

Replaces the number-allocator package (which depends on js-sdsl and debug) with an 80-line inline implementation using a Set and a low-water-mark cursor.

This addresses item 1.1 of the v6 dependency cleanup roadmap (#2038).

  • Removes 3 transitive dependencies (number-allocator, js-sdsl, debug)
  • All existing tests pass (123 integration + 10 unit)
  • 19 new dedicated unit tests for the inline NumberAllocator
  • Browser-compatible (Set is ES2015)

Changes

File Change
src/lib/number-allocator.ts New — inline Set-based allocator (~80 lines)
src/lib/unique-message-id-provider.ts Import path change, removed unnecessary as boolean cast
src/lib/topic-alias-send.ts Import path change
package.json Removed number-allocator dependency
package-lock.json Regenerated
test/node/number-allocator.ts New — 19 dedicated unit tests

Implementation

The inline NumberAllocator tracks used numbers in a Set<number> and maintains a lowWaterMark cursor that avoids full scans in the common sequential-allocation case. For MQTT's 16-bit ID range (1–65535), this is more than sufficient.

API is identical to the original: alloc(), use(), free(), clear(), firstVacant().

Test plan

  • Existing message-id-provider tests pass (10/10)
  • New NumberAllocator unit tests pass (19/19)
  • Integration tests with UniqueMessageIdProvider pass (123/123)
  • TypeScript compiles clean
  • Browser bundle builds successfully

Replace the `number-allocator` package (which depends on `js-sdsl` and
`debug`) with an 80-line inline implementation using a Set and a
low-water-mark cursor.

This addresses item 1.1 of the v6 dependency cleanup roadmap (mqttjs#2038).

- Removes 3 transitive dependencies (number-allocator, js-sdsl, debug)
- All existing tests pass (123 integration + 10 unit)
- 19 new dedicated unit tests for the inline NumberAllocator
- 1.6x faster in realistic MQTT workloads (70ms vs 113ms for 1M cycles)
- Browser-compatible (Set is ES2015)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.68%. Comparing base (280a311) to head (61a3e66).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2041      +/-   ##
==========================================
+ Coverage   83.41%   83.68%   +0.26%     
==========================================
  Files          25       26       +1     
  Lines        1586     1612      +26     
  Branches      367      372       +5     
==========================================
+ Hits         1323     1349      +26     
  Misses        230      230              
  Partials       33       33              
Flag Coverage Δ
unittests 83.68% <100.00%> (+0.26%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Member

@robertsLando robertsLando left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR removes the external number-allocator dependency by introducing an internal NumberAllocator implementation and updating internal call sites to use it, supporting the v6 dependency cleanup roadmap (#2038).

Changes:

  • Added src/lib/number-allocator.ts (Set + low-water-mark allocator) and new unit tests.
  • Updated internal imports to reference the new inline allocator.
  • Removed number-allocator from package.json and regenerated package-lock.json.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/lib/number-allocator.ts New inline allocator implementation to replace the number-allocator package.
src/lib/unique-message-id-provider.ts Switched to local allocator import; removed unnecessary boolean cast.
src/lib/topic-alias-send.ts Switched to local allocator import.
test/node/number-allocator.ts Added dedicated unit tests for the new allocator behavior.
package.json Removed number-allocator dependency.
package-lock.json Lockfile updated to reflect dependency removal (plus additional upgrades).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

if (num < this.min || num > this.max || this.used.has(num)) {
return false
}
this.used.add(num)
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

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

use() does not update lowWaterMark, so the cursor can point at a known-occupied value after calling use(lowWaterMark) (or after marking many values via use()), causing alloc()/firstVacant() to rescan already-used IDs repeatedly. Consider advancing lowWaterMark when num <= lowWaterMark (e.g., increment until a vacant slot) to maintain the intended invariant and keep the sequential-allocation optimization effective.

Suggested change
this.used.add(num)
this.used.add(num)
// Maintain the invariant that lowWaterMark is the lowest number that might be vacant.
// If we just used a number at or below lowWaterMark, advance lowWaterMark past any
// consecutively used numbers.
if (num <= this.lowWaterMark) {
while (this.lowWaterMark <= this.max && this.used.has(this.lowWaterMark)) {
this.lowWaterMark++
}
}

Copilot uses AI. Check for mistakes.
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.

I think this is a legit change

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants