Skip to content

Commit 1629205

Browse files
committed
Some optimisations to IfcDiff for a 50% speed up (461s down to 300s on sample data)
1 parent d058f17 commit 1629205

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

src/ifcdiff/ifcdiff.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@
2929

3030

3131
class IfcDiff:
32-
def __init__(self, old_file, new_file, output_file, inverse_classes=None):
32+
def __init__(self, old_file, new_file, output_file, inverse_classes=None, is_shallow=False):
3333
self.old_file = old_file
3434
self.new_file = new_file
3535
self.output_file = output_file
3636
self.change_register = {}
37-
self.representation_ids = []
37+
self.representation_ids = set()
3838
self.inverse_classes = inverse_classes
3939
self.precision = 2
40+
self.is_shallow = is_shallow
4041

4142
def diff(self):
4243
print("# IFC Diff")
@@ -61,16 +62,18 @@ def diff(self):
6162

6263
for global_id in same_elements:
6364
total_diffed += 1
64-
print("{}/{} diffed ...".format(total_diffed, total_same_elements), end="\r", flush=True)
65+
if total_diffed % 250 == 0:
66+
print("{}/{} diffed ...".format(total_diffed, total_same_elements), end="\r", flush=True)
6567
old_element = self.old.by_id(global_id)
6668
new_element = self.new.by_id(global_id)
67-
self.diff_element(old_element, new_element)
68-
self.diff_element_inverse_relationships(old_element, new_element)
69-
69+
if self.diff_element(old_element, new_element) and self.is_shallow:
70+
continue
71+
if self.diff_element_inverse_relationships(old_element, new_element) and self.is_shallow:
72+
continue
7073
representation_id = self.get_representation_id(new_element)
7174
if representation_id in self.representation_ids:
7275
continue
73-
self.representation_ids.append(representation_id)
76+
self.representation_ids.add(representation_id)
7477
self.diff_element_geometry(old_element, new_element)
7578

7679
print(" - {} item(s) were changed either geometrically or with data".format(len(self.change_register.keys())))
@@ -114,15 +117,16 @@ def diff_element(self, old_element, new_element):
114117
significant_digits=self.precision,
115118
ignore_string_type_changes=True,
116119
ignore_numeric_type_changes=True,
117-
exclude_regex_paths=[
120+
exclude_regex_paths={
118121
r"root.*id$",
119122
r".*Representation.*",
120123
r".*OwnerHistory.*",
121124
r".*ObjectPlacement.*",
122-
],
125+
},
123126
)
124127
if diff and new_element.GlobalId:
125128
self.change_register.setdefault(new_element.GlobalId, {}).update(diff)
129+
return True
126130

127131
def diff_element_inverse_relationships(self, old_element, new_element):
128132
if not self.inverse_classes:
@@ -154,6 +158,7 @@ def diff_element_inverse_relationships(self, old_element, new_element):
154158
)
155159
if diff and new_element.GlobalId:
156160
self.change_register.setdefault(new_element.GlobalId, {}).update(diff)
161+
return True
157162

158163
def diff_element_geometry(self, old_element, new_element):
159164
try:
@@ -167,32 +172,35 @@ def diff_element_geometry(self, old_element, new_element):
167172
exclude_regex_paths=r"root.*id$",
168173
)
169174
DeepDiff(
170-
old_element.Representation,
171-
new_element.Representation,
175+
old_element.HasOpenings,
176+
new_element.HasOpenings,
172177
terminate_on_first=True,
173-
skip_after_n=1000, # Arbitrary value to "skim" check
174178
significant_digits=self.precision,
175179
ignore_string_type_changes=True,
176180
ignore_numeric_type_changes=True,
177181
exclude_regex_paths=r"root.*id$",
178182
)
179183
DeepDiff(
180-
old_element.HasOpenings,
181-
new_element.HasOpenings,
184+
old_element.HasProjections,
185+
new_element.HasProjections,
182186
terminate_on_first=True,
183187
significant_digits=self.precision,
184188
ignore_string_type_changes=True,
185189
ignore_numeric_type_changes=True,
186190
exclude_regex_paths=r"root.*id$",
187191
)
188192
DeepDiff(
189-
old_element.HasProjections,
190-
new_element.HasProjections,
193+
old_element.Representation.get_info_2(recursive=True),
194+
new_element.Representation.get_info_2(recursive=True),
191195
terminate_on_first=True,
196+
skip_after_n=500, # Arbitrary value to "skim" check
192197
significant_digits=self.precision,
193198
ignore_string_type_changes=True,
194199
ignore_numeric_type_changes=True,
195-
exclude_regex_paths=r"root.*id$",
200+
exclude_regex_paths=[
201+
r"root.*id']$",
202+
r".*ContextOfItems.*",
203+
],
196204
)
197205
except:
198206
if new_element.GlobalId:

0 commit comments

Comments
 (0)