From 452ae462eac92441650a1790d45078fa6b03d8d3 Mon Sep 17 00:00:00 2001 From: Lachlan Grose Date: Fri, 27 Nov 2026 00:56:09 +1030 Subject: [PATCH] fix(builder): check each builder once per up_to_date call A feature depends on the builders of its faults, and a fault frame on the builders of the faults that cut it. BaseBuilder.up_to_date recursed into every fault builder, and each fault frame into its three coordinate builders, with no memory of what was already checked, so one check walked every path through that graph (x3 at each fault frame). It runs at the start of every evaluation. In a network of cutting faults this dominated: building one fault of a 94-fault model made 36 million up_to_date calls (and as many logger.info calls) for 652 fault applications. The outermost call now keeps the set of builders it has checked (in a context variable), and a builder already in it returns at once. The recursion still brings the faults up to date before the builder itself, so a skipped builder is already up to date. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XADRvHhG72666JkYNHtzUx --- .../features/builders/_base_builder.py | 20 +++++++ .../modelling/test_builder_up_to_date_once.py | 55 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 tests/unit/modelling/test_builder_up_to_date_once.py diff --git a/LoopStructural/modelling/features/builders/_base_builder.py b/LoopStructural/modelling/features/builders/_base_builder.py index bf52a7d3a..a5957e5fa 100644 --- a/LoopStructural/modelling/features/builders/_base_builder.py +++ b/LoopStructural/modelling/features/builders/_base_builder.py @@ -1,7 +1,16 @@ +from contextvars import ContextVar + from LoopStructural.utils import getLogger logger = getLogger(__name__) +# The builders already checked by the outermost up_to_date call. A feature depends +# on the builders of its faults, and a fault frame on the builders of the faults +# that cut it; without this set every path through that graph was checked again, +# so the check grew with the number of paths (millions in a network of cutting +# faults) and ran on every evaluation. +_CHECKED: ContextVar = ContextVar("loopstructural_builders_checked", default=None) + class BaseBuilder: def __init__(self, model, name: str = "Feature"): @@ -89,6 +98,17 @@ def up_to_date(self, callback=None): a function that is called when the feature is updated """ + checked = _CHECKED.get() + if checked is None: + token = _CHECKED.set(set()) + try: + return self.up_to_date(callback=callback) + finally: + _CHECKED.reset(token) + if id(self) in checked: + # this call already brought this builder up to date + return + checked.add(id(self)) logger.info(f'Feature: {self.name} up to date: {self._up_to_date}') for f in self.faults: f.builder.up_to_date(callback=callback) diff --git a/tests/unit/modelling/test_builder_up_to_date_once.py b/tests/unit/modelling/test_builder_up_to_date_once.py new file mode 100644 index 000000000..e05c6809e --- /dev/null +++ b/tests/unit/modelling/test_builder_up_to_date_once.py @@ -0,0 +1,55 @@ +"""One up_to_date check looks at each builder once. + +A feature depends on the builders of its faults, and a fault frame on the builders +of the faults that cut it. The check walked every path through that graph, so in a +network of cutting faults it grew with the number of paths and ran on every +evaluation (36 million calls to build one fault of a 94-fault model). +""" + +from collections import Counter + +import numpy as np +import pandas as pd + +from LoopStructural import GeologicalModel +from LoopStructural.interpolators import DiscreteInterpolator +from tests.fixtures.fault_models import COLUMNS, OLD, YOUNG, add_fault + + +def _model(): + model = GeologicalModel(np.zeros(3), np.ones(3)) + young = add_fault(model, "young", **YOUNG, steps=3) + old = add_fault(model, "old", **OLD, steps=3, faults=[young]) + add_fault(model, "oldest", center=(0.5, 0.5, 0.7), dip=70, dip_azimuth=0, + displacement=0.08, steps=3, faults=[old, young]) + xyz = np.random.default_rng(1).uniform(0, 1, (100, 3)) + data = pd.DataFrame({"X": xyz[:, 0], "Y": xyz[:, 1], "Z": xyz[:, 2], "val": xyz[:, 2], + "nx": np.nan, "ny": np.nan, "nz": np.nan, "coord": np.nan, + "feature_name": "strat"})[COLUMNS] + strat = model.create_and_add_foliation("strat", data=data, nelements=2000, buffer=0.3) + model.update() + return strat + + +def test_each_interpolator_is_checked_once(monkeypatch): + strat = _model() + reads = Counter() + flag = DiscreteInterpolator.up_to_date + + def counted(self): + reads[id(self)] += 1 + return flag.fget(self) + + monkeypatch.setattr(DiscreteInterpolator, "up_to_date", property(counted, flag.fset)) + strat.builder.up_to_date() + # strat and the 3 coordinates of each of the 3 fault frames + assert len(reads) == 10 + assert max(reads.values()) == 1 + + +def test_a_stale_fault_is_still_rebuilt(): + strat = _model() + young = strat.faults[-1] if strat.faults[-1].name == "young" else strat.faults[0] + young.builder.builders[0]._up_to_date = False + strat.builder.up_to_date() + assert young.builder.builders[0]._up_to_date