Skip to content

Commit a441e37

Browse files
committed
Avoid quadratic mask-domain remapping
1 parent 2bf6e8e commit a441e37

1 file changed

Lines changed: 13 additions & 17 deletions

File tree

newton/_src/sim/builder.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4085,25 +4085,21 @@ def _merge_builder_custom_attributes(
40854085
collision_mask_domain_remap: dict[int, int] = {}
40864086
source_domain_attr = builder.custom_attributes.get(collision_mask_domain_key)
40874087
if source_domain_attr is not None and source_domain_attr.values:
4088-
source_values = (
4089-
source_domain_attr.values.values()
4088+
source_items = (
4089+
source_domain_attr.values.items()
40904090
if isinstance(source_domain_attr.values, dict)
4091-
else source_domain_attr.values
4091+
else enumerate(source_domain_attr.values)
40924092
)
4093-
source_domains = sorted({int(value) for value in source_values if int(value) >= 0})
4094-
merged_domain_attr = self.custom_attributes.get(collision_mask_domain_key)
4095-
if merged_domain_attr is not None and merged_domain_attr.values:
4096-
merged_values = (
4097-
merged_domain_attr.values.values()
4098-
if isinstance(merged_domain_attr.values, dict)
4099-
else merged_domain_attr.values
4100-
)
4101-
next_domain = max((int(value) for value in merged_values if int(value) >= 0), default=-1) + 1
4102-
else:
4103-
next_domain = 0
4104-
collision_mask_domain_remap = {
4105-
source_domain: next_domain + index for index, source_domain in enumerate(source_domains)
4106-
}
4093+
# Copied shape ranges never overlap, so the first destination shape
4094+
# in each source domain is already a unique, deterministic ID. This
4095+
# avoids rescanning the growing destination during replication.
4096+
shape_offset = entity_offsets["shape"]
4097+
for shape, value in source_items:
4098+
if value is None:
4099+
continue
4100+
source_domain = int(value)
4101+
if source_domain >= 0:
4102+
collision_mask_domain_remap.setdefault(source_domain, shape_offset + shape)
41074103

41084104
def get_offset(entity_or_key: str | None) -> int:
41094105
if entity_or_key is None:

0 commit comments

Comments
 (0)