FhSim  3.1.0
Marine systems simulation
Loading...
Searching...
No Matches
0211 — The net file reader is quadratic in the number of panels: every attribute read resolves its path from the root
ID 0211
Class ARCHITECTURE
Severity 2
Status ready

Models: Net/NetStructure, Net/NetStructureWithConstraints

Found: Deep review 2026-09-28: net-force-performance A5 (worktrees/nethydro/review/deep/net-force-performance.md, at 1e94ae1); rechecked against 1e94ae1 and filed by the deep-review fix pass (branch fix/deep-review-net)

Decision needed:

Deep review net-force-performance.md A5 (review severity 2). Confirmed by measurement and code trace. Not fixed here: the quadratic cost is in fhsim's InputReader, which has no handle-based API the reader could use instead.

Evidence

  • src/net/NetStructureSpec.cpp:187-240 (panels) and :250-330 (cables) read about ten attributes per El<i> through InputReader::Get*Attribute(tag, ..., AttributePath).
  • fhsim src/simobject/utilities/InputReader.cpp GetElement(path) walks the path from the document root on every call: FirstChildElement(name) → nextElement (XmlDoc.cpp:37-48), a linear scan of the siblings with a string compare. So every attribute of El<i> scans i siblings: O(P) per attribute, about 10 P²/2 compares per net.
  • Review measurement: callgrind p2k: NetStructureSpec::ReadInputFile 2.28 G Ir, 32 % of a 0.1 s run (nextElement alone 2.12 G); p20k spends ≈ 14 s in setup. NetStructureArray has its own loop over the same reader (NetStructureArray.cpp:320-366).
  • InputReader (fhsim include/fhsim/InputReader.h) offers only path-based reads and GetAllAttributes / GetAllElements; no call returns an element handle to read from.

Effect

Setup of a 10 000-panel net spends seconds, a 50 000-panel net minutes, in the reader.

Possible fix

In fhsim (a new FHSIM item): cache the last resolved AttributePath → XmlNode in InputReader::GetElement (the document is immutable after loading), which makes the loop linear with no change here; or add a handle-based read API that this reader then uses. Either gives identical output. Reimplementing the attribute parsing in this library would duplicate InputReader's parsing and logging and risks changing messages and defaults.

Test that would prove it

In fhsim: a count of sibling visits (or a timing) for reading N elements, linear in N; here, the regression baselines bit-identical and a timing of the p20k setup.

Risk

None for the results; the fix lands upstream.

Progress (2026-09-30)

Re-checked: fhsim's InputReader::GetElement still walks the path from the document root on every call. The fix is upstream in fhsim (a cache of the last resolved path, or a handle-based API) and is not filed there yet; Net/NetStructureArray, which had its own copy of the loop, was removed (MARE-0080), so Net/NetStructure and Net/NetStructureWithConstraints share the one in NetStructureSpec::ReadInputFile. Nothing to change here.