Skip to content

Commit ac24ff0

Browse files
committed
Simplify join
1 parent 1142a34 commit ac24ff0

File tree

1 file changed

+18
-36
lines changed

1 file changed

+18
-36
lines changed

packages/d2ts/src/version-index.ts

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -146,44 +146,26 @@ export class Index<K, V> implements IndexType<K, V> {
146146
)
147147

148148
// We want to iterate over the smaller of the two indexes to reduce the
149-
// number of operations we need to do.
150-
if (this.#inner.size <= other.#inner.size) {
151-
for (const [key, versions] of this.#inner) {
152-
if (!other.has(key)) continue
153-
const otherVersions = other.get(key)
154-
for (const [rawVersion1, data1] of versions) {
155-
const version1 =
156-
this.#compactionFrontier &&
157-
this.#compactionFrontier.lessEqualVersion(rawVersion1)
158-
? rawVersion1.advanceBy(this.#compactionFrontier)
159-
: rawVersion1
160-
for (const [version2, data2] of otherVersions) {
161-
for (const [val1, mul1] of data1) {
162-
for (const [val2, mul2] of data2) {
163-
const resultVersion = version1.join(version2)
164-
collections.update(resultVersion, (existing) => {
165-
existing.push([key, [val1, val2], mul1 * mul2])
166-
return existing
167-
})
168-
}
169-
}
170-
}
171-
}
172-
}
173-
} else {
174-
for (const [key, otherVersions] of other.entries()) {
175-
if (!this.has(key)) continue
176-
const versions = this.get(key)
149+
// number of operations we need to do.
150+
const thisIsTheSmallerIndex = this.#inner.size <= other.#inner.size
151+
const [smallestIndex, otherIndex] =
152+
thisIsTheSmallerIndex ? [this, other] : [other, this]
153+
154+
for (const [key, versions] of smallestIndex.#inner) {
155+
if (!otherIndex.has(key)) continue
156+
const otherVersions = otherIndex.get(key)
157+
for (const [version1, data1] of versions) {
177158
for (const [version2, data2] of otherVersions) {
178-
for (const [version1, data1] of versions) {
159+
for (const [val1, mul1] of data1) {
179160
for (const [val2, mul2] of data2) {
180-
for (const [val1, mul1] of data1) {
181-
const resultVersion = version1.join(version2)
182-
collections.update(resultVersion, (existing) => {
183-
existing.push([key, [val1, val2], mul1 * mul2])
184-
return existing
185-
})
186-
}
161+
const resultVersion = version1.join(version2)
162+
collections.update(resultVersion, (existing) => {
163+
const values: [V, V2] = thisIsTheSmallerIndex
164+
? [val1 as V, val2 as V2]
165+
: [val2 as V, val1 as V2]
166+
existing.push([key, values, mul1 * mul2])
167+
return existing
168+
})
187169
}
188170
}
189171
}

0 commit comments

Comments
 (0)