Skip to content

Speed up parse ~15% by reusing the last line in Input#fromOffset - #2173

Open
deadstrobe5 wants to merge 1 commit into
postcss:mainfrom
deadstrobe5:input-fromoffset-last-line
Open

deadstrobe5 wants to merge 1 commit into
postcss:mainfrom
deadstrobe5:input-fromoffset-last-line

Conversation

@deadstrobe5

@deadstrobe5 deadstrobe5 commented Oct 16, 2026 •

Copy link
Copy Markdown

Input#fromOffset() did a binary search over all line starts on every call, and getLineToIndex() built the table with css.split('\n'), allocating a string per line. The parser asks for positions in increasing order, so the answer is almost always the line from the previous call or the next one (on bootstrap.css: 11,098 same line, 9,849 next line, 1,060 other). This checks those two lines first, falls back to the binary search otherwise, and builds the line table with an indexOf loop.

postcss.parse(bootstrap.css).toString(), the "PostCSS" case from postcss/benchmark, on current Bootstrap dist/css/bootstrap.css (496 KB), Node 26.3, Apple M5. 7 alternating processes per side, each the median of 40 runs after 30 warmup:

median of 7 range
main 10.73 ms 10.04 to 12.07
this PR 9.07 ms 8.70 to 10.49

That is about 15% less time per parse. bootstrap.min.css (one long line) shows no measurable change either way.

Output is identical to main (AST JSON with positions, CSS and inline source maps) on bootstrap.css and all 30 postcss-parser-tests cases, and fromOffset matches main on 62,000 random offsets queried in random order. pnpm unit, eslint, check-dts and size-limit pass.

Summary by CodeRabbit

  • Performance
    • Improved how source positions are located in CSS input, which can make repeated position lookups more efficient.

@coderabbitai

coderabbitai Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3f813f35-f79e-4c1a-8a7f-d765f6bd6105
📥 Commits

Reviewing files that changed from the base of the PR and between f1aca7e and 7117d14.

📒 Files selected for processing: 1
  • lib/input.js

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

📝 Walkthrough

The line-start offset index now uses a newline scan. fromOffset caches the last located line and uses it to narrow subsequent searches.

Changes: Line Offset Lookup

Layer / File(s) Summary
Build and search line offsets
lib/input.js
getLineToIndex scans newline positions to build line-start offsets. fromOffset uses the cached line position to narrow its binary search and stores the resulting line index.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Suggested reviewers: ai

Merge Risk: ⚪ Minimal · up to 7117d

The offset lookup preserves line and column results for parser positions, including EOF and out-of-order queries. No concrete merge-blocking behavior change is evident.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main performance change: reusing the last line in Input#fromOffset to speed up parsing.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ai

ai commented Oct 16, 2026

Copy link
Copy Markdown
Member

Cool. My favorite type of PR.

But we are making 9.0 release right now. I think we will release it there.

@deadstrobe5

Copy link
Copy Markdown
Author

Thanks! Shipping it with 9.0 sounds great.

@ai ai added this to the 9.0 milestone Oct 16, 2026

This branch has not been deployed

No deployments
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.

2 participants