Skip to content

perf(names): build the voltage-level names once, cheaply — RTE grid loads in ~11 s again (was ~80 s) - #231

Merged
marota merged 1 commit into
ainetus:mainfrom
marota:perf/voltage-level-names
Nov 27, 2026
Merged

marota merged 1 commit into
ainetus:mainfrom
marota:perf/voltage-level-names

Conversation

@marota

@marota marota commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

Why

Study loads on the RTE 7000 grid had become slow: 76–108 s, on main and on #230 alike. VoltageLevelNames.__init__ (from 2bfae39) took 19 s, all of it in _reach. On that grid every voltage level has an empty name, so none has a real name of its own (_own is empty) and all 6 835 count as nameless. _reach then walked the whole grid from each of them (~46 M steps) without ever finding a named substation. The namer was also built three times per load: once by the Context tab's capture_snapshot during /api/config, then once each by /api/branches and /api/voltage-levels. Those two arrive together and race on the unlocked cache in network_service.voltage_level_namer(), so each built its own (40–70 s per request).

What

  1. No walk when nothing is named. The walk runs only when at least one voltage level has a name of its own. Every bus stays nameless (is_nameless) exactly as before.
  2. Bounded walk. _reach stops at the end of the hop that finds _ENOUGH_PLACES = 3 substations. _pick reads the nearest group (at most 3), completed with the next nearest up to 2, so 3 found over whole hops settle its answer even with one of them left out (seen_from). Two would be enough for today's calls, but three also covers _pick with an exclusion and two names. The test checks that stronger property.
  3. One namer per network. voltage_level_namer() builds under a threading.Lock with a double check, and keeps the namer with the network it was built for. A build that finishes after a new study has replaced the network is therefore never served. /api/config passes that namer to context_map_service.capture(..., vl_names=...), which hands it to capture_snapshot(network, vl_names).

Names unchanged

I dumped every name the namer feeds, before and after:

  • as_dict, name, is_nameless;
  • seen_from at both ends of every branch;
  • the rewrite tokens;
  • get_voltage_level_names, get_display_names and the Context tab's names.

They are byte-identical on pypsa_eur_fr225_400, fr400, eur380_400, bare_env_small_grid_test and the RTE grid. So are the /api/branches (700 KB) and /api/voltage-levels (69 KB) responses of the two backends below.

Timings (RTE 7000 grid)

VoltageLevelNames(network): 17.8 s → 0.02 s. The PyPSA grids are unchanged (5–25 ms).

The study load is measured as the UI does it: POST /api/config, then /api/branches, /api/voltage-levels and /api/network-diagram?format=text in parallel. Each side ran on its own backend copy, and the 4 loads per side were interleaved:

/api/config branches voltage levels N diagram total
before (6c1702c) 27.3 / 29.2 / 27.7 / 28.3 s 52.5 / 51.4 / 49.6 / 51.7 s 53.0 / 52.4 / 49.1 / 51.0 s 3.0 / 2.7 / 1.4 / 1.6 s 80.3 / 81.6 / 77.2 / 80.0 s
after 9.4 / 10.3 / 8.2 / 10.1 s 1.8 / 0.6 / 2.0 / 0.6 s < 0.05 s 2.0 / 0.7 / 2.1 / 0.4 s 11.4 / 10.9 / 10.3 / 10.7 s

Tests

  • TestBoundedWalk builds the names twice, with the bound and with _ENOUGH_PLACES = inf (an unbounded walk), and compares:

    • as_dict, name, is_nameless and the rewrite tokens;
    • seen_from from every voltage level to every other;
    • _pick with every substation left out, asking for one or two names.

    It runs on hand-built graphs: ties at the nearest hop, a substation at two voltages counting once, taps several hops away. It also runs on 200 seeded random grids, where 159 walks are cut short.

    Mutation check: a bound of 1 changes names. A bound of 2 keeps today's names but breaks _pick with an exclusion and two names.

  • Skipped walk: a grid without a single name is not walked, and its branches are not even read.

  • One namer per network: four racing requests build one namer (the old code built four), and a namer built for a replaced network is never served.

  • Context-map wiring: the capture reads the namer it is handed, and /api/config passes it the network service's namer.

  • Suite: pytest expert_backend/tests gives 1434 passed, 5 skipped. The 2 failures already on main still fail the same way. check_code_quality.py is OK and the OpenAPI contract is unchanged.

Docs: sld-diagram-feeder-labels.md § 4, CHANGELOG, and the backend and tests CLAUDE.md guides.

🤖 Generated with Claude Code

…oads fast again

VoltageLevelNames (2bfae39) names a nameless bus by the real substations it
ties, walking the branches through the other nameless buses. On the RTE 7000
grid no voltage level has a name (all 6 835 are empty), so every voltage level
was nameless and each walk crossed the whole grid without finding anything:
~46 M steps, 19 s per build. And the namer was built three times per study
load: by the Context tab's snapshot during /api/config, then by /api/branches
and /api/voltage-levels, which race on network_service.voltage_level_namer's
unlocked cache and each built their own (40-70 s per request).

- The walks are skipped when no voltage level has a name of its own: there is
  no substation to find (every bus stays nameless, as before).
- A walk stops at the end of the hop that finds three substations
  (_ENOUGH_PLACES): _pick reads the nearest group (at most three) completed
  with the next nearest up to two, so three found over whole hops settle it
  even with one substation left out (seen_from).
- network_service.voltage_level_namer builds once per network under a lock
  (double check), and keeps the namer with its network so one built for a
  network replaced meanwhile is never served. /api/config hands that namer to
  the Context tab's capture (capture_snapshot(network, vl_names)), so a study
  load builds it once.

Names are unchanged: every name the namer feeds (as_dict, name, seen_from at
both ends of every branch, rewrite tokens, the /api/branches and
/api/voltage-levels name maps, the Context tab names) is byte-identical
before and after on pypsa_eur_fr225_400, fr400, eur380_400, the small test
grid and the RTE grid. A unit test compares the bounded walk with an
unbounded one on hand-built graphs (ties, a substation at two voltages, taps
several hops away) and 200 seeded random grids, for name, seen_from and _pick
with any substation left out; others cover the skipped walk, the namer built
once by racing requests, the replaced network, and the snapshot reading the
namer it is handed.

VoltageLevelNames on the RTE grid: 17.8 s -> 0.02 s. Study load (POST
/api/config, then branches + voltage levels + N diagram in parallel):
  before  77.2-81.6 s (/api/config 27-29 s, branches 50-53 s,
          voltage levels 49-53 s, N diagram 1-3 s)
  after   10.3-11.4 s (/api/config 8-10 s, branches 0.6-2 s,
          voltage levels < 0.1 s, N diagram 0.4-2 s)
4 interleaved loads each, one backend copy per side; their /api/branches and
/api/voltage-levels responses are byte-identical.

Docs: sld-diagram-feeder-labels.md § 4, CHANGELOG, backend / tests CLAUDE.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Antoine Marot <amarot91@gmail.com>
@marota
marota merged commit 5195435 into ainetus:main Nov 27, 2026
6 checks passed
marota pushed a commit to marota/Co-Study4Grid that referenced this pull request Nov 27, 2026
…ng-rate-view

CHANGELOG: both sides add sections under [Unreleased]; kept both, this
branch's (Loading view, Context twin consignation) first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Antoine Marot <amarot91@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant