mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-21 14:23:53 +00:00
ifcparse: unregister a deleted instance's inverse records via its attributes (#9467)
process_deletion_inverse() called inverse_index::remove_source(), which
walked every record in the file's inverse index to find the ones whose
source is the deleted instance: O(R) per deletion, the dominant cost of
file.remove() on large files now that the lookup side no longer re-sorts.
The records a deleted instance contributed are exactly the entity
references in its own attributes, so walk those with the same visitor
build_inverses_() uses for registration and remove each record with a
targeted binary search instead. remove_source() has no callers left and
is deleted.
Also use the ordered view of batch_deletion_ids_ (a boost multi_index
that already had one) for the is-this-referencer-also-being-deleted
check in process_deletion_(), which was a linear std::find over the
sequenced view: O(b) per referencing instance made batch deletion of b
instances quadratic.
file.remove on 300 IfcPropertySet of a 155 MB IFC4 model (201k IfcRoot)
drops from 3.15 ms to 0.17 ms per call, batched removal of 2000 from
3.34 ms to 0.17 ms per call, root.remove_product on 100 walls from
332 ms to 131 ms per call.
Claude-Session: https://claude.ai/code/session_01HNrXDmR88wKPCYwGE21SyH
(cherry picked from commit 938442303f)
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -165,3 +165,80 @@ TEST_CASE("Inverse lookups stay consistent across interleaved adds, removals and
|
||||
}
|
||||
CHECK(referencing_ids(target) == std::vector<int>{(int)trimmed.id()});
|
||||
}
|
||||
|
||||
TEST_CASE("Deleting an instance unregisters the records its own attributes contributed", "[ifcparse]") {
|
||||
ifcopenshell::file file(ifcopenshell::schema_by_name("IFC4"));
|
||||
const auto* point_declaration = file.schema()->declaration_by_name("IfcCartesianPoint");
|
||||
const auto* polyline_declaration = file.schema()->declaration_by_name("IfcPolyline");
|
||||
const auto* trimmed_curve_declaration = file.schema()->declaration_by_name("IfcTrimmedCurve");
|
||||
|
||||
auto target = file.create(point_declaration);
|
||||
auto second = file.create(point_declaration);
|
||||
|
||||
// A reference registered before the first lookup lands in the base tier,
|
||||
// one registered after it in the delta.
|
||||
auto base_referencer = file.create(polyline_declaration);
|
||||
base_referencer.set_attribute_value(0, std::vector<express::base>{target, target});
|
||||
REQUIRE(file.instances_by_reference(target.id()).size() == 2);
|
||||
auto delta_referencer = file.create(polyline_declaration);
|
||||
delta_referencer.set_attribute_value(0, std::vector<express::base>{target, second});
|
||||
REQUIRE(file.instances_by_reference(target.id()).size() == 3);
|
||||
|
||||
// Duplicate references in one aggregate contribute two records; deleting
|
||||
// the source must drop both.
|
||||
file.remove_entity(base_referencer);
|
||||
CHECK(file.instances_by_reference(target.id()).size() == 1);
|
||||
|
||||
// Referencing the same instance through two attributes contributes a
|
||||
// record per attribute; deleting the source must drop them all.
|
||||
auto trimmed = file.create(trimmed_curve_declaration);
|
||||
trimmed.set_attribute_value(1, std::vector<express::base>{second});
|
||||
trimmed.set_attribute_value(2, std::vector<express::base>{second});
|
||||
CHECK(file.instances_by_reference(second.id()).size() == 3);
|
||||
file.remove_entity(trimmed);
|
||||
CHECK(file.instances_by_reference(second.id()).size() == 1);
|
||||
|
||||
// Deleting the target first prunes it out of the source's attribute, so
|
||||
// deleting the source afterwards finds nothing left to unregister.
|
||||
file.remove_entity(target);
|
||||
file.remove_entity(delta_referencer);
|
||||
CHECK(file.instances_by_reference(second.id()).empty());
|
||||
CHECK(file.get_total_inverses(second.id()) == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("Batch deletion prunes surviving referencers and leaves no stale records", "[ifcparse]") {
|
||||
ifcopenshell::file file(ifcopenshell::schema_by_name("IFC4"));
|
||||
const auto* point_declaration = file.schema()->declaration_by_name("IfcCartesianPoint");
|
||||
const auto* polyline_declaration = file.schema()->declaration_by_name("IfcPolyline");
|
||||
|
||||
auto kept_point = file.create(point_declaration);
|
||||
std::vector<express::base> doomed_points;
|
||||
for (int i = 0; i < 50; ++i) {
|
||||
doomed_points.push_back(file.create(point_declaration));
|
||||
}
|
||||
|
||||
// The survivor references every doomed point plus the kept one; a doomed
|
||||
// referencer references the kept point.
|
||||
auto survivor = file.create(polyline_declaration);
|
||||
auto survivor_points = doomed_points;
|
||||
survivor_points.push_back(kept_point);
|
||||
survivor.set_attribute_value(0, survivor_points);
|
||||
auto doomed_referencer = file.create(polyline_declaration);
|
||||
doomed_referencer.set_attribute_value(0, std::vector<express::base>{kept_point});
|
||||
REQUIRE(file.instances_by_reference(kept_point.id()).size() == 2);
|
||||
|
||||
file.batch();
|
||||
for (auto& point : doomed_points) {
|
||||
file.remove_entity(point);
|
||||
}
|
||||
file.remove_entity(doomed_referencer);
|
||||
file.unbatch();
|
||||
|
||||
CHECK((std::vector<express::base>)survivor.get_attribute_value(0) == std::vector<express::base>{kept_point});
|
||||
CHECK(file.instances_by_reference(kept_point.id()).size() == 1);
|
||||
CHECK(file.get_total_inverses(kept_point.id()) == 1);
|
||||
for (auto& point : doomed_points) {
|
||||
CHECK(file.instances_by_reference(point.id()).empty());
|
||||
}
|
||||
CHECK(file.instances_by_reference(doomed_referencer.id()).empty());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user