Commit 5e095b4
authored
Optimize replace_api_key_with_env_var_name
The optimized code replaces chained `.get()` calls with direct dictionary access inside a try/except block (`flow["data"]["nodes"]` instead of `flow.get("data", {}).get("nodes", [])`), which is faster when the keys exist (the common case) because CPython's exception handling is cheaper than repeated dict lookups and fallback-value creation. Line profiler shows this change reduced the per-node overhead: the optimized version spends ~4.8% of time in the single `template = node["data"]["node"]["template"]` line versus the original's cumulative ~14.1% across three separate `.get()` calls and `isinstance` checks. The optimization also hoists the `isinstance(value, dict)` check before examining `value.get("name")`, avoiding two dict lookups on non-dict values. Trade-off: exceptions now raised for malformed flows, but the early-exit try/except at the top preserves the original's robustness for missing top-level keys.1 parent 7afd5b1 commit 5e095b4
1 file changed
Lines changed: 13 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
94 | 94 | | |
95 | 95 | | |
96 | 96 | | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
103 | 106 | | |
104 | | - | |
105 | 107 | | |
106 | 108 | | |
107 | 109 | | |
108 | | - | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
109 | 113 | | |
110 | 114 | | |
111 | 115 | | |
112 | 116 | | |
113 | 117 | | |
114 | 118 | | |
| 119 | + | |
115 | 120 | | |
116 | 121 | | |
117 | 122 | | |
| |||
0 commit comments