Skip to content

Fix O(n²) complexity in prepareList#50

Open
heiskr wants to merge 1 commit intosyntax-tree:mainfrom
heiskr:fix/prepare-list-quadratic-complexity
Open

Fix O(n²) complexity in prepareList#50
heiskr wants to merge 1 commit intosyntax-tree:mainfrom
heiskr:fix/prepare-list-quadratic-complexity

Conversation

@heiskr
Copy link
Copy Markdown

@heiskr heiskr commented Apr 9, 2026

Initial checklist

  • I read the support docs
  • I read the contributing guide
  • I agree to follow the code of conduct
  • I searched issues and discussions and couldn't find anything or linked relevant results below
  • I made sure the docs are up to date
  • I included tests (or that's not needed)

Description of changes

prepareList calls events.splice() twice per list item, making it O(n²). This defers insertions into arrays and applies them in a single backward merge pass, making it O(n). Also tightens the backward line-ending scan to stop at start instead of 0.

Fixes #49

Defer events.splice calls and apply them in a single backward
merge pass. Also tighten the backward line-ending scan to stop
at the list start.

Fixes syntax-tree#49

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot added the 👋 phase/new Post is being triaged automatically label Apr 9, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions Bot added 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Apr 9, 2026
@ChristianMurphy ChristianMurphy added the 🏁 area/perf This affects performance label Apr 9, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves list preprocessing performance by eliminating per-item events.splice() calls in prepareList, reducing complexity from O(n²) to O(n) for large lists.

Changes:

  • Defers listItem enter/exit insertions by collecting insertion positions/events during the walk.
  • Applies all deferred insertions in a single backward merge pass to avoid repeated array shifting.
  • Tightens the backward line-ending scan to stop at the current list’s start index.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Member

@Murderlon Murderlon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also no comments from Devin review. LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🏁 area/perf This affects performance 🤞 phase/open Post is being triaged manually

Development

Successfully merging this pull request may close these issues.

prepareList has O(n^2) complexity from per-item events.splice

4 participants