Commit bcab95a
committed
Reduce unnecessary Comparators in Range set
There are a few cases that lead to Range's Comparator sets being longer
than strictly necessary. This reduces performance of methods that
iterate over ranges repeatedly (for example, `intersects` and `subset`),
and leads to some confusing toString output like turning `x || * || X`
into `||||` instead of `*`.
- If any simple range in the set contains the null set <0.0.0-0, then
the entire simple range is the null set. `2.x <0.0.0-0` is the same as
just `<0.0.0-0`. (This is used for `>*` and `<*`, which cannot match
anything.)
- Ensure that a given Comparator will only occur once within each simple
range set. `2.3.x ^2.3` doesn't need to include `>=2.3.0` more than
once.
- If a simple range set contains more than one comparator, remove any `*`
comparators. `* >=2.3.4` is the same as just `>=2.3.4`. This was
already being done in the cast to a string, but some `ANY` Comparators
would be left behind in the set used for matching.
- If a Range set contains the simple range `*`, then drop any other
simple ranges in the set. `* || 2.x` is the same as `*`.
There's still some unnecessary comparators in there. For example, the
range `2.3 ^2.3.4` parses to `>=2.3.0 <2.4.0-0 >=2.3.4 <3.0.0-0`. Of
course, anything that is `<2.4.0-0` is also `<3.0.0-0`, and anything
that is `>=2.3.4` is also `>=2.3.0`, so the `<3.0.0-0` and `>=2.3.0`
Comparators are not necessary. But simplifying those out would be a bit
more work.
To do that, we could walk the set of Comparators checking to see if they
are a subset of any other Comparators in the list, and if so, removing
them. The subset check would not have to be a full Range.subset(); we
could just see if the gtlt points in the same direction, and if one
semver is greater than the other. It's an O(n^2) operation, but one on
typically very small n.
PR-URL: #324
Credit: @isaacs
Close: #3241 parent 226e6dc commit bcab95a
File tree
3 files changed
+48
-5
lines changed- classes
- test
- fixtures
- ranges
3 files changed
+48
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
49 | 67 | | |
50 | 68 | | |
51 | 69 | | |
| |||
87 | 105 | | |
88 | 106 | | |
89 | 107 | | |
90 | | - | |
| 108 | + | |
91 | 109 | | |
92 | 110 | | |
93 | 111 | | |
94 | 112 | | |
| 113 | + | |
95 | 114 | | |
96 | 115 | | |
97 | 116 | | |
98 | 117 | | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
99 | 133 | | |
100 | 134 | | |
101 | 135 | | |
| |||
155 | 189 | | |
156 | 190 | | |
157 | 191 | | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
158 | 195 | | |
159 | 196 | | |
160 | 197 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | | - | |
| 41 | + | |
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
82 | | - | |
| 82 | + | |
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
| 90 | + | |
| 91 | + | |
90 | 92 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
| 40 | + | |
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| |||
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
75 | | - | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
76 | 80 | | |
77 | 81 | | |
78 | 82 | | |
| |||
0 commit comments