⚡ Bolt: Replace O(n²) nested loop with O(n) hash map lookup#617
⚡ Bolt: Replace O(n²) nested loop with O(n) hash map lookup#617seonghobae wants to merge 1 commit into
Conversation
Replaced the inline generator expression calculating the column position with an O(1) dictionary counter, reducing the overall time complexity from O(N^2) to O(N).
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
Pull request overview
Note
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
This PR optimizes DBML column parsing by replacing an O(N²) scan over already-parsed columns with an O(1) per-column counter keyed by relation OID, improving import performance on large DBML inputs.
Changes:
- Introduced
col_count_by_oidto track per-table column ordinals during parsing. - Replaced generator-based counting (
sum(1 for ...)) with dictionary counter lookup. - Documented the optimization in
.Jules/bolt.md.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| backend/app/spec/dbml_import.py | Uses an O(1) per-table counter to compute column_position instead of scanning columns. |
| .Jules/bolt.md | Adds a short note describing the DBML import complexity improvement. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| fk_specs: list[tuple[str, str, str, str, str, str]] = [] # child s/t/c, parent s/t/c | ||
|
|
||
| oid_by_table: dict[tuple[str, str], int] = {} | ||
| col_count_by_oid: dict[int, int] = {} |
| settings = (cm.group("settings") or "").lower() | ||
| oid = oid_by_table[current] | ||
| is_pk = bool(re.search(r"\bpk\b|primary\s+key", settings)) | ||
| col_count_by_oid[oid] = col_count_by_oid.get(oid, 0) + 1 |
| "relation_oid": oid, | ||
| "column_name": col_name, | ||
| "column_position": sum(1 for c in columns if c["relation_oid"] == oid) + 1, | ||
| "column_position": col_count_by_oid[oid], |
| ## 2024-07-22 - O(N^2) complexity in DBML import | ||
| **Learning:** Found an O(N^2) complexity issue in the DBML import parser where it was using an inline generator expression (`sum(1 for ...)`) to calculate the column position by iterating over all previously parsed columns. | ||
| **Action:** Replaced the inline loop with an auxiliary O(1) dictionary counter (`col_count_by_oid`) to keep a running tally of column positions. |
💡 What: Replaced the O(N^2) inline generator expression that computes column positions in
dbml_import.pywith an O(1) dictionary counter (col_count_by_oid).🎯 Why: In large DBML files, the parser was doing a full iteration over the previously collected columns array to calculate the ordinal index of a given column belonging to a relation.
📊 Impact: Reduces time complexity of the DBML import from O(N^2) to O(N) when parsing columns, resulting in much faster DBML loading.
🔬 Measurement: Verified by backend unit tests (
cd backend && pytest --cov=app), specifically ensuring test cases around DBML imports function properly without latency issues.PR created automatically by Jules for task 8362007228843951190 started by @seonghobae