Skip to content

perf!: replace LinkedList with Vector for child node storage#79

Merged
usernane merged 1 commit into
devfrom
perf/vector-child-storage
Jun 10, 2026
Merged

perf!: replace LinkedList with Vector for child node storage#79
usernane merged 1 commit into
devfrom
perf/vector-child-storage

Conversation

@usernane

Copy link
Copy Markdown
Member

Summary

Replace LinkedList with Vector for HTMLNode child storage, eliminating O(n²) index-access patterns.

Motivation

LinkedList::get($index) is O(n), making loops over children O(n²). With 1000 children, rendering alone took 134ms. Fixes #66.

Changes

  • Change $childrenList from LinkedList to Vector in HTMLNode
  • Replace removeElement() calls with remove() (Vector API)
  • Replace index-based remove() calls with removeAt()
  • Add bounds check in getChild() to return null instead of throwing
  • Update type hints in private helper methods

How to Test / Verify

All 308 existing tests pass. Benchmark results with 1000 children:

Operation LinkedList Vector Speedup
toHTML(false) 134 ms 15 ms 9x
getChildByID (last) 239 ms 7 ms 35x
index-based loop 105 ms 0.3 ms 363x
addChild() x 1000 25 ms 18 ms 1.4x
Full doc build+render 43 ms 39 ms 1.1x

Breaking Changes and Migration Steps

  • children() return type changes from LinkedList to Vector
  • Code type-hinting LinkedList for children() return must change to Vector
  • Vector::get() throws OutOfRangeException on invalid index (mitigated by bounds check in getChild())

Checklist

  • I reviewed my own diff before requesting review
  • My commits follow Conventional Commits
  • I added/updated tests (or explained why not) — existing 308 tests pass
  • I updated docs (if needed) Docs Repo — N/A
  • I ran lint/cs-fixer (if applicable) (composer fix-cs) — N/A, minimal change
  • I considered backward compatibility
  • I considered security

Related issues

Closes #66

Replace LinkedList (O(n) index access) with Vector (O(1) index access)
for the internal children list. This eliminates O(n²) patterns in
rendering, ID searches, and tag searches.

Benchmarks with 1000 children:
- toHTML(): 9x faster (134ms → 15ms)
- getChildByID(): 35x faster (239ms → 7ms)
- index-based loop: 363x faster (105ms → 0.3ms)

BREAKING CHANGE: children() now returns Vector instead of LinkedList.
Code that type-hints LinkedList for children() return must update.

Closes #66
@usernane usernane merged commit 7c8ebe2 into dev Jun 10, 2026
2 checks passed
@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Jun 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.31%. Comparing base (cf3dfe1) to head (38e0a7a).
⚠️ Report is 15 commits behind head on dev.

Additional details and impacted files
@@             Coverage Diff              @@
##                dev      #79      +/-   ##
============================================
- Coverage     98.44%   98.31%   -0.13%     
- Complexity      898      917      +19     
============================================
  Files             6        6              
  Lines          1799     1843      +44     
============================================
+ Hits           1771     1812      +41     
- Misses           28       31       +3     
Flag Coverage Δ
php-8.1 ?
php-8.2 ?
php-8.3 98.31% <100.00%> (-0.02%) ⬇️
php-8.4 ?
php-8.5 97.50% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant