Skip to content

perf(linter/plugins): use start and end instead of range in tokens methods#16808

Merged
graphite-app[bot] merged 1 commit intomainfrom
12-13-perf_linter_plugins_use_start_and_end_instead_of_range_in_tokens_methods
Dec 13, 2025
Merged

perf(linter/plugins): use start and end instead of range in tokens methods#16808
graphite-app[bot] merged 1 commit intomainfrom
12-13-perf_linter_plugins_use_start_and_end_instead_of_range_in_tokens_methods

Conversation

@overlookmotel
Copy link
Copy Markdown
Member

@overlookmotel overlookmotel commented Dec 13, 2025

#16805 added start and end properties to tokens. Use them instead of the slower and more verbose range[0] and range[1] in all tokens methods.

Note: Only use start and end for tokens and comments, as we produce them and know for sure that these properties are present. Continue to use range for objects passed to methods by user.

@github-actions github-actions bot added A-linter Area - Linter A-cli Area - CLI A-linter-plugins Area - Linter JS plugins labels Dec 13, 2025
@github-actions github-actions bot added the C-performance Category - Solution not expected to change functional behavior, only performance label Dec 13, 2025
Copy link
Copy Markdown
Member Author

overlookmotel commented Dec 13, 2025


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@overlookmotel overlookmotel force-pushed the 12-13-perf_linter_plugins_use_start_and_end_instead_of_range_in_tokens_methods branch from 485d2a0 to 12d543f Compare December 13, 2025 18:42
@overlookmotel overlookmotel force-pushed the 12-13-refactor_linter_plugins_shorten_nodeortoken_comment_type branch from b1f2d7a to 6274182 Compare December 13, 2025 18:42
@overlookmotel overlookmotel self-assigned this Dec 13, 2025
@overlookmotel overlookmotel marked this pull request as ready for review December 13, 2025 18:49
Copilot AI review requested due to automatic review settings December 13, 2025 18:49
Copy link
Copy Markdown
Contributor

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

This PR optimizes token-related methods in the linting plugin by using direct start and end properties instead of array indexing (range[0] and range[1]) for system-produced tokens and comments. This is a performance improvement that makes property access faster and more direct. The PR correctly maintains the use of range array indexing for user-provided parameters where these properties may not exist.

Key changes:

  • Removed TODO comment about replacing range[0] with start (now addressed)
  • Updated all token/comment property accesses in binary search and iteration logic to use start and end
  • Preserved range[0] and range[1] usage for user-provided function parameters

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

Copy link
Copy Markdown

@charliecreates charliecreates bot left a comment

Choose a reason for hiding this comment

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

Most changes correctly replace range[0/1] with start/end for tokens/comments and keep range for user-supplied nodes. Two potential improvements remain: strengthen debugCheckTokensAndComments() to validate end/non-overlap invariants, and consider a token/comment fast-path in getTokenAfter() to use end when applicable.

Additional notes (2)
  • Maintainability | apps/oxlint/src-js/plugins/tokens.ts:259-263
    expected.sort() is now comparing only a.start/b.start and throws on ties. Previously it used range[0] which is equivalent for start comparisons, but the debug check still doesn't validate the end ordering invariant (i.e., that the merged list is strictly non-overlapping and monotonically increasing by end). Since this debug function is explicitly meant to validate ordering correctness, it's a good opportunity to strengthen it now that end is available on tokens/comments.

  • Performance | apps/oxlint/src-js/plugins/tokens.ts:1011-1019
    getTokenAfter() still reads rangeEnd from nodeOrToken.range[1], which is fine per the instruction to keep using range for user-provided params. However, this function searches in tokenList which may include comments when includeComments is true; the binary search compares tokenList[mid].start against rangeEnd (good). The potential issue is correctness when nodeOrToken is itself a token/comment: using range[1] is correct, but if we know nodeOrToken is a token/comment in this code path, we could use end and avoid array indexing. As written, it unconditionally uses range, missing the perf win for the token/comment case.

Summary of changes

What changed

  • Updated token/comment position access throughout apps/oxlint/src-js/plugins/tokens.ts to use the new token.start / token.end properties instead of token.range[0] / token.range[1].
  • Removed an obsolete TODO in initTokensAndComments() about migrating from range to start.
  • Adjusted ordering/check logic in debugCheckTokensAndComments() and whitespace detection (isSpaceBetween*) to rely on start/end.
  • Updated all binary searches in token query helpers (getTokens*, getFirstToken*, getLastToken*, getToken*) to compare against start.

Overall this is a targeted perf-oriented refactor that keeps range for user-provided node inputs while using start/end for internally-produced tokens/comments, matching the PR context.

@charliecreates charliecreates bot removed the request for review from CharlieHelps December 13, 2025 18:55
@graphite-app graphite-app bot force-pushed the 12-13-refactor_linter_plugins_shorten_nodeortoken_comment_type branch from 6274182 to f567ee7 Compare December 13, 2025 18:59
@graphite-app graphite-app bot force-pushed the 12-13-perf_linter_plugins_use_start_and_end_instead_of_range_in_tokens_methods branch from 12d543f to ad06b3f Compare December 13, 2025 19:00
@overlookmotel overlookmotel force-pushed the 12-13-perf_linter_plugins_use_start_and_end_instead_of_range_in_tokens_methods branch from ad06b3f to 32ddb16 Compare December 13, 2025 21:14
@overlookmotel overlookmotel force-pushed the 12-13-refactor_linter_plugins_shorten_nodeortoken_comment_type branch from f567ee7 to 2ddff0c Compare December 13, 2025 21:14
@overlookmotel overlookmotel force-pushed the 12-13-perf_linter_plugins_use_start_and_end_instead_of_range_in_tokens_methods branch from 32ddb16 to 9b6667a Compare December 13, 2025 22:12
@overlookmotel overlookmotel force-pushed the 12-13-refactor_linter_plugins_shorten_nodeortoken_comment_type branch 2 times, most recently from 395c082 to ae67bee Compare December 13, 2025 22:20
@overlookmotel overlookmotel force-pushed the 12-13-perf_linter_plugins_use_start_and_end_instead_of_range_in_tokens_methods branch from 9b6667a to cf8e0a1 Compare December 13, 2025 22:20
@graphite-app graphite-app bot added the 0-merge Merge with Graphite Merge Queue label Dec 13, 2025
@graphite-app
Copy link
Copy Markdown
Contributor

graphite-app bot commented Dec 13, 2025

Merge activity

…ens methods (#16808)

#16805 added `start` and `end` properties to tokens. Use them instead of the slower and more verbose `range[0]` and `range[1]` in all tokens methods.

Note: Only use `start` and `end` for tokens and comments, as we produce them and know for sure that these properties are present. Continue to use `range` for objects passed to methods by user.
@graphite-app graphite-app bot force-pushed the 12-13-refactor_linter_plugins_shorten_nodeortoken_comment_type branch from ae67bee to c4fb462 Compare December 13, 2025 22:56
@graphite-app graphite-app bot force-pushed the 12-13-perf_linter_plugins_use_start_and_end_instead_of_range_in_tokens_methods branch from cf8e0a1 to 3b7d345 Compare December 13, 2025 22:57
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Dec 13, 2025
Base automatically changed from 12-13-refactor_linter_plugins_shorten_nodeortoken_comment_type to main December 13, 2025 23:02
@graphite-app graphite-app bot merged commit 3b7d345 into main Dec 13, 2025
19 checks passed
@graphite-app graphite-app bot deleted the 12-13-perf_linter_plugins_use_start_and_end_instead_of_range_in_tokens_methods branch December 13, 2025 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-cli Area - CLI A-linter Area - Linter A-linter-plugins Area - Linter JS plugins C-performance Category - Solution not expected to change functional behavior, only performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants