Skip to content

Commit cd66754

Browse files
committed
improve performance of fetching missing combinations
1 parent ff2cb57 commit cd66754

2 files changed

Lines changed: 48 additions & 24 deletions

File tree

‎src/lib/server/consistency.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function get_contradiction(
3030
return { contradiction }
3131
}
3232

33-
export function contradiction_worker(
33+
function contradiction_worker(
3434
satisfied_properties: Set<string>,
3535
unsatisfied_properties: Set<string>,
3636
implications: NormalizedImplication[]

‎src/lib/server/fetchers/missing_data.ts‎

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { db } from '#lib/server/db.ts'
22
import type { StructureShort, StructureType } from '#lib/commons/types.ts'
3-
import { contradiction_worker } from '#lib/server/consistency.ts'
3+
import { deduce_properties } from '#shared/deduction.utils.ts'
44
import { get_normalized_implications } from '#shared/implications.ts'
55

66
export function fetch_missing_data(type: StructureType) {
@@ -58,19 +58,14 @@ export function fetch_missing_data(type: StructureType) {
5858
)
5959
.all(type)
6060

61-
const witnessed_pairs = db
62-
.prepare<[StructureType], { p: string; q: string }>(
63-
`SELECT DISTINCT
64-
a.property_id AS p,
65-
an.property_id AS q
66-
FROM property_assignments a
67-
INNER JOIN property_assignments an
68-
ON a.structure_id = an.structure_id
69-
WHERE
70-
a.type = ?
71-
AND an.type = a.type
72-
AND a.is_satisfied = TRUE
73-
AND an.is_satisfied = FALSE`
61+
const assignments = db
62+
.prepare<
63+
[StructureType],
64+
{ structure_id: string; property_id: string; is_satisfied: 0 | 1 }
65+
>(
66+
`SELECT structure_id, property_id, is_satisfied
67+
FROM property_assignments
68+
WHERE type = ? AND is_satisfied IS NOT NULL`
7469
)
7570
.all(type)
7671

@@ -83,31 +78,60 @@ export function fetch_missing_data(type: StructureType) {
8378
(impl) => !impl.associated_assumptions
8479
)
8580

86-
const witnessed_pairs_set = new Set(witnessed_pairs.map(({ p, q }) => `${p}|${q}`))
81+
const assignments_by_structure: Record<
82+
string,
83+
{ satisfied: string[]; unsatisfied: string[] }
84+
> = {}
85+
86+
for (const { structure_id, property_id, is_satisfied } of assignments) {
87+
assignments_by_structure[structure_id] ??= { satisfied: [], unsatisfied: [] }
88+
const { satisfied, unsatisfied } = assignments_by_structure[structure_id]
89+
if (is_satisfied) satisfied.push(property_id)
90+
else unsatisfied.push(property_id)
91+
}
92+
93+
// maps p to the set of all q such that some structure witnesses p ∧ ¬q
94+
const witnessed_pairs = new Map<string, Set<string>>()
95+
96+
for (const { satisfied, unsatisfied } of Object.values(assignments_by_structure)) {
97+
for (const p of satisfied) {
98+
let witnessed_qs = witnessed_pairs.get(p)
99+
if (!witnessed_qs) {
100+
witnessed_qs = new Set()
101+
witnessed_pairs.set(p, witnessed_qs)
102+
}
103+
for (const q of unsatisfied) witnessed_qs.add(q)
104+
}
105+
}
106+
107+
const is_witnessed = (p: string, q: string) => witnessed_pairs.get(p)?.has(q) ?? false
87108

88109
const missing_combinations: [string, string][] = []
89110

90111
for (const p of properties) {
112+
// p ∧ ¬q is contradictory iff q can be deduced from p
113+
const { deduced_satisfied_properties: deducible_properties } = deduce_properties(
114+
new Set([p.id]),
115+
implications,
116+
() => ({ proof: null, stop: false })
117+
)
118+
91119
for (const q of properties) {
92120
if (p.id === q.id) continue
93121

94-
if (witnessed_pairs_set.has(`${p.id}|${q.id}`)) continue
122+
if (is_witnessed(p.id, q.id)) continue
95123

96124
if (
97125
p.dual_property_id &&
98126
q.dual_property_id &&
99-
witnessed_pairs_set.has(`${p.dual_property_id}|${q.dual_property_id}`)
127+
is_witnessed(p.dual_property_id, q.dual_property_id)
100128
) {
101129
continue
102130
}
103131

104-
const contradiction = contradiction_worker(
105-
new Set([p.id]),
106-
new Set([q.id]),
107-
implications
108-
)
132+
if (deducible_properties.has(q.id)) continue
109133

110-
if (!contradiction) missing_combinations.push([p.id, q.id])
134+
missing_combinations.push([p.id, q.id])
111135
}
112136
}
113137

0 commit comments

Comments
 (0)