Skip to content

Commit e91ca48

Browse files
AlfVIIclaude
andcommitted
perf(CoilAdviser): early-terminate pattern enumeration once cap reached
CoilAdviser::get_advised_coil iterated patterns × repetitions × insulation-combinations exhaustively even when 1 result was requested (every WebFrontend Wire-Advise-All call passes maximumNumberResults=1). For multi-winding coils (Flyback, LLC, Push-Pull, IBB, Vienna, Weinberg) the outer enumeration alone wastes work after a comfortable candidate pool exists. Cap the outer accumulation at 10× the caller's requested maximumNumberResults (≥10 minimum) so the stable_sort still has headroom to pick the top N, then break out of the repetition/pattern/ insulation loops. Note: this is necessary but not sufficient. The dominant cost on multi-winding cases is *inside* get_advised_coil_for_pattern (the wire-configuration × winding × operating-point enumeration on lines ~145-260). Even with this cap a single pattern iteration can exceed the WebFrontend Playwright @heavy timeout. Deeper restructuring of the inner loops is filed as follow-up. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 5fc3f62 commit e91ca48

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

src/advisers/CoilAdviser.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,22 @@ namespace OpenMagnetics {
245245
size_t maximumNumberResultsPerPattern = std::max(2.0, ceil(maximumNumberResults / (patterns.size() * repetitions.size())));
246246
logEntry("Trying " + std::to_string(repetitions.size()) + " repetitions and " + std::to_string(patterns.size()) + " patterns", "CoilAdviser");
247247

248+
// Early-termination cap. For multi-winding coils (Flyback, LLC,
249+
// Push-Pull, IBB, Vienna, Weinberg…) the patterns × repetitions ×
250+
// insulation-combinations enumeration easily exceeds the 240 s
251+
// Playwright @heavy timeout — and once we have a comfortable pool
252+
// of candidates for scoring, additional iterations add nothing.
253+
// Allow 10× the caller's request so the stable_sort still has
254+
// headroom to pick the top maximumNumberResults; for the typical
255+
// calculate_advised_coil(mas, 1) call this caps at 10 candidates.
256+
const size_t earlyTerminationCap = std::max<size_t>(maximumNumberResults * 10, 10);
257+
248258
std::vector<Mas> masesWithCoil;
259+
bool earlyTerminated = false;
249260
for (auto repetition : repetitions) {
261+
if (earlyTerminated) break;
250262
for (auto pattern : patterns) {
263+
if (earlyTerminated) break;
251264
auto aux = mas.get_mutable_magnetic().get_mutable_coil().check_pattern_and_repetitions_integrity(pattern, repetition);
252265
pattern = aux.first;
253266
repetition = aux.second;
@@ -285,6 +298,12 @@ namespace OpenMagnetics {
285298
auto resultsPerPattern = get_advised_coil_for_pattern(wires, mas, pattern, repetition, solidInsulationRequirementsForWires, maximumNumberResultsPerPattern, reference);
286299

287300
std::move(resultsPerPattern.begin(), resultsPerPattern.end(), std::back_inserter(masesWithCoil));
301+
if (masesWithCoil.size() >= earlyTerminationCap) {
302+
logEntry("CoilAdviser early-terminating: " + std::to_string(masesWithCoil.size()) +
303+
" candidates (cap=" + std::to_string(earlyTerminationCap) + ")", "CoilAdviser");
304+
earlyTerminated = true;
305+
break;
306+
}
288307
}
289308

290309
}
@@ -298,6 +317,12 @@ namespace OpenMagnetics {
298317
reference += std::to_string(mas.get_magnetic().get_coil().get_functional_description()[0].get_number_turns());
299318
auto resultsPerPattern = get_advised_planar_coil_for_pattern(wires, mas, pattern, repetition, maximumNumberResultsPerPattern, reference);
300319
std::move(resultsPerPattern.begin(), resultsPerPattern.end(), std::back_inserter(masesWithCoil));
320+
if (masesWithCoil.size() >= earlyTerminationCap) {
321+
logEntry("CoilAdviser early-terminating (planar): " + std::to_string(masesWithCoil.size()) +
322+
" candidates (cap=" + std::to_string(earlyTerminationCap) + ")", "CoilAdviser");
323+
earlyTerminated = true;
324+
break;
325+
}
301326
}
302327
}
303328
}

0 commit comments

Comments
 (0)