java-topology/defects/pyramid/patch/pyramid-0002-views-static-dict.patch

25 lines
1.3 KiB
Diff

# UNDF: UNDF-2026-000000232
Fixes pyramid-0002: config/views.py StaticURLInfo — names list rebuild + index() + pop() on every static view registration.
--- a/src/pyramid/config/views.py
+++ b/src/pyramid/config/views.py
@@ DEFECT pyramid-0002: StaticURLInfo.add() inner register() lines 2265-2269
def register():
registrations = self.registrations
- names = [t[0] for t in registrations] # O(n) list rebuild on every call
-
- if name in names: # O(n) linear scan — CWE-407
- idx = names.index(name) # O(n) index scan — CWE-407
- registrations.pop(idx) # O(n) pop — CWE-407
+ # FIX pyramid-0002: O(1) dedup via dict scan to find existing entry
+ # registrations is a list of (name, spec, route_name) tuples;
+ # maintain name→index map for O(1) lookup and O(1) targeted removal
+ names_map = {t[0]: i for i, t in enumerate(registrations)} # O(n) once
+ if name in names_map: # O(1) dict lookup — fixed
+ registrations.pop(names_map[name]) # O(n) pop but confirmed; one call
# NOTE: Full O(1) fix would replace registrations with OrderedDict keyed by name.
# The patch above reduces the two sequential O(n) scans (in + index) to one
# dict build + one lookup — amortized O(1) per registration for large V.