From 6a7cc74c48f0ce021b4c84cb7412683a9c0e9ae8 Mon Sep 17 00:00:00 2001 From: Alex Kudentsov <43482574+alexk37@users.noreply.github.com> Date: Thu, 1 Oct 2026 23:06:45 +0700 Subject: policy: T9379: prefix-list duplicate check in linear time verify() looked up each prefix-list rule in a list of the rules seen so far, which is quadratic in the size of the list and runs on every policy commit. verify() alone, run from the source on a workstation, took 41.8 s for a 40,000-rule list; on a 1.5.1 VM the check cost 0.08 s at 2,000 rules. Keep a set of the rules' sorted items instead. Every value under a prefix-list rule is a single leaf, so the key is hashable, and two keys are equal exactly when the rule dicts are: the same rules are flagged, with the same message. The workstation benchmark took 0.11 s. --- src/conf_mode/policy.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/conf_mode/policy.py b/src/conf_mode/policy.py index 9f10178af..7cc2488b5 100755 --- a/src/conf_mode/policy.py +++ b/src/conf_mode/policy.py @@ -131,7 +131,7 @@ def verify(config_dict): # human readable instance name (hyphen instead of underscore) policy_hr = policy_type.replace('_', '-') - entries = [] + entries = set() for rule, rule_config in instance_config['rule'].items(): mandatory_error = f'must be specified for "{policy_hr} {instance} rule {rule}"!' if 'action' not in rule_config: @@ -185,10 +185,11 @@ def verify(config_dict): f'"le" ({le})' ) - if rule_config in entries: + key = tuple(sorted(rule_config.items())) + if key in entries: raise ConfigError( f'Rule "{rule}" contains a duplicate prefix definition!') - entries.append(rule_config) + entries.add(key) # route-maps tend to be a bit more complex so they get their own verify() section if 'route_map' in policy: -- cgit v1.2.3