Repository navigation
perf(names): build the voltage-level names once, cheaply — RTE grid loads in ~11 s again (was ~80 s) - #231
Merged
Conversation
…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
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>
5 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Study loads on the RTE 7000 grid had become slow: 76–108 s, on
mainand on #230 alike.VoltageLevelNames.__init__(from 2bfae39) took 19 s, all of it in_reach. On that grid every voltage level has an emptyname, so none has a real name of its own (_ownis empty) and all 6 835 count as nameless._reachthen 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'scapture_snapshotduring/api/config, then once each by/api/branchesand/api/voltage-levels. Those two arrive together and race on the unlocked cache innetwork_service.voltage_level_namer(), so each built its own (40–70 s per request).What
is_nameless) exactly as before._reachstops at the end of the hop that finds_ENOUGH_PLACES = 3substations._pickreads 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_pickwith an exclusion and two names. The test checks that stronger property.voltage_level_namer()builds under athreading.Lockwith 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/configpasses that namer tocontext_map_service.capture(..., vl_names=...), which hands it tocapture_snapshot(network, vl_names).Names unchanged
I dumped every name the namer feeds, before and after:
as_dict,name,is_nameless;seen_fromat both ends of every branch;rewritetokens;get_voltage_level_names,get_display_namesand the Context tab's names.They are byte-identical on
pypsa_eur_fr225_400,fr400,eur380_400,bare_env_small_grid_testand 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-levelsand/api/network-diagram?format=textin parallel. Each side ran on its own backend copy, and the 4 loads per side were interleaved:/api/config6c1702c)Tests
TestBoundedWalkbuilds the names twice, with the bound and with_ENOUGH_PLACES = inf(an unbounded walk), and compares:as_dict,name,is_namelessand the rewrite tokens;seen_fromfrom every voltage level to every other;_pickwith 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
_pickwith 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/configpasses it the network service's namer.Suite:
pytest expert_backend/testsgives 1434 passed, 5 skipped. The 2 failures already onmainstill fail the same way.check_code_quality.pyis OK and the OpenAPI contract is unchanged.Docs:
sld-diagram-feeder-labels.md§ 4, CHANGELOG, and the backend and testsCLAUDE.mdguides.🤖 Generated with Claude Code