@atlaspack/graph 3.4.7 → 3.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # @atlaspack/graph
2
2
 
3
+ ## 3.5.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [[`73ea3c4`](https://github.com/atlassian-labs/atlaspack/commit/73ea3c4d85d4401fdd15abcbf988237e890e7ad3), [`b1b3693`](https://github.com/atlassian-labs/atlaspack/commit/b1b369317c66f8a431c170df2ebba4fa5b2e38ef)]:
8
+ - @atlaspack/feature-flags@2.17.0
9
+
10
+ ## 3.5.0
11
+
12
+ ### Minor Changes
13
+
14
+ - [#600](https://github.com/atlassian-labs/atlaspack/pull/600) [`1b52b99`](https://github.com/atlassian-labs/atlaspack/commit/1b52b99db4298b04c1a6eb0f97994d75a2d436f9) Thanks [@mattcompiles](https://github.com/mattcompiles)! - Add static intersect, size and equals methods to BitSet
15
+
3
16
  ## 3.4.7
4
17
 
5
18
  ### Patch Changes
@@ -0,0 +1,37 @@
1
+ /*
2
+ * BitSets are primarily used for performance reasons, so we want to be able to
3
+ * validate that changes we make to it are performace improvements.
4
+ *
5
+ * This file exists as a ready-made playground for benchmarking changes you may
6
+ * want to make to the BitSet implementation.
7
+ *
8
+ * Run with `yarn workspace @atlaspack/graph benchmark` from the root
9
+ */
10
+
11
+ // Needed to make Flow work in the benchmarks
12
+ require('@atlaspack/babel-register');
13
+
14
+ const {BitSet} = require('../src/BitSet.js');
15
+ const b = require('benny');
16
+
17
+ function createBitSetWithEntries(capacity, entries) {
18
+ let bitSet = new BitSet(capacity);
19
+ for (const index of entries) {
20
+ bitSet.add(index);
21
+ }
22
+ return bitSet;
23
+ }
24
+
25
+ let bundleIndices = [4334, 348, 2145, 480, 747, 1446, 326, 2791, 2658, 1334];
26
+
27
+ let bundleBitSet = createBitSetWithEntries(5000, bundleIndices);
28
+
29
+ b.suite(
30
+ 'BitSet - size',
31
+ b.add('Control', () => {
32
+ bundleBitSet.size();
33
+ }),
34
+ b.configure({minSamples: 100}),
35
+ b.cycle(),
36
+ b.complete(),
37
+ );
package/lib/BitSet.js CHANGED
@@ -25,6 +25,11 @@ class BitSet {
25
25
  res.union(b);
26
26
  return res;
27
27
  }
28
+ static intersect(a, b) {
29
+ let res = a.clone();
30
+ res.intersect(b);
31
+ return res;
32
+ }
28
33
  get capacity() {
29
34
  return this.bits.length * 32;
30
35
  }
@@ -63,6 +68,26 @@ class BitSet {
63
68
  this.bits[i] &= ~other.bits[i];
64
69
  }
65
70
  }
71
+ size() {
72
+ let bits = this.bits;
73
+ let setBitsCount = 0;
74
+ for (let k = 0; k < bits.length; k++) {
75
+ let chunk = bits[k];
76
+ while (chunk !== 0) {
77
+ chunk &= chunk - 1; // Clear the least significant bit set
78
+ setBitsCount++;
79
+ }
80
+ }
81
+ return setBitsCount;
82
+ }
83
+ equals(other) {
84
+ for (let i = 0; i < this.bits.length; i++) {
85
+ if (this.bits[i] !== other.bits[i]) {
86
+ return false;
87
+ }
88
+ }
89
+ return true;
90
+ }
66
91
  forEach(fn) {
67
92
  // https://lemire.me/blog/2018/02/21/iterating-over-set-bits-quickly/
68
93
  let bits = this.bits;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaspack/graph",
3
- "version": "3.4.7",
3
+ "version": "3.5.1",
4
4
  "description": "Blazing fast, zero configuration web application bundler",
5
5
  "license": "(MIT OR Apache-2.0)",
6
6
  "publishConfig": {
@@ -15,9 +15,16 @@
15
15
  "engines": {
16
16
  "node": ">= 16.0.0"
17
17
  },
18
+ "scripts": {
19
+ "benchmark": "node ./benchmark/BitSet.js"
20
+ },
18
21
  "dependencies": {
19
- "@atlaspack/feature-flags": "2.16.0",
22
+ "@atlaspack/feature-flags": "2.17.0",
20
23
  "nullthrows": "^1.1.1"
21
24
  },
25
+ "devDependencies": {
26
+ "@atlaspack/babel-register": "2.14.1",
27
+ "benny": "^3.7.1"
28
+ },
22
29
  "type": "commonjs"
23
30
  }
package/src/BitSet.js CHANGED
@@ -28,6 +28,12 @@ export class BitSet {
28
28
  return res;
29
29
  }
30
30
 
31
+ static intersect(a: BitSet, b: BitSet): BitSet {
32
+ let res = a.clone();
33
+ res.intersect(b);
34
+ return res;
35
+ }
36
+
31
37
  get capacity(): number {
32
38
  return this.bits.length * 32;
33
39
  }
@@ -82,6 +88,31 @@ export class BitSet {
82
88
  }
83
89
  }
84
90
 
91
+ size(): number {
92
+ let bits = this.bits;
93
+ let setBitsCount = 0;
94
+
95
+ for (let k = 0; k < bits.length; k++) {
96
+ let chunk = bits[k];
97
+
98
+ while (chunk !== 0) {
99
+ chunk &= chunk - 1; // Clear the least significant bit set
100
+ setBitsCount++;
101
+ }
102
+ }
103
+
104
+ return setBitsCount;
105
+ }
106
+
107
+ equals(other: BitSet): boolean {
108
+ for (let i = 0; i < this.bits.length; i++) {
109
+ if (this.bits[i] !== other.bits[i]) {
110
+ return false;
111
+ }
112
+ }
113
+ return true;
114
+ }
115
+
85
116
  forEach(fn: (bit: number) => void) {
86
117
  // https://lemire.me/blog/2018/02/21/iterating-over-set-bits-quickly/
87
118
  let bits = this.bits;
@@ -107,4 +107,46 @@ describe('BitSet', () => {
107
107
  assertValues(set2, [3, 5]);
108
108
  assertValues(set3, [1, 3, 5]);
109
109
  });
110
+
111
+ it('BitSet.intersect should create a new BitSet with the intersect', () => {
112
+ let set1 = new BitSet(5);
113
+ set1.add(1);
114
+ set1.add(3);
115
+
116
+ let set2 = new BitSet(5);
117
+ set2.add(3);
118
+ set2.add(5);
119
+
120
+ let set3 = BitSet.intersect(set1, set2);
121
+ assertValues(set1, [1, 3]);
122
+ assertValues(set2, [3, 5]);
123
+ assertValues(set3, [3]);
124
+ });
125
+
126
+ it('should identify equality with another BitSet', () => {
127
+ let set1 = new BitSet(5);
128
+ set1.add(1);
129
+ set1.add(3);
130
+
131
+ let set2 = new BitSet(5);
132
+ set2.add(3);
133
+ set2.add(5);
134
+
135
+ let set3 = set1.clone();
136
+
137
+ assert(set1.equals(set3));
138
+ assert(!set1.equals(set2));
139
+ });
140
+
141
+ it('should calculate size of BitSet', () => {
142
+ let set1 = new BitSet(5);
143
+ set1.add(1);
144
+ set1.add(3);
145
+
146
+ assert.equal(set1.size(), 2);
147
+
148
+ set1.add(3);
149
+ set1.add(4);
150
+ assert.equal(set1.size(), 3);
151
+ });
110
152
  });