Skip to content

Refactor walk and traverse to use arena-based node access#247

Open
bartveneman wants to merge 3 commits into
mainfrom
claude/optimize-walker-performance-a2Naj
Open

Refactor walk and traverse to use arena-based node access#247
bartveneman wants to merge 3 commits into
mainfrom
claude/optimize-walker-performance-a2Naj

Conversation

@bartveneman
Copy link
Copy Markdown
Member

Summary

Refactored the walk() and traverse() functions to directly access the CSS data arena instead of relying on node object properties. This change improves performance by avoiding repeated object property lookups and enables more efficient tree traversal.

Key Changes

  • Refactored walk() function: Now extracts arena, source, and index from the node and uses arena methods (get_type(), get_first_child(), get_next_sibling()) for traversal instead of accessing node properties
  • Refactored traverse() function: Similarly updated to use arena-based access for both enter and leave callbacks
  • Introduced internal helper functions: Added _walk() and _traverse() private functions that accept arena, source, and index parameters to avoid repeated extraction logic
  • Added internal accessors to CSSNode: Exposed __get_source() and __get_index() methods to allow walk/traverse functions to access the underlying data needed for arena operations
  • Updated .gitignore: Added package-lock.json to ignore list

Implementation Details

  • The refactoring maintains the same public API and control flow behavior (BREAK, SKIP)
  • Child traversal now uses arena indices (with 0 as null sentinel) instead of null-based linked list navigation
  • Depth calculation logic remains unchanged but is now applied consistently in both public and private functions
  • The helper functions reduce code duplication between the public entry points and recursive traversal logic

https://claude.ai/code/session_01UVBKCBKB11qjh9k2PwbLen

claude added 3 commits May 17, 2026 22:31
…cations

- Rewrite walk/traverse internals to navigate the arena directly using
  integer indices (get_first_child/get_next_sibling) instead of going
  through the first_child/next_sibling CSSNode getters for sibling
  traversal bookkeeping
- Eliminate the { enter, leave } object created on every recursive call
  in traverse — previously N extra allocations per traversal, now zero
- CSSNode wrappers are still created exactly once per visited node for
  callback invocations; the root node passed in is reused as-is
- Add __get_source() and __get_index() internal helpers to CSSNode to
  expose the arena index and source string needed by the new walker

https://claude.ai/code/session_01UVBKCBKB11qjh9k2PwbLen
@codecov-commenter
Copy link
Copy Markdown

Bundle Report

Changes will increase total bundle size by 1.48kB (0.78%) ⬆️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
@projectwallace/css-parser-esm 190.88kB 1.48kB (0.78%) ⬆️

Affected Assets, Files, and Routes:

view changes for bundle: @projectwallace/css-parser-esm

Assets Changed:

Asset Name Size Change Total Size Change (%)
css-node-BTgoFWDU.js (New) 14.88kB 14.88kB 100.0% 🚀
walk.js 1.36kB 3.18kB 74.56% ⚠️
css-node-C3t4b8Jt.js (Deleted) -14.76kB 0 bytes -100.0% 🗑️

Files in css-node-BTgoFWDU.js:

  • ./src/css-node.ts → Total Size: 14.66kB

Files in walk.js:

  • ./src/walk.ts → Total Size: 3.03kB

@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.20%. Comparing base (2f19177) to head (2437dc7).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #247      +/-   ##
==========================================
+ Coverage   93.14%   93.20%   +0.06%     
==========================================
  Files          17       17              
  Lines        3035     3062      +27     
  Branches      845      853       +8     
==========================================
+ Hits         2827     2854      +27     
  Misses        208      208              

☔ View full report in Codecov by Sentry.
📢 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.

3 participants