Commit f893713
authored
Optimize _add_global_declarations_for_language
The optimized code achieves a **102% speedup** (from 409ms to 202ms) by eliminating redundant tree-sitter parsing operations when inserting multiple declarations.
**Key optimization:** In the original code, after inserting each new declaration, the entire source was re-parsed via `analyzer.find_module_level_declarations(result)` to update line numbers. With many declarations (e.g., 100+ in test scenarios), this caused quadratic behavior—each insertion triggered a full parse of increasingly larger source code.
The optimization introduces `_insert_declaration_after_dependencies_fast()`, which returns not just the modified source but also metadata about the insertion: the insertion line and number of lines added. Instead of re-parsing, the code now updates the `existing_decl_end_lines` dictionary incrementally by:
1. Shifting end lines of declarations appearing after the insertion point
2. Recording the newly inserted declaration's end line directly
This transforms O(n²) parse operations into O(n) dictionary updates, where n is the number of declarations.
**Performance gains by test category:**
- **Dependency chains** (100 declarations): 1326% faster (37.2ms → 2.61ms)
- **Independent declarations** (100 items): 88.3% faster (61.3ms → 32.6ms)
- **Wide dependency graphs** (100 items): 1291% faster (42.2ms → 3.03ms)
- **Simple cases** (1-3 declarations): 15-25% faster
The optimization is most impactful when inserting many declarations with dependencies—precisely the scenario where re-parsing becomes expensive. For codebases with optimized code introducing numerous helper constants or utility declarations, this eliminates a major performance bottleneck while maintaining identical correctness.1 parent c1128eb commit f893713
1 file changed
Lines changed: 94 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
793 | 793 | | |
794 | 794 | | |
795 | 795 | | |
796 | | - | |
| 796 | + | |
| 797 | + | |
797 | 798 | | |
798 | 799 | | |
| 800 | + | |
799 | 801 | | |
800 | | - | |
801 | | - | |
802 | | - | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
803 | 814 | | |
804 | 815 | | |
805 | 816 | | |
| |||
1096 | 1107 | | |
1097 | 1108 | | |
1098 | 1109 | | |
| 1110 | + | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + | |
| 1187 | + | |
| 1188 | + | |
0 commit comments