Optimize PackageLister version constraints with discrete bitmasks - #4874
Open
sigurdm wants to merge 1 commit into
Open
Optimize PackageLister version constraints with discrete bitmasks#4874sigurdm wants to merge 1 commit into
sigurdm wants to merge 1 commit into
Conversation
sigurdm
force-pushed
the
optimize-bitmask-constraints
branch
6 times, most recently
from
August 20, 2026 09:56
8d6f021 to
32e3312
Compare
Introduce Bitmask to represent discrete bit vectors over a fixed-size index range. In PackageLister, index versions once and use binary search to construct bitmasks for VersionConstraint instances, enabling O(1) countVersions() and bestVersion() operations.
sigurdm
force-pushed
the
optimize-bitmask-constraints
branch
from
August 20, 2026 10:05
32e3312 to
40edb26
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR optimizes version constraint evaluations in the version solver (
PackageLister) by introducing discrete bitmasks (VersionBitmask).Problem
Previously,
PackageLister.countVersionsandPackageLister.bestVersionperformed linear traversals (.where((id) => constraint.allows(id.version)).lengthand reverse iteration loops) over the full version list of each candidate package. For mature packages with hundreds of versions (e.g.analyzer,http,intl,build_runner), this caused solving time and backtracking to scale with package age/version count ($O(N)$).Solution
VersionBitmask(lib/src/solver/version_bitmask.dart):Uint32List).&,|,difference,allowsAll,allowsAny,count()(popcount),highestIndex(),lowestIndex(), andallows(index).PackageLister(lib/src/solver/package_lister.dart):_PackageVersionIndex.VersionConstraintinstances via fast binary search (Version,VersionRange,VersionUnion, with SemVer pre-release normalization) and caches mask lookups.countVersions()andbestVersion()withtest/solver/version_bitmask_test.dart):[0, 1, 5, 62, 63, 64, 65, 128, 256, 1000], range boundaries, set operations, and equality.Benchmarks (50,000 queries per config)
countVersionsbestVersioncountVersionsbestVersioncountVersionsbestVersioncountVersionsbestVersioncountVersionsbestVersion